Last active
August 29, 2015 14:07
-
-
Save kimsama/a2b438ea291e2d958b0b to your computer and use it in GitHub Desktop.
A sample which runs coroutine method within Update()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| /// | |
| /// Add any IEnumerator function to the queue which to be executed in the Update() | |
| /// | |
| public class IEnumeratorSample : MonoBehaviour | |
| { | |
| Queue<IEnumerator> queue = new Queue<IEnumerator>(); | |
| IEnumerator action = null; | |
| void Start () | |
| { | |
| queue.Enqueue(Process()); | |
| } | |
| bool keydown = false; | |
| void Update () | |
| { | |
| if (Input.GetKeyDown(KeyCode.Space)) | |
| { | |
| keydown = true; | |
| } | |
| if (Input.GetKeyUp(KeyCode.Space)) | |
| keydown = false; | |
| FireAction(queue); | |
| } | |
| void FireAction(Queue<IEnumerator> t) | |
| { | |
| if (action == null || action.MoveNext() == false) | |
| { | |
| if (queue.Count == 0) | |
| action = null; | |
| else | |
| { | |
| action = t.Dequeue(); | |
| if (action.MoveNext() == false) | |
| action = null; | |
| } | |
| } | |
| } | |
| IEnumerator Process() | |
| { | |
| while(!keydown) | |
| yield return 0; | |
| keydown = false; | |
| Debug.Log("first"); | |
| while (!keydown) | |
| yield return 0; | |
| keydown = false; | |
| Debug.Log("second"); | |
| while (!keydown) | |
| yield return 0; | |
| keydown = false; | |
| Debug.Log("third"); | |
| while (!keydown) | |
| yield return 0; | |
| keydown = false; | |
| Debug.Log("fourth"); | |
| yield return 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment