首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >unity中的异步协程处理

unity中的异步协程处理
EN

Stack Overflow用户
提问于 2018-10-24 21:02:49
回答 1查看 1.5K关注 0票数 0

我已经使用协程执行了一个后端服务调用,以检索我的category.cs文件中的球员类别:

代码语言:javascript
复制
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 (在使用该类的实例后端调用的不同类中)

代码语言:javascript
复制
public IEnumerator GetPlayerProfile(System.Action<Profile> callback){
        yield return GetPlayerProfile (callback, false);
    }

问题:

由于我使用的是外部服务呼叫,有时球员资料上传会有延迟。

在执行其余代码行之前,我需要确保startcoroutine已使用result完成。

在从互联网上搜索后,我尝试创建以下类,它可以确保在执行其余行之前完成couroutine调用:

代码语言:javascript
复制
    {
    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)这样的事情吗?

谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2018-10-26 02:07:12

尝试使用WaitUntil。

https://docs.unity3d.com/ScriptReference/WaitUntil.html

如下所示:

代码语言:javascript
复制
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();
}

然后..。

代码语言:javascript
复制
StartCoroutine(GetProfile);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52969751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档