我已经使用协程执行了一个后端服务调用,以检索我的category.cs文件中的球员类别:
public override void OnEnter(Page p)
{
backend = globalScriptObject.GetComponent<IBackendController>();
items.Clear ();
StartCoroutine (backend.GetPlayerProfile ( profile =>{
this.maxSelectableItems = Mathf.CeilToInt(profile.level/10+1);
if(this.maxSelectableItems == 7) maxSelectableItems = int.MaxValue;
DisableSelections();
}));GetPlayerProfile (在使用该类的实例后端调用的不同类中)
public IEnumerator GetPlayerProfile(System.Action<Profile> callback){
yield return GetPlayerProfile (callback, false);
}问题:
由于我使用的是外部服务呼叫,有时球员资料上传会有延迟。
在执行其余代码行之前,我需要确保startcoroutine已使用result完成。
在从互联网上搜索后,我尝试创建以下类,它可以确保在执行其余行之前完成couroutine调用:
{
StartCoroutine(FinishFirst(5.0f, DoLast));
}
IEnumerator FinishFirst(float waitTime, Action doLast) {
print("in FinishFirst");
yield return new WaitForSeconds(waitTime);
print("leave FinishFirst");
doLast();
}
void DoLast() {
print("do after everything is finished");
print("done");
}但如何在我的源代码中使用上述内容是我需要来自社区的建议。
另外,我能在GetPlayerProfile方法中做像yield waitForSec(Float)这样的事情吗?
谢谢!!
发布于 2018-10-26 02:07:12
尝试使用WaitUntil。
https://docs.unity3d.com/ScriptReference/WaitUntil.html
如下所示:
IEnumerator GetProfile(){
var profile = null;
yield GetPlayerProfile((p) => {profile = p});
yield WaitUntil(p != null);
this.maxSelectableItems = Mathf.CeilToInt(profile.level/10+1);
if(this.maxSelectableItems == 7) maxSelectableItems = int.MaxValue;
DisableSelections();
}然后..。
StartCoroutine(GetProfile);https://stackoverflow.com/questions/52969751
复制相似问题