Skip to content

Instantly share code, notes, and snippets.

@RoyAwesome
Created August 15, 2014 20:24
Show Gist options
  • Save RoyAwesome/983dce62feb7f7ac8fdc to your computer and use it in GitHub Desktop.
Save RoyAwesome/983dce62feb7f7ac8fdc to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections.Generic;
using System;
public class Dispatcher : MonoBehaviour {
Queue<Action> JoinActions = new Queue<Action>();
public static Dispatcher Instance = null;
// Use this for initialization
void Start () {
Instance = this;
}
// Update is called once per frame
void Update ()
{
lock(JoinActions)
{
if(JoinActions.Count > 0)
{
do
{
Action a = JoinActions.Dequeue();
try
{
a();
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
} while (JoinActions.Count > 0);
}
}
}
public static void Dispatch(Action a)
{
lock(Instance.JoinActions)
{
Instance.JoinActions.Enqueue(a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment