【问题标题】:It stops for a while when button is clicked单击按钮时会停止一段时间
【发布时间】:2016-07-30 11:46:43
【问题描述】:
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 : 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;

}

}

当我单击一个按钮时,程序会停止一段时间然后开始工作......我认为这是因为线程或其他原因...... 请分享你的意见.. .当我点击一个按钮时,程序会停止一段时间然后工作......我认为这是因为线程或什么...... 请分享您的意见..

【问题讨论】:

  • 我认为您确实需要发布minimal reproducible example。现在您缺少代码,因此我们很难诊断。
  • 您说程序“暂时”停止。 “一会儿”到底有多长?
  • 它将乳液擦在皮肤上,否则它会再次得到软管
  • @Enigmativity 我添加了按钮的代码。
  • @ChrisF 它会停止大约 1 分钟......然后再次工作。

标签: c# multithreading unity3d coroutine


【解决方案1】:

停止使用:

Thread.Sleep(timeToWait);

这会停止整个线程,在这种情况下,Unity 会完全停止运行。

既然您仍然使用例程,请改用它:

yield return new WaitForSeconds(timeToWait);

并更改此行:

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

到这里:

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

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2013-02-23
    • 2015-04-25
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多