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

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

我是一个团结和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 = "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.

更多推荐

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

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

发布评论

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

>www.elefans.com

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