-
Notifications
You must be signed in to change notification settings - Fork 275
/
UnityTickedQueue.cs
142 lines (124 loc) · 3.27 KB
/
UnityTickedQueue.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using UnityEngine;
using TickedPriorityQueue;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// A helper class to create Unity updated Ticked Priority Queues.
/// Instance will return the default instance, and CreateInstance will return a named instance.
/// MaxProcessedPerUpdate will get or set the max number of items to be processed per Unity update.
/// </summary>
public class UnityTickedQueue : MonoBehaviour
{
#region Instances
private static Dictionary<string, UnityTickedQueue> _instances;
private static UnityTickedQueue _instance;
/// <summary>
/// Retrieves a default static instance for ease of use.
/// The name of the created GameObject will be Ticked Queue.
/// </summary>
public static UnityTickedQueue Instance
{
get
{
if (_instance == null)
{
_instance = CreateInstance(null);
}
return _instance;
}
}
/// <summary>
/// Retrieves a named custom instance.
/// The queue's GameObject will be named Ticked Queue - name.
/// If the name already exists, it will retrieve the older named instance.
/// </summary>
/// <param name="name">
/// A <see cref="System.String"/> giving the name of the queue.
/// </param>
/// <returns>
/// A <see cref="UnityTickedQueue"/> instance.
/// </returns>
public static UnityTickedQueue GetInstance(string name)
{
if (string.IsNullOrEmpty(name)) return Instance;
name = name.ToLower();
UnityTickedQueue queue = null;
if (_instances == null)
_instances = new Dictionary<string, UnityTickedQueue>();
else
{
_instances.TryGetValue(name, out queue);
}
if (queue == null)
{
queue = CreateInstance(name);
_instances[name] = queue;
}
return queue;
}
private static UnityTickedQueue CreateInstance(string name)
{
if (string.IsNullOrEmpty(name)) name = "Ticked Queue";
else name = "Ticked Queue - " + name;
GameObject go = new GameObject(name);
return go.AddComponent<UnityTickedQueue>();
}
#endregion
private TickedQueue _queue = new TickedQueue();
public bool IsPaused {
get {
return _queue.IsPaused;
}
set {
_queue.IsPaused = value;
enabled = !value;
}
}
public TickedQueue Queue {
get {
return this._queue;
}
}
void OnEnable()
{
_queue.TickExceptionHandler = LogException;
}
/// <summary>
/// Adds an ITicked reference to the queue.
/// </summary>
/// <param name="ticked">
/// A <see cref="ITicked"/> reference, which will be ticked periodically based on its properties.
/// </param>
public void Add(ITicked ticked)
{
_queue.Add(ticked);
}
/// <summary>
/// Removes an ITicked reference from the queue.
/// </summary>
/// <param name="ticked">
/// A <see cref="ITicked"/> reference, which will be ticked periodically based on its properties.
/// </param>
/// <returns>True if the item was successfully removed, false if otherwise</returns>
public bool Remove(ITicked ticked)
{
return _queue.Remove(ticked);
}
/// <summary>
/// Sets the maximum number of items to be processed every time Unity updates.
/// </summary>
public int MaxProcessedPerUpdate
{
get { return _queue.MaxProcessedPerUpdate; }
set { _queue.MaxProcessedPerUpdate = value; }
}
private void Update()
{
_queue.Update();
}
void LogException(Exception e, ITicked ticked)
{
Debug.LogException(e, this);
}
}