如何在其他协程开始之前先完成协程 [英] how to make a coroutine finish first before other coroutine start

查看:113
本文介绍了如何在其他协程开始之前先完成协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团结和c#的新手.

Hi im a newbie in unity and c#..

我在同一场景中有两个脚本文件,

I have two script file in the same scene,

versionchecker.cs文件中的1个协程从我的Web服务器获取版本号数据

1 coroutine in file versionchecker.cs to get a version number data from my web server

public string versionURL = "http://localhost/check.php";

 IEnumerator GetVersion()
 {
     WWW vs_get = new WWW(versionURL);
     yield return vs_get;

     if (vs_get.error != null)
     {
         connection = 1;
     }
     else
     {
         currentVersion = vs_get.text;
         bundleVersion = PlayerSettings.bundleVersion;
         connection = 0;
     }
 }

但是在beginingscreen.cs中的另一个文件中,我有一个用于开始屏幕的协程..

But in another file in beginingscreen.cs, i have a coroutine for a begining screen..

 void Start () {
     if(!isExit)
         StartCoroutine (BeginningAnimation ());
     else
         StartCoroutine (EndAnimation ());
 }

 IEnumerator BeginningAnimation()
 {
     fade.FadeIn (1.5f);
     yield return new WaitForSeconds (2);
     fade.FadeOut (1);
     yield return new WaitForSeconds (0.9f);
     Application.LoadLevel (LevelToLoad);
 }

 IEnumerator EndAnimation()
 {
     yield return new WaitForSeconds (0.5f);
     fade.FadeOut (1);
     yield return new WaitForSeconds (1);
     Application.Quit ();
 }

我将此脚本放置在游戏的同一场景中..但有时,开始屏幕的协程首先在获取版本的协程之前完成,因为获取版本需要与Web服务器的连接,并且有时Web服务器处于滞后状态.

this script i place it in the same scene of my game.. but sometimes the coroutine for begining screen finish first before the coroutine for get version because the get version need a connection to webserver, and sometime the web server is lagging..

所以我该如何先使获取版本的协程完成,然后才能开始显示屏幕.

so how can i make the get version coroutine finish first and after that begining screen can start..

推荐答案

两种不同的方法:

仅当第一个协程完成执行时,才可以添加组件脚本(beginingscreen.cs).从而确保其他协程不会太早开始.

You could just add the component script (beginingscreen.cs) only when the first coroutine finished executing. Thus ensuring that the other coroutines do not start too early.

IEnumerator GetVersion()
{
    // ...
    gameObject.AddComponent<BeginingScreen>();
}

您可以在beginingscreen.cs中将Start方法作为协程,然后调用GetVersion并等待其完成(GetVersion必须公开可见):

You can make the Start method a coroutine in beginingscreen.cs, then call GetVersion and wait for its completion (GetVersion needs to be publicly visible) :

IEnumerator Start()
{
    var getVersion = gameObject.GetComponent<VersionChecker>();
    if (getVersion != null)
    {
        yield return StartCoroutine(getVersion.GetVersion());
    }

    if(!isExit)
        yield return StartCoroutine (BeginningAnimation());
    else
        yield return StartCoroutine (EndAnimation());
}

在两种解决方案中,您都需要两个组件(脚本)以某种方式相互交互.或者,您可以创建处理此交互的第三个脚本.

In both solutions, you need the two components (scripts) to somehow interact with each other. Or you can create a third script that handles this interaction.

这篇关于如何在其他协程开始之前先完成协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆