C# 协同程序未在OnResponseGot内部启动
我有一个协同程序,它应该获取一个音频剪辑,然后启动另一个协同程序来播放该剪辑,并等待它完成,以便以后可以播放另一个剪辑。问题是-程序不会前进到我获取音频剪辑的协同程序C# 协同程序未在OnResponseGot内部启动,c#,unity3d,coroutine,C#,Unity3d,Coroutine,我有一个协同程序,它应该获取一个音频剪辑,然后启动另一个协同程序来播放该剪辑,并等待它完成,以便以后可以播放另一个剪辑。问题是-程序不会前进到我获取音频剪辑的协同程序 private void RetrieveFromDatabase(int index) { Debug.Log($"{index} started..."); FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + Tea
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));
}
}
}
private void RetrieveFromDatabase(int索引)
{
Log($“{index}已启动…”);
FirebaseDatabase.DefaultInstance.GetReference(“/Teams/”+TeamsSelection.teamSelected+“/”).Child(index.ToString()).Child(“userPosition”).GetValueAsync()
.ContinueWith(任务=>
{
if(task.IsFaulted)
{
//处理错误
}
else if(任务已完成)
{
DataSnapshot快照=task.Result;
OnResponseGot(快照、索引);
返回;
}
});
}
私有void OnResponseGot(DataSnapshot DataSnapshot,int索引)
{
if(dataSnapshot==null | | int.Parse(dataSnapshot.Value.ToString())<10)
{
Log(“再次尝试检索”+索引);
检索数据库(索引);
}
其他的
{
int userPos=int.Parse(dataSnapshot.Value.ToString());
Log(“检索到的userPos”+userPos);
start例程(FetchWithUWR(userPos,index));
//FetchWithResourceLoad(用户POS、索引);
}
}
IEnumerator FetchWithUWR(int-userPos,int-index)
{
Log(“启动音频文件的uwr”);
使用(UnityWebRequest uwr=UnityWebRequestMultimedia.GetAudioClip($“C:/Users/Domas/Desktop/Projects/Wild_-West/Assets/Resources/Audio/WAV_-ENG/D{userPos}a”,AudioType.WAV))
{
产生返回uwr.SendWebRequest();
Log(“生成了一个文件!”);
if(uwr.isNetworkError)
{
调试日志(uwr.error);
}
其他的
{
AudioClip clip=DownloadHandlerAudioClip.GetContent(uwr);
Log(“启动协同程序”+索引);
Start例程(播放音频剪辑和StartRetrievingFromDatabase(索引、剪辑));
}
}
}
我甚至不知道从哪里开始查找问题,因为我没有收到任何错误。程序只是继续运行,但没有协同程序。如果您尝试在其他线程中启动协同程序,则可能会出现此问题。在这种情况下,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);
}
}
private bool requestDone;
私有数据快照数据快照;
私有整数索引;
私有void RetrieveFromDatabase(int索引)
{
Log($“{index}已启动…”);
requestDone=false;
start例程(waitforresponse());
FirebaseDatabase.DefaultInstance.GetReference(“/Teams/”+TeamsSelection.teamSelected+“/”).Child(index.ToString()).Child(“userPosition”).GetValueAsync()
.ContinueWith(任务=>
{
if(task.IsFaulted)
{
//处理错误
}
else if(任务已完成)
{
DataSnapshot快照=task.Result;
这个指数=指数;
thos.dataSnapshot=快照;
}
requestDone=true;
});
}
私有IEnumerator waitForResponse()
{
而(!requestDone)返回null;
if(dataSnapshot==null | | int.Parse(dataSnapshot.Value.ToString())<10)
{
Log(“再次尝试检索”+索引);
检索数据库(索引);
}
其他的
{
int userPos=int.Parse(dataSnapshot.Value.ToString());
Log(“检索到的userPos”+userPos);
start例程(FetchWithUWR(userPos,index));
//FetchWithResourceLoad(用户POS、索引);
}
}
Unity控制台上是否有任何错误
?没有源于此脚本的错误,有一个错误来自FirebaseMonoBehavior,但我怀疑它与此问题有关。但我会调查。错误可能会取消脚本执行。特别是当错误在同一帧中时,您不能信任错误之后出现的任何行为。另一种方法是创建断点并检查代码停止执行的位置。您能提供方法流吗?哪个方法首先调用?FirebaseDatabase方法在Unity主线程上运行?您能告诉我们您收到的错误消息吗?