-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathThreadManager.cs
61 lines (53 loc) · 1.62 KB
/
ThreadManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright (C) Luaek - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Maxi Levi <maxilevi@live.com>, November 2017
*/
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThreadManager : MonoBehaviour {
private static List<KeyValuePair<Action, Action>> Functions = new List<KeyValuePair<Action, Action>>();
public static bool isPlaying = true;
/// <summary>
/// Executes the give method on the main thread after a frame has passed.
/// </summary>
public static void ExecuteOnMainThread(Action func)
{
lock(Functions){
Functions.Add( new KeyValuePair<Action, Action>(func, () => NullCallBack()) );
}
}
public static void ExecuteOnMainThread(Action func, Action Callback)
{
lock(Functions){
Functions.Add( new KeyValuePair<Action, Action>(func, Callback));
}
}
/// <summary>
/// Custom sleep method
/// </summary>
public static void Sleep(int Milliseconds){
long start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
while(true){
long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
if (now - start >= Milliseconds)
break;
}
}
private static void NullCallBack(){}
void Update()
{
isPlaying = Application.isPlaying;
lock(Functions){
for(int i = Functions.Count-1; i > -1; i--)
{
Functions[i].Key();
Functions[i].Value();
Functions.RemoveAt(i);
}
}
}
}