【问题标题】:User null input check for some time and thread用户空输入检查一段时间和线程
【发布时间】:2016-07-30 04:46:38
【问题描述】:
public class green : MonoBehaviour
{
    private AudioSource source;
    public AudioClip sound;
    static int result = 0;

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

    public void Update()
    {
        if (result == 1 && gm.checka == true)
        {
            Debug.Log("e");
            StopCoroutine("RoutineCheckInputAfter3Minutes");
            gm.life += 1;
            source.Stop();
            gm.checka = false;
            Debug.Log("j");
        }
        if (result == 2 && gm.checkb == true)
        {
            Debug.Log("f");

            StopCoroutine("RoutineCheckInputAfter3Minutes");
            gm.life += 1;
            source.Stop();
            gm.checkb = false;
            Debug.Log("z");
        }
        else if (result == 3 && gm.checkc == true)
        {
            StopCoroutine("RoutineCheckInputAfter3Minutes");
            Debug.Log("g");
            gm.life += 1;
            source.Stop();
            gm.checkc = false;
            Debug.Log(gm.life);
        }
    }
}

有两个问题

  1. 如果用户在 3 分钟内没有按任何按钮,我想让音乐停止并且生命变量减少 -1。但是如果用户按下右键,life变量会增加+1。但是我不知道如何从用户那里得到空输入3分钟。

  2. 如果我在这个程序中使用while,它会关闭……直到生命值低于 0,我想重复播放不规则时间的音乐。

【问题讨论】:

  • result 是什么意思? gm 是什么?你如何决定那些checkacheckbcheckc?您应该给出一个干净的代码,并删除其中不必要的部分。
  • 我以为人们一下子就知道了;;;下次我会注意的;;;

标签: c# multithreading unity3d timer


【解决方案1】:

编辑:不要在 Unity 中使用线程

How Unity works

Alternative to Thread

Couroutine Example 1

Coroutine Example 2

* 简单来说,Thread.Sleep 挂在 Unity 上,Unity 暂时无法运行,这就是它看起来运行缓慢的原因。

您可以使用协程来解决这个问题。

void Start()
{
    StartCoroutine("RoutineCheckInputAfter3Minutes");
}

IEnumerator RoutineCheckInputAfter3Minutes()
{
    yield return new WaitForSeconds(3f*60f);
    gm.life -= 1;
}

void RightButtonClicked()
{
    gm.life += 1;

    StopCoroutine("RoutineCheckInputAfter3Minutes");
    StartCoroutine("RoutineCheckInputAfter3Minutes");
}

或者,如果其他代码部分有效,您可以将 a 函数转换为 协程

void Start()
{
    StartCoroutine(a());
}
public IEnumerator a()
{
    while (gm.life >= 0)
    {
        System.Random ran = new System.Random();
        int timeToWait = ran.Next(1, 50) * 1000;
        yield return new WaitForSeconds(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");
    }
}

这是基于您的代码的协程示例用法超出范围:

A pastebin link to code

【讨论】:

  • routinecheckinputafter3minutes 函数与右键单击函数同时工作?
  • 没有。它只是一个辅助函数。您可以随时调用它,例如单击按钮。
  • 谢谢!它运作良好......但它在“Thread.Sleep(timeToWait);
  • 很抱歉打扰你.. .
  • 不要使用线程。使用协程。阅读统一文档,并查看一些关于它的教程。不是太难。 2-3小时后,我可以发送一个例子。我现在没空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多