Repository navigation
Expand file tree
/
Copy pathUiLog.cs
More file actions
254 lines (214 loc) · 6.89 KB
/
Copy pathUiLog.cs
File metadata and controls
254 lines (214 loc) · 6.89 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using static UnityEngine.Application;
public class UiLog : MonoBehaviour
{
private static StringBuilder log = new StringBuilder();
private static GameObject instance;
public static GUIStyle style { get; set; }
public static int MaxLogs { get => _maxLogs; set { _maxLogs = value < 1 ? 1 : value; } }
private static int _maxLogs = 100;
private static List<LogRegister> logRegister = new List<LogRegister>();
public int fontSize = 40;
public bool defaultLog = true;
public bool warningLog = true;
public bool errorLog = true;
public Color defaultColor = Color.black;
public Color warningColor = Color.blue;
public Color errorColor = Color.red;
public static int _fontSize = 40;
public static bool _defaultLog;
public static bool _warningLog;
public static bool _errorLog;
public static Color _defaultColor = Color.black;
public static Color _warningColor = Color.blue;
public static Color _errorColor = Color.red;
private struct LogRegister
{
public string condition;
public string stackTrace;
public LogType type;
public LogRegister(string condition, string stackTrace, LogType type)
{
this.condition = condition;
this.stackTrace = stackTrace;
this.type = type;
}
}
public static void Display(bool show)
{
if(show)
{
if (!instance)
{
instance = new GameObject("UI-logger", typeof(UiLog));
instance.hideFlags = HideFlags.HideInHierarchy;
}
Application.logMessageReceived -= LogReceiver;
Application.logMessageReceived += LogReceiver;
}
else
{
Application.logMessageReceived -= LogReceiver;
if (instance)
{
Destroy(instance);
instance = null;
}
}
}
private static void LogReceiver(string condition, string stackTrace, LogType type)
{
if (!IsTypeEnabled(type))
return;
logRegister.Add(new LogRegister(condition, stackTrace, type));
while (logRegister.Count > MaxLogs)
{
logRegister.RemoveAt(0);
}
Repaint();
}
private static void Repaint()
{
log.Clear();
string defaultC = ToRGBHex(_defaultColor);
string warning = ToRGBHex(_warningColor);
string error = ToRGBHex(_errorColor);
for (int i = 0; i < logRegister.Count; i++)
{
string color = string.Empty;
switch (logRegister[i].type)
{
case LogType.Error:
color = error;
break;
case LogType.Assert:
color = error;
break;
case LogType.Warning:
color = warning;
break;
case LogType.Log:
color = defaultC;
break;
case LogType.Exception:
color = error;
break;
}
string thisLog = string.Empty;
if (logRegister[i].type == LogType.Log)
{
log.Append("<color=");
log.Append(color);
log.Append(">");
log.Append(logRegister[i].condition);
log.Append("</color>");
log.Append("\n");
}
else
{
log.Append("<color=");
log.Append(color);
log.Append(">");
log.Append(logRegister[i].condition);
log.Append("\n");
log.Append(logRegister[i].stackTrace);
log.Append("</color>");
log.Append("\n");
}
}
}
private static bool IsTypeEnabled(LogType type)
{
bool enabled = false;
switch (type)
{
case LogType.Error:
enabled = _errorLog;
break;
case LogType.Assert:
enabled = _errorLog;
break;
case LogType.Warning:
enabled = _warningLog;
break;
case LogType.Log:
enabled = _defaultLog;
break;
case LogType.Exception:
enabled = _errorLog;
break;
}
return enabled;
}
public static void Clear()
{
log.Clear();
logRegister.Clear();
}
public static string ToRGBHex(Color c)
{
return string.Format("#{0:X2}{1:X2}{2:X2}", ToByte(c.r), ToByte(c.g), ToByte(c.b));
}
private static byte ToByte(float f)
{
f = Mathf.Clamp01(f);
return (byte)(f * 255);
}
private void Start()
{
if (!instance)
{
instance = gameObject;
Display(true);
//set the colors and the logs of the instance to the static values so it can persist throgh scenes
_fontSize = fontSize;
_defaultColor = defaultColor;
_warningColor = warningColor;
_errorColor = errorColor;
_defaultLog = defaultLog;
_warningLog = warningLog;
_errorLog = errorLog;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
Display(true);
if (Input.GetKeyDown(KeyCode.Alpha1))
Debug.Log("Unity - Log");
if (Input.GetKeyDown(KeyCode.Alpha2))
Debug.LogError("Unity - Error");
if (Input.GetKeyDown(KeyCode.Alpha3))
Debug.LogWarning("Unity - Warning");
if (Input.GetKeyDown(KeyCode.Alpha4))
print(ToRGBHex(_defaultColor));
}
private void OnGUI()
{
GUI.Label(
new Rect(0, 0, Screen.width, Screen.height),
log.ToString(),
style != null ? style : new GUIStyle()
{
fontSize = _fontSize * Screen.height / Screen.height,
padding = new RectOffset(10,10,10,10),
alignment = TextAnchor.LowerLeft,
wordWrap = true,
richText = true,
});
}
private void OnValidate()
{
_fontSize = fontSize;
_defaultColor = defaultColor;
_warningColor = warningColor;
_errorColor = errorColor;
_defaultLog = defaultLog;
_warningLog = warningLog;
_errorLog = errorLog;
Repaint();
}
}