Tag: coroutine

2048年的游戏开发者是如何让他们的瓷砖顺利移动的? 看下面的详细信息

我已经制作了2048游戏的完整副本,但是我通过传送移动了瓷砖(没有平滑移动的瓷砖,就像在原始游戏中一样) 我使用以下代码来移动瓷砖的smothness。 //GameManager script void MoveRight () { //some code .. AnimateTileMovement (newPosition); // newposition is the position to whihc the tiles is going to move //some code which i need to execute (ONLY AFTER MY COMPLETE MOVEMENT OF TILE) // BUT AS SOON AS TileMovement return its first null this code execute which is […]

在Coroutine中“yield return 0”和“yield return null”之间有什么区别?

我很新,对“ yield ”有点困惑。 但最后我理解使用WaitForSeconds它是如何工作的 但我看不出“ yield return 0 ”和“ yield return null ”之间的区别。 他们都在等待下一帧执行? 对不起,我的英语不好。 非常感谢你。

StartCoroutine被调用了很多次(C#Unity)

我正在Unity中创建一个弹出菜单选项。 现在我的问题是我在无效更新中做的协程被调用了很多次。 我的意思是在我的Unity控制台上,Debug.Logs正在递增。 它应该是正确的,因为它已经是协程。 有些人可以帮助我理解更多的协程并帮助我解决我的小问题。 这是我的代码: [SerializeField] GameObject Option; [SerializeField] Button btn,btn2; [SerializeField] GameObject open, close; [SerializeField] GameObject[] opt; bool startFinding = false; void Start() { Option.SetActive(false); Button popUp = btn.GetComponent(); Button popUp2 = btn2.GetComponent(); popUp.onClick.AddListener(PopUpOption); popUp2.onClick.AddListener(ClosePopUp); } void Update() { if (startFinding) { StartCoroutine(GameOptions()); } } IEnumerator GameOptions() { //Get All the tags opt […]

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

嗨我是团结的新手和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 ()); } […]

协同程序和while循环

我一直在沿着一条路径进行物体移动我从Navmesh Unity3d开始使用我正在使用coroutine,我用while循环控制它,因为我可以显示 public void DrawPath(NavMeshPath pathParameter, GameObject go) { Debug.Log(“path Parameter” + pathParameter.corners.Length); if (agent == null || agent.path == null) { Debug.Log(“Returning”); return; } line.material = matToApplyOnLineRenderer; line.SetWidth(1f, 1f); line.SetVertexCount(pathParameter.corners.Length); allPoints = new Vector3[pathParameter.corners.Length]; for (int i = 0; i 0) { if (index != path.corners.Length – 1) { start = allPoints[index]; index += […]

如何停止合作?

当两个协同例程运行时,如何停止第一个协同例程? GLOBALS.stableTime = 5; IEnumerator StableWaittingTime () { yield return new WaitForSeconds (1f); if (GLOBALS.stableTime == 0) { GameManager.instance.LevelFaildMethod (); } else { GameManager.instance.stableWaittingTime.text = GLOBALS.stableTime.ToString (); GLOBALS.stableTime–; StartCoroutine (“StableWaittingTime”); } }

协程是Unity3D中的新线程吗?

关于协同程序 (在Unity3D和其他地方)如何工作,我感到困惑和好奇。 coroutine是新线程吗? 他们说Unity的文档 : 协程是一个可以暂停执行(yield)直到给定的YieldInstruction完成的函数。 他们在这里有C#示例: using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Start() { print(“Starting ” + Time.time); StartCoroutine(WaitAndPrint(2.0F)); print(“Before WaitAndPrint Finishes ” + Time.time); } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print(“WaitAndPrint ” + Time.time); } } 我对这个例子有很多疑问: 在上面的例子中,哪一行是协程? 是的WaitAndPrint()是一个协程吗? 是WaitForSeconds()一个协程吗? 在这一行中: yield return new WaitForSeconds(waitTime); […]

异步/等待作为协同程序的替代品

我使用C#迭代器作为协同程序的替代品,它一直很好用。 我想切换到async / await,因为我认为语法更清晰,它给了我类型安全。 在这篇(过时的)博客文章中,Jon Skeet展示了实现它的可能方法 。 我选择采用稍微不同的方式(通过实现我自己的SynchronizationContext并使用Task.Yield )。 这很好用。 然后我意识到会有问题; 目前协程不必完成运行。 它可以在任何产生的点上优雅地停止。 我们可能有这样的代码: private IEnumerator Sleep(int milliseconds) { Stopwatch timer = Stopwatch.StartNew(); do { yield return null; } while (timer.ElapsedMilliseconds < milliseconds); } private IEnumerator CoroutineMain() { try { // Do something that runs over several frames yield return Coroutine.Sleep(5000); } finally { Log("Coroutine […]

在继续使用C#Unityfunction之前,请等待协程完成

我正在努力让一个单位在Unity2d中通过网格移动。 我让运动没有问题。 我希望函数MovePlayer等到协程完成后再继续,所以程序将等到玩家完成移动后再发出更多订单。 这是我的代码:public class Player:MonoBehaviour { public Vector3 position; private Vector3 targetPosition; private float speed; void Awake () { speed = 2.0f; position = gameObject.transform.position; targetPosition = position; GameManager.instance.AddPlayerToList(this); //Register this player with our instance of GameManager by adding it to a list of Player objects. } //Function that moves the player, takes a […]

Unity – 只需在协同程序完成后才能返回值

我正在进行团结游戏并遇到一个我无法解决的问题。 我通过标准的WWW对象连接到Web服务器并使用协程来执行POST请求。 代码本身可以工作,但我需要更新变量值并在协程完成后返回该变量,这是我无法做到的。 public int POST(string username, string passw) { WWWForm form = new WWWForm(); form.AddField(“usr”, username); form.AddField(“pass”, passw); WWW www = new WWW(url, form); StartCoroutine(WaitForRequest(www)); //problem is here ! return success_fail; } private IEnumerator WaitForRequest(WWW www) { yield return www; if (www.error == null) { if(www.text.Contains(“user exists”)) { success_fail = 2; } else { […]