-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSceneViewNotification.cs
269 lines (226 loc) · 9.09 KB
/
SceneViewNotification.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
#if UNITY_EDITOR
public class SceneViewNotification : Editor
{
static List<Notification> notifications = new List<Notification>();
public enum NotificationType
{
Info,
Warning,
Error
}
private class Notification
{
public string text;
public DateTime dateTime;
public NotificationType type;
}
public static void Add(string text, NotificationType type)
{
Notification n = new Notification();
n.dateTime = DateTime.Now;
n.text = text;
n.type = type;
if (notifications.Count >= MaxItems) notifications.RemoveAt(notifications.Count - 1);
notifications.Insert(0, n);
}
public static void Clear()
{
notifications.Clear();
}
const float width = 300f;
const float height = 155f;
const int lineHeight = 20;
private static float linePos;
public static int MaxItems
{
get { return EditorPrefs.GetInt("SVN_MAX_ITEMS", 30); }
set { EditorPrefs.SetInt("SVN_MAX_ITEMS", value); }
}
public static float MaxLifetime
{
get { return EditorPrefs.GetFloat("SVN_LIFETIME", 3f); }
set { EditorPrefs.SetFloat("SVN_LIFETIME", value); }
}
public static float FadeoutDuration
{
get { return EditorPrefs.GetFloat("SVN_FADE_DUR", 2f); }
set { EditorPrefs.SetFloat("SVN_FADE_DUR", value); }
}
private static bool mouseOver;
private static void OnScene(SceneView sceneView)
{
Handles.BeginGUI();
//Get rekt
Rect highlightRect = new Rect(5, sceneView.camera.pixelHeight - 5 - 100, 100, 150);
Rect lineRect = new Rect(10, sceneView.camera.pixelHeight - 25, width - 10f, lineHeight);
Rect iconRect = new Rect(10, sceneView.camera.pixelHeight - 25, width - 10f, lineHeight);
lineRect.x += 20f; //Make room for icon
Vector2 mousePos = Event.current.mousePosition;
mouseOver = highlightRect.Contains(mousePos);
//Move everything up 20px to make room for the clear button
lineRect.y = (mouseOver) ? lineRect.y - 20f : lineRect.y;
iconRect.y = (mouseOver) ? iconRect.y - 20f : iconRect.y;
//Show button on mouseover
if (mouseOver) if (GUI.Button(new Rect(0, sceneView.camera.pixelHeight - 18f, 55f, 20f), "Clear log", EditorStyles.toolbarButton)) Clear();
foreach (Notification n in notifications)
{
//Time alive running from 0 and up
float lifetime = ((float)DateTime.Now.Subtract(n.dateTime).TotalMilliseconds);
float alpha = 1;
//When past maximum lifetime, start fadeTime at 0
if (lifetime >= (MaxLifetime * 1000))
{
float fadeTime = lifetime - (MaxLifetime * 1000);
alpha = 1 - (fadeTime / (FadeoutDuration * 1000));
}
Color originalColor = GUI.color;
Color textColor = GUI.color;
Texture icon = null;
switch (n.type)
{
case NotificationType.Info:
{
textColor = Color.white;
icon = EditorGUIUtility.IconContent("console.infoicon.sml").image;
}
break;
case NotificationType.Warning:
{
textColor = new Color(252f / 255f, 174f / 255f, 78f / 255f);
icon = EditorGUIUtility.IconContent("console.warnicon.sml").image;
}
break;
case NotificationType.Error:
{
textColor = new Color(255f / 255f, 112f / 255f, 112f / 255f);
icon = EditorGUIUtility.IconContent("console.erroricon.sml").image;
}
break;
}
GUI.color = new Color(1f, 1f, 1f, mouseOver ? 1f : alpha);
GUI.Label(iconRect, icon);
GUI.color = new Color(textColor.r, textColor.g, textColor.b, mouseOver ? 1f : alpha);
string hourString = ((n.dateTime.Hour <= 9) ? "0" : "") + n.dateTime.Hour;
string minuteString = ((n.dateTime.Minute <= 9) ? "0" : "") + n.dateTime.Minute;
string secString = ((n.dateTime.Second <= 9) ? "0" : "") + n.dateTime.Second;
string timeString = "[" + hourString + ":" + minuteString + ":" + secString + "] ";
GUI.Label(lineRect, new GUIContent(" " + timeString + "" + n.text + ""), LogText);
//Decrease height pos of next line
lineRect.y -= lineHeight;
iconRect.y -= lineHeight;
GUI.color = originalColor;
}
Handles.EndGUI();
}
static string[] infoMessages = new string[] { "Ding dong!", "So informative!", "<i>So important!</i>", "Something happened!" };
static string[] warningMessages = new string[] { "Oops!", "Careful", "Warning!", "Thin ice" };
static string[] errorMessages = new string[] { "<b>Error!</b>", "Dun goofed!", "Made a boo boo!" };
#if UNITY_2019_1_OR_NEWER
[SettingsProvider]
public static SettingsProvider SceneViewNotificationSettings()
{
var provider = new SettingsProvider("Editor/Scene Notifications", SettingsScope.Project)
{
label = "Scene Notifications",
guiHandler = (searchContent) =>
{
MaxLifetime = EditorGUILayout.Slider("Life time", MaxLifetime, 0.1f, 10f);
FadeoutDuration = EditorGUILayout.Slider("Fade time", FadeoutDuration, 0.1f, 10f);
MaxItems = EditorGUILayout.IntField("Maximum lines", MaxItems);
EditorGUILayout.Space();
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PrefixLabel(" ");
if (GUILayout.Button("Test message"))
{
NotificationType type = (NotificationType)(int)UnityEngine.Random.Range(0, 3);
string[] messages = new string[4];
if (type == NotificationType.Info) messages = infoMessages;
if (type == NotificationType.Warning) messages = warningMessages;
if (type == NotificationType.Error) messages = errorMessages;
string text = messages[UnityEngine.Random.Range(0, messages.Length)];
Add(text, type);
}
}
},
keywords = new HashSet<string>(new[] { "Scene", "Notifications", "Log" })
};
return provider;
}
#else
[PreferenceItem("Scene Notifications")]
public static void PreferencesGUI()
{
MaxLifetime = EditorGUILayout.Slider("Life time", MaxLifetime, 0.1f, 10f);
FadeoutDuration = EditorGUILayout.Slider("Fade time", FadeoutDuration, 0.1f, 10f);
MaxItems = EditorGUILayout.IntField("Maximum lines", MaxItems);
EditorGUILayout.Space();
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PrefixLabel(" ");
if (GUILayout.Button("Test message"))
{
NotificationType type = (NotificationType)(int)UnityEngine.Random.Range(0, 3);
string[] messages = new string[4];
if (type == NotificationType.Info) messages = infoMessages;
if (type == NotificationType.Warning) messages = warningMessages;
if (type == NotificationType.Error) messages = errorMessages;
string text = messages[UnityEngine.Random.Range(0, messages.Length)];
Add(text, type);
}
}
}
#endif
[InitializeOnLoad]
sealed class InitializeOnLoad : Editor
{
[InitializeOnLoadMethod]
public static void Initialize()
{
if (EditorApplication.isPlaying) return;
#if UNITY_2019_1_OR_NEWER
SceneView.duringSceneGui += OnScene;
#else
SceneView.onSceneGUIDelegate += OnScene;
#endif
}
}
private static GUIStyle _LogText;
public static GUIStyle LogText
{
get
{
if (_LogText == null)
{
_LogText = new GUIStyle(UnityEngine.GUI.skin.label)
{
richText = true,
alignment = TextAnchor.MiddleLeft,
wordWrap = false,
fontSize = 12,
stretchWidth = true,
font = (Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande.ttf"),
//fontStyle = FontStyle.Bold,
padding = new RectOffset()
{
left = 0,
right = 0,
top = 0,
bottom = 0
},
clipping = TextClipping.Overflow
};
}
return _LogText;
}
}
}
#endif