Skip to content

Instantly share code, notes, and snippets.

@kimsama
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save kimsama/a2b438ea291e2d958b0b to your computer and use it in GitHub Desktop.

Select an option

Save kimsama/a2b438ea291e2d958b0b to your computer and use it in GitHub Desktop.
A sample which runs coroutine method within Update()
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