forked from QianMo/Unity-Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventQueue.cs
More file actions
141 lines (105 loc) · 3.83 KB
/
Copy pathEventQueue.cs
File metadata and controls
141 lines (105 loc) · 3.83 KB
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
//-------------------------------------------------------------------------------------
// EventQueue.cs
// Reference :https://github.com/GandhiGames/message_queue
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace EventQueuePatternExample
{
public class EventQueue : MonoBehaviour
{
#region 事件队列相关
/// <summary>
/// 队列中待投递的事件队列
/// </summary>
private List<IMessageEvent> pendingEventQueueList = new List<IMessageEvent>();
/// <summary>
/// 事件投递函数
/// </summary>
void OnAddEventToQueue(IMessageEvent e)
{
pendingEventQueueList.Add(e);
if (logToConsole)
{
Debug.Log("Message Recieved [" + System.DateTime.Now + "]: " + e.message.ToString());
}
}
void Update()
{
for (int i = pendingEventQueueList.Count - 1; i >= 0; i--)
{
if (Time.time > pendingEventQueueList[i].displayTime)
pendingEventQueueList.RemoveAt(i);
}
}
void Start()
{
if (pendingEventQueueList.Count > 0)
{
pendingEventQueueList.Clear();
}
EventQueueManager.Instance.AddListener<MessageEvent>(OnAddEventToQueue);
}
void OnDisable()
{
EventQueueManager.Instance.RemoveListener<MessageEvent>(OnAddEventToQueue);
}
#endregion
#region UI显示相关
void OnEnable()
{
SetUIStyle();
}
void SetUIStyle()
{
LOW_PRIORTY.normal.textColor = lowPriorityColour;
LOW_PRIORTY.fontStyle = lowPriorityStyle;
NORMAL_PRIORITY.normal.textColor = normalPriorityColour;
NORMAL_PRIORITY.fontStyle = normalPriorityStyle;
HIGH_PRIORITY.normal.textColor = highPriorityColour;
HIGH_PRIORITY.fontStyle = highPriorityStyle;
}
public bool logToConsole = true;
public bool prependDateTime = false;
[Header("Message Colours")]
public Color
highPriorityColour = Color.red;
public Color normalPriorityColour = Color.black;
public Color lowPriorityColour = Color.white;
[Header("Message Font Style")]
public FontStyle
highPriorityStyle = FontStyle.Bold;
public FontStyle normalPriorityStyle = FontStyle.Normal;
public FontStyle lowPriorityStyle = FontStyle.Normal;
[Header("Message Location")]
public Vector2
queueLocation = new Vector2(25, 25);
public Vector2 messageSize = new Vector2(200, 15);
private static readonly GUIStyle LOW_PRIORTY = new GUIStyle(), NORMAL_PRIORITY = new GUIStyle(), HIGH_PRIORITY = new GUIStyle();
void OnGUI()
{
float yPos = queueLocation.y;
foreach (var m in pendingEventQueueList)
{
GUIStyle style = GetMessageStyle(m);
string message = (prependDateTime) ? "[" + m.timeRaised + "]: " + m.message.ToString() : m.message.ToString();
GUI.Label(new Rect(queueLocation.x, yPos, messageSize.x, messageSize.y), message, style);
yPos += messageSize.y;
}
}
GUIStyle GetMessageStyle(IMessageEvent e)
{
switch (e.priority)
{
case MessagePriority.Low:
return LOW_PRIORTY;
case MessagePriority.Medium:
return NORMAL_PRIORITY;
default:
return HIGH_PRIORITY;
}
}
#endregion
}
}