单击按钮时它会停止一段时间

It stops for a while when button is clicked

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");
        }
    }
}

public class gm:MonoBehaviour {

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);