如何在其他协同开始之前先完成协程

嗨我是团结的新手和c#..

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

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

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的另一个文件中,我有一个开始屏幕的协程..

  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服务器滞后..

那么我怎么能先得到版本协程完成,然后开始屏幕就可以了…

两种不同的方法:

只有在第一个协程完成执行时才可以添加组件脚本(beginingscreen.cs)。 从而确保其他协同程序不会过早启动。

 IEnumerator GetVersion() { // ... gameObject.AddComponent(); } 

您可以在beginingscreen.cs中将Start方法设置为协程,然后调用GetVersion并等待其完成(GetVersion需要公开显示):

 IEnumerator Start() { var getVersion = gameObject.GetComponent(); if (getVersion != null) { yield return StartCoroutine(getVersion.GetVersion()); } if(!isExit) yield return StartCoroutine (BeginningAnimation()); else yield return StartCoroutine (EndAnimation()); } 

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