如何在其他协程启动之前先完成一个协程

编程入门 行业动态 更新时间:2024-10-28 10:36:50
本文介绍了如何在其他协程启动之前先完成一个协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 unity 和 c# 的新手..

Hi im a newbie in unity and c#..

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

I have two script file in the same scene,

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

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

public string versionURL = "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 (); }

这个脚本我把它放在我的游戏的同一个场景中..但有时开始屏幕的协程在获取版本的协程之前先完成,因为获取版本需要连接到网络服务器,有时网络服务器滞后..

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

那么我怎样才能让 get 版本协程先完成,然后开始屏幕才能启动..

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.

更多推荐

如何在其他协程启动之前先完成一个协程

本文发布于:2023-11-23 10:43:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个协   前先   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!