【问题标题】:Coroutine not starting inside OnResponseGot协程未在 OnResponseGot 内部启动
【发布时间】:2020-01-20 09:56:24
【问题描述】:

我有一个协程应该获取一个音频剪辑,然后启动另一个协程来播放该剪辑并等待它完成,以便之后可以播放另一个剪辑。问题是 - 程序没有前进到我获取音频剪辑的协程。

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   OnResponseGot(snapshot, index);
                   return;
               }
           });
    }

    private void OnResponseGot(DataSnapshot dataSnapshot, int index)
    {
        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

    IEnumerator FetchWithUWR(int userPos, int index)
    {
        Debug.Log("Starting uwr for audioFiles");
        using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip($"C:/Users/Domas/Desktop/Projects/Wild_West/Assets/Resources/Audio/WAV_ENG/D{userPos}a", AudioType.WAV))
        {
            yield return uwr.SendWebRequest();
            Debug.Log("yielded a file!");
            if (uwr.isNetworkError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
                Debug.Log("Starting Coroutine " + index);
                StartCoroutine(PlayAudioClipAndStartRetrievingFromDatabase(index, clip));
            }
        }
    }

我什至不知道从哪里开始寻找问题,因为我没有收到任何错误,程序只是继续运行,但没有协程。

【问题讨论】:

  • UnityConsole有什么错误吗?
  • 没有来自这个脚本的错误,有一个来自 FirebaseMonobehaviour 的错误,但我怀疑它与这个问题有什么关系。但我会调查的。
  • 错误可以取消脚本执行。特别是如果错误在同一帧中,您不能相信错误之后的任何行为。另一种方法是设置断点并检查代码停止执行的位置。
  • 能否提供方法流程?哪个方法先调用? FirebaseDatabase 方法在 Unity 主线程上运行?
  • 您能告诉我们您收到的错误信息吗?

标签: c# unity3d coroutine


【解决方案1】:

如果您尝试在其他线程中启动协程,则会出现此问题。在这种情况下,Firebase 可以处理异常并使其保持沉默。尝试使用类似这样的东西在主线程中等待请求:

    private bool requestDone;
    private DataSnapshot dataSnapshot;
    private int index;

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        requestDone = false;
        StartCoroutine(WaitForResponce());

        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   this.index = index;
                   thos.dataSnapshot = snapshot;
               }
               requestDone = true;
           });
    }

    private IEnumerator WaitForResponce()
    {
        while (!requestDone) yield return null;

        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 2021-02-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2020-04-12
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多