当点击按钮时,它会停止一段时间

本文关键字:一段时间 按钮 | 更新日期: 2023-09-27 17:59:21

public class green : MonoBehaviour
{
    private AudioSource source;
    public AudioClip sound;
    static int result = 0;
    // Use this for initialization
    void Start()
    {
        StartCoroutine("RoutineCheckInputAfter3Minutes");
        Debug.Log("a");
    }
    IEnumerator RoutineCheckInputAfter3Minutes()
    {
        System.Random ran = new System.Random();
        int timeToWait = ran.Next(1, 50) * 1000;
        Thread.Sleep(timeToWait);
        source = this.gameObject.AddComponent<AudioSource>();
        source.clip = sound;
        source.loop = true;
        source.Play();
        System.Random r = new System.Random();
        result = r.Next(1, 4);
        Debug.Log("d");
        yield return new WaitForSeconds(3f * 60f);
        gm.life -= 1;
        Debug.Log(gm.life + "값");
        source.Stop();
        Debug.Log("z");
        if (gm.life >= 0)
        {
            StartCoroutine("RoutineCheckInputAfter3Minutes");
        }
    }
    // Update is called once per frame
    public void Update()
    {
        if (result == 1 && gm.checkeat == true)
        {
            Debug.Log("e");
            gm.life += 1;
            Debug.Log("j");
            Debug.Log(gm.life + "값");
            source.Stop();
            gm.checkeat = false;
            StopCoroutine("RoutineCheckInputAfter3Minutes");
            StartCoroutine("RoutineCheckInputAfter3Minutes");
        }
        if (result == 2 && gm.checkshit == true)
        {
            Debug.Log("f");
            gm.life += 1;
            Debug.Log("o");
            Debug.Log(gm.life + "값");
            source.Stop();
            gm.checkshit = false;
            StopCoroutine("RoutineCheckInputAfter3Minutes");
            StartCoroutine("RoutineCheckInputAfter3Minutes");
        }
        else if (result == 3 && gm.checksleep == true)
        {
            Debug.Log("g");
            gm.life += 1;
            Debug.Log(gm.life);
            Debug.Log(gm.life + "값");
            source.Stop();
            gm.checksleep = false;
            StopCoroutine("RoutineCheckInputAfter3Minutes");
            StartCoroutine("RoutineCheckInputAfter3Minutes");
        }
    }
}

公共类gm:MonoBehavior{

static public int life = 0;
static public bool checkeat = false;
static public bool checkshit = false;
static public bool checksleep = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void eating(string eat)
{
    Debug.Log(life + "값");
    checkeat = true;
}
public void shitting(string shit)
{
    Debug.Log(life + "값");
    checkshit = true;
}
public void sleeping(string sleep)
{
    Debug.Log(life + "값");
    checksleep = true;
}

}

当我点击一个按钮时,程序会停止一段时间,然后工作。。。我想是因为线什么的。。。请分享你的意见。。.当我点击一个按钮时,程序会停止一段时间,然后工作。。。我想是因为线什么的。。。请分享你的意见。。

当点击按钮时,它会停止一段时间

停止使用:

Thread.Sleep(timeToWait);

这会暂停整个线程,在本例中,Unity完全无法运行。

既然你无论如何都在使用例程,那就用这个吧:

yield return new WaitForSeconds(timeToWait);

并更改这一行:

int timeToWait = ran.Next(1, 50) * 1000;

对此:

int timeToWait = ran.Next(1, 50);