Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - how to make a coroutine finish first before other coroutine start

Hi im a newbie in unity and c#..

I have two script file in the same scene,

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;
     }
 }

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 ();
 }

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two different approaches:

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>();
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...