unity3d Unity C#延时/协程出现问题

xyhw6mcr  于 2023-02-09  发布在  C#
关注(0)|答案(3)|浏览(349)

我试图写一个脚本,当你按下一个按钮,使它示例化一个随机稀有卡,但我需要删除卡后,一个小的延迟,虽然我不认为延迟是工作,因为卡似乎立即消失
代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardMasterScript : MonoBehaviour
{
    public GameObject[] CardArray = new GameObject[8];
    public float RollNumber;

    public void RollCard()
    {
        RollNumber = Random.Range(0.1f, 100);

        Debug.Log("Number Rolled: " + RollNumber);

        if(RollNumber <= 47.9)
        {
            var card = Instantiate(CardArray[0]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if(RollNumber > 47.9 && RollNumber <= 77.9)
        {
            var card = Instantiate(CardArray[1]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if( RollNumber > 77.9 && RollNumber <= 87.9)
        {
            var card = Instantiate(CardArray[2]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if(RollNumber > 87.9 && RollNumber <= 94.9)
        {
            var card = Instantiate(CardArray[3]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if(RollNumber > 94.9 && RollNumber <= 97.9)
        {
            var card = Instantiate(CardArray[4]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if(RollNumber > 97.9 && RollNumber <= 99.4)
        {
            var card = Instantiate(CardArray[5]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if(RollNumber > 99.4 && RollNumber <= 99.9)
        {
            var card = Instantiate(CardArray[6]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        } else if (RollNumber > 99.9 && RollNumber <= 100)
        {
            var card = Instantiate(CardArray[7]);
            StartCoroutine(TimeDelay());
            Destroy(card);
        }
    }
    
    public IEnumerator TimeDelay()
    {
        yield return new WaitForSeconds(0.5f);
    }
}
  • 请帮助我解决这个问题,如果你知道如何,并提前感谢你!

一开始我以为是示例化的问题,它根本就没有示例化卡片,但是现在我不太确定是什么问题。

wfypjpf4

wfypjpf41#

必须在等待时间之后调用destroy,就像这样

public IEnumerator TimeDelay(){    
    yield return new WaitForSeconds(0.5f);
    Destroy(obj);
}

和其他提示:销毁方法包含延迟参数

Destroy(obj,0.5f);

https://docs.unity3d.com/ScriptReference/Object.Destroy.html

6uxekuva

6uxekuva2#

在Quick方法中,只需在Destroy方法的第二个参数中输入延迟值。

Destroy(card, .5f); // Delay Time

第二种方法是在Coroutine内部编写函数本身,因为延迟时间不是在协程外部执行的。

private IEnumerator DelayRun(GameObject gameObject)
{
    yield return new WaitForSeconds(.5f);
    
    Destroy(gameObject);
}

第三种方法是用延迟时间指定动作,这允许您在延迟之后执行任何命令,只要确保使用System库即可。

using System;

...

void Start()
{
    StartCoroutine(DelayRun(.5f, () => Destroy(card)));
}

private IEnumerator DelayRun(float delay, Action action)
{
    yield return new WaitForSeconds(delay);
    
    action.Invoke();
}
laik7k3q

laik7k3q3#

销毁是独立于协程发生的,当你启动协程时,你把程序“分裂”成两个分支,现在你的第一个分支只是打开闹钟半秒钟,而你的另一个分支删除你的对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardMasterScript : MonoBehaviour
{
    public GameObject[] CardArray = new GameObject[8];
    public float RollNumber;

    public void RollCard()
    {
        RollNumber = Random.Range(0.1f, 100);

        Debug.Log("Number Rolled: " + RollNumber);

        if(RollNumber <= 47.9)
        {
            var card = Instantiate(CardArray[0]);
            StartCoroutine(TimeDelay());
        } else if(RollNumber > 47.9 && RollNumber <= 77.9)
        {
            var card = Instantiate(CardArray[1]);
            StartCoroutine(TimeDelay());
        } else if( RollNumber > 77.9 && RollNumber <= 87.9)
        {
            var card = Instantiate(CardArray[2]);
            StartCoroutine(TimeDelay());
        } else if(RollNumber > 87.9 && RollNumber <= 94.9)
        {
            var card = Instantiate(CardArray[3]);
            StartCoroutine(TimeDelay());
        } else if(RollNumber > 94.9 && RollNumber <= 97.9)
        {
            var card = Instantiate(CardArray[4]);
            StartCoroutine(TimeDelay());
        } else if(RollNumber > 97.9 && RollNumber <= 99.4)
        {
            var card = Instantiate(CardArray[5]);
            StartCoroutine(TimeDelay());
        } else if(RollNumber > 99.4 && RollNumber <= 99.9)
        {
            var card = Instantiate(CardArray[6]);
            StartCoroutine(TimeDelay());
        } else if (RollNumber > 99.9 && RollNumber <= 100)
        {
            var card = Instantiate(CardArray[7]);
            StartCoroutine(TimeDelay());
        }
    }
    
    public IEnumerator TimeDelay()
    {
        yield return new WaitForSeconds(0.5f);
        Destroy(card);
    }
}

在您的情况下,实际上不需要使用协程,因为Destroy方法有一个内置延迟参数。
简化后的代码可能如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardMasterScript : MonoBehaviour
{
    public GameObject[] CardArray = new GameObject[8];
    public float RollNumber;

    public void RollCard()
    {
        RollNumber = Random.Range(0.1f, 100);

        Debug.Log("Number Rolled: " + RollNumber);
        GameObject card = null;
        if(RollNumber <= 47.9)
        {
            card = Instantiate(CardArray[0]);
        } else if(RollNumber <= 77.9)
        {
            card = Instantiate(CardArray[1]);
        } else if(RollNumber <= 87.9)
        {
            card = Instantiate(CardArray[2]);
        } else if(RollNumber <= 94.9)
        {
            card = Instantiate(CardArray[3]);
        } else if(RollNumber <= 97.9)
        {
            card = Instantiate(CardArray[4]);
        } else if(RollNumber <= 99.4)
        {
            card = Instantiate(CardArray[5]);
        } else if(RollNumber <= 99.9)
        {
            card = Instantiate(CardArray[6]);
        } else
        {
            card = Instantiate(CardArray[7]);
        }
        Destroy(card, 0.5f);
    }
}

相关问题