-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugPanic.cs
More file actions
334 lines (284 loc) · 7.84 KB
/
DebugPanic.cs
File metadata and controls
334 lines (284 loc) · 7.84 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System.IO;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
/// <summary>
/// デバッグ用パニック検知
/// </summary>
[InitializeOnLoad]
public static class DebugPanic
{
#region property
/// <summary>
/// 状態
/// </summary>
private enum State
{
/// <summary>
/// 何もしない
/// </summary>
None,
/// <summary>
/// 監視中
/// </summary>
Detection,
/// <summary>
/// 検知した
/// </summary>
Error,
}
/// <summary>
/// 検知レベル
/// </summary>
private enum DetectionLevel
{
/// <summary>
/// ガバ
/// </summary>
None,
/// <summary>
/// Exception
/// </summary>
Soft,
/// <summary>
/// Exception,Assert
/// </summary>
Medium,
/// <summary>
/// Exception,Assert,Error
/// </summary>
Hard,
/// <summary>
/// Exception,Assert,Error,Warning
/// </summary>
VeryHard,
}
private static readonly string[] MenuItemPath = new string[]
{
"Tools/DebugPanicLevel/None",
"Tools/DebugPanicLevel/Soft",
"Tools/DebugPanicLevel/Medium",
"Tools/DebugPanicLevel/Hard",
"Tools/DebugPanicLevel/VeryHard"
};
private static State _state = State.None;
private static DetectionLevel _level = DetectionLevel.Hard;
private static GameObject _panicObject;
private static RawImage _backGround;
private static Text _text;
#endregion
static DebugPanic()
{
if(EditorApplication.isPlayingOrWillChangePlaymode)
{
UpdateMenuItemChecked();
_level = GetDetectionLevelByMenuChecked();
SetState(State.Detection);
}
EditorApplication.playModeStateChanged += OnChangedPlayMode;
}
#region menu
[MenuItem("Tools/DebugPanicLevel/None", priority = 101)]
private static void DetectionLevelNone()
{
_level = DetectionLevel.None;
UpdateMenuItemChecked();
}
[MenuItem("Tools/DebugPanicLevel/Soft", priority = 102)]
private static void DetectionLevelSoft()
{
_level = DetectionLevel.Soft;
UpdateMenuItemChecked();
}
[MenuItem("Tools/DebugPanicLevel/Medium", priority = 103)]
private static void DetectionLevelMedium()
{
_level = DetectionLevel.Medium;
UpdateMenuItemChecked();
}
[MenuItem("Tools/DebugPanicLevel/Hard", priority = 104)]
private static void DetectionLevelHard()
{
_level = DetectionLevel.Hard;
UpdateMenuItemChecked();
}
[MenuItem("Tools/DebugPanicLevel/VeryHard", priority = 105)]
private static void DetectionLevelVeryHard()
{
_level = DetectionLevel.VeryHard;
UpdateMenuItemChecked();
}
[MenuItem("Tools/DebugPanicLevel/ShowInExplorer", priority = 255)]
private static void ShowInExplorer()
{
// 一つ上の階層が開かれてしまうのでProcessに変更
// EditorUtility.RevealInFinder(Application.persistentDataPath);
Process.Start(Application.persistentDataPath);
}
private static void UpdateMenuItemChecked()
{
for(int i = 0; i < MenuItemPath.Length; ++i)
{
Menu.SetChecked(MenuItemPath[i], (_level == (DetectionLevel)i));
}
}
private static DetectionLevel GetDetectionLevelByMenuChecked()
{
DetectionLevel level = DetectionLevel.None;
for(int i = 0; i < MenuItemPath.Length; ++i)
{
if(Menu.GetChecked(MenuItemPath[i]))
{
level = (DetectionLevel)i;
break;
}
}
return level;
}
#endregion
#region event
private static void OnChangedPlayMode(PlayModeStateChange state)
{
switch(state)
{
// 再生時にコンストラクタが走るので初期化に使えない
// case PlayModeStateChange.ExitingEditMode:
// オブジェクトにアタッチされたスクリプトのほうが実行が早い場合がある
// case PlayModeStateChange.EnteredPlayMode:
// SetState(State.Detection);
// break;
case PlayModeStateChange.ExitingPlayMode:
SetState(State.None);
break;
}
}
/// <summary>
/// ログのハンドリング
/// </summary>
/// <param name="condition"></param>
/// <param name="stackTrace"></param>
/// <param name="type"></param>
private static void OnLogMessageReceived(string condition, string stackTrace, LogType type)
{
if(_state != State.Detection) return;
if(IsPanic(type) == false) return;
SetState(State.Error);
CreatePanicObject(condition, stackTrace);
Screenshot();
}
#endregion
#region method
/// <summary>
/// 状態を設定する
/// </summary>
/// <param name="state"></param>
private static void SetState(State state)
{
_state = state;
switch(_state)
{
case State.None:
Application.logMessageReceived -= OnLogMessageReceived;
Destroy();
break;
case State.Detection:
Application.logMessageReceived += OnLogMessageReceived;
break;
case State.Error:
Application.logMessageReceived -= OnLogMessageReceived;
break;
}
}
/// <summary>
/// 検知
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static bool IsPanic(LogType type)
{
bool ret = false;
switch(_level)
{
case DetectionLevel.None:
break;
case DetectionLevel.Soft:
ret = (type == LogType.Exception);
break;
case DetectionLevel.Medium:
ret = (type == LogType.Exception);
ret |= (type == LogType.Assert);
break;
case DetectionLevel.Hard:
ret = (type == LogType.Exception);
ret |= (type == LogType.Assert);
ret |= (type == LogType.Error);
break;
case DetectionLevel.VeryHard:
ret = (type == LogType.Exception);
ret |= (type == LogType.Assert);
ret |= (type == LogType.Error);
ret |= (type == LogType.Warning);
break;
}
return ret;
}
/// <summary>
/// パニックオブジェクト作成
/// </summary>
/// <param name="condition"></param>
/// <param name="stackTrace"></param>
private static void CreatePanicObject(string condition, string stackTrace)
{
_panicObject = new GameObject("panic");
Canvas canvas = _panicObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
GameObject backGroundObject = new GameObject("backGround");
_backGround = backGroundObject.AddComponent<RawImage>();
_backGround.rectTransform.SetParent(_panicObject.transform, false);
_backGround.rectTransform.anchorMin = new Vector2(0, 0);
_backGround.rectTransform.anchorMax = new Vector2(1, 1);
_backGround.rectTransform.anchoredPosition = Vector2.zero;
_backGround.rectTransform.sizeDelta = new Vector2(0, 0);
_backGround.color = new Color(0, 0, 1, 0.5f);
GameObject textObject = new GameObject("stackTrace");
_text = textObject.AddComponent<Text>();
_text.rectTransform.SetParent(_panicObject.transform, false);
_text.rectTransform.anchorMin = new Vector2(0, 0);
_text.rectTransform.anchorMax = new Vector2(1, 1);
_text.rectTransform.anchoredPosition = Vector2.zero;
_text.rectTransform.sizeDelta = new Vector2(0, 0);
_text.horizontalOverflow = HorizontalWrapMode.Wrap;
_text.verticalOverflow = VerticalWrapMode.Truncate;
_text.alignment = TextAnchor.UpperLeft;
_text.raycastTarget = false;
_text.fontSize = 22;
_text.color = Color.white;
_text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
_text.text = condition + "\n" + stackTrace;
}
/// <summary>
/// スクショ
/// </summary>
private static void Screenshot()
{
// TODO:パス指定可能に
string path = Application.persistentDataPath + Path.DirectorySeparatorChar;
string filename = "Screenshot_" + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";
ScreenCapture.CaptureScreenshot(path + filename);
}
/// <summary>
/// 破棄
/// </summary>
private static void Destroy()
{
if(_panicObject != null)
{
GameObject.DestroyImmediate(_panicObject);
_panicObject = null;
_backGround = null;
_text = null;
}
}
#endregion
}