Created
March 31, 2016 12:00
-
-
Save paraself/8ea04dc03d5a6e9302bc1938a67c1e8b to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System; | |
public class QueuedTask : Singleton<QueuedTask> { | |
const int MaxProcessCount = 2; | |
public class Task { | |
public Task (Action action) { | |
this.action = action; | |
this.isDone = false; | |
} | |
public Action action; | |
public bool isDone; | |
} | |
public static Queue<Task> taskQueue = new Queue<Task> (100); | |
public static void AddTask ( Task task ) { | |
task.isDone = false; | |
taskQueue.Enqueue(task); | |
} | |
void Update () { | |
for (int i=0;i<MaxProcessCount;i++) { | |
if (taskQueue.Count==0) return; | |
Task t = taskQueue.Peek(); | |
if (t!=null) { | |
t.action(); | |
t.isDone = true; | |
taskQueue.Dequeue(); | |
} | |
} | |
} | |
} | |
//// Usage Example | |
//public class TestCCCC : MonoBehaviour { | |
// | |
// QueuedTask.Task task; | |
// | |
// void Start() { | |
// task = new QueuedTask.Task (sb); | |
// } | |
// | |
// void sb () { | |
// //some heavy work | |
// } | |
// | |
// void Update() { | |
// QueuedTask.AddTask(task); | |
// if (task.isDone == false ) return; | |
// else { | |
// //continue the logic | |
// //... | |
// } | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment