【发布时间】: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));
}
}
}
我什至不知道从哪里开始寻找问题,因为我没有收到任何错误,程序只是继续运行,但没有协程。
【问题讨论】:
-
Unity
Console有什么错误吗? -
没有来自这个脚本的错误,有一个来自 FirebaseMonobehaviour 的错误,但我怀疑它与这个问题有什么关系。但我会调查的。
-
错误可以取消脚本执行。特别是如果错误在同一帧中,您不能相信错误之后的任何行为。另一种方法是设置断点并检查代码停止执行的位置。
-
能否提供方法流程?哪个方法先调用? FirebaseDatabase 方法在 Unity 主线程上运行?
-
您能告诉我们您收到的错误信息吗?