DebugConsole.cs
4.63 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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[assembly: System.Reflection.AssemblyVersion("1.0.*")]
public class DebugConsole : MonoBehaviour
{
// Use this event to reset junk in your game to their initial states.
public static event System.Action OnReset;
public static int fps { get; private set; }
[Header("Spawning")]
[SerializeField]
private CanvasGroup _container;
[SerializeField]
private KeyCode toggleKey = KeyCode.D;
[SerializeField]
private int _tapsToSpawn = 3;
[SerializeField]
private float _totalTapTime = 1;
[Header("Info")]
[SerializeField]
private Text _appInfo;
[SerializeField]
private Text _fpsCounter;
[SerializeField]
private float _fpsUpdateFrequency = 0.5f;
[SerializeField]
private Text _debugTextNode;
private static DebugConsole _inst = null;
void Awake()
{
if (_inst != null)
{
Destroy(gameObject);
return;
}
_inst = this;
DontDestroyOnLoad(this);
}
private void Start ()
{
if (Application.isMobilePlatform)
GetComponent<CanvasScaler>().scaleFactor = 3;
Application.logMessageReceived += OnLogMessageReceived;
_debugTextNode.text = "Debug Console Window";
_debugTextNode.color = Color.cyan;
Hide();
string[] appInfo = new string[]
{
Application.companyName,
Application.productName,
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
};
_appInfo.text = string.Format("{0} {1} - v{2}", appInfo);
StartCoroutine(FPS());
}
private IEnumerator FPS()
{
while (true)
{
int lastFrameCount = Time.frameCount;
float lastTime = Time.realtimeSinceStartup;
yield return new WaitForSeconds(_fpsUpdateFrequency);
float timeSpan = Time.realtimeSinceStartup - lastTime;
int frameCount = Time.frameCount - lastFrameCount;
fps = Mathf.RoundToInt(frameCount / timeSpan);
_fpsCounter.text = fps.ToString() + " fps";
}
}
private List<Text> _logItems = new List<Text>();
private const int _maxLogItems = 50;
private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
{
Text newText = Instantiate(_debugTextNode) as Text;
newText.transform.SetParent(_debugTextNode.transform.parent);
newText.transform.localScale = _debugTextNode.transform.localScale;
newText.text = string.Format("{0}\n{1}", condition, stackTrace);
switch (type)
{
case LogType.Log:
newText.color = Color.white;
break;
case LogType.Warning:
newText.color = Color.yellow;
break;
case LogType.Exception:
newText.color = Color.red;
break;
case LogType.Error:
newText.color = Color.red;
break;
}
_logItems.Add(newText);
if (_logItems.Count > _maxLogItems)
{
Destroy(_logItems[0].gameObject);
_logItems.RemoveAt(0);
}
}
public void DebugTap()
{
if (_container.gameObject.activeSelf)
Hide();
else
_tapCount++;
}
private int _tapCount;
private float _currentTapTime;
private void Update()
{
if (Input.GetKeyDown(toggleKey))
Toggle();
if (_tapCount == 0) return;
_currentTapTime += Time.deltaTime;
if (_currentTapTime >= _totalTapTime)
{
_currentTapTime = 0;
_tapCount = 0;
}
else if (_tapCount >= _tapsToSpawn)
{
_tapCount = 0;
_currentTapTime = 0;
Toggle();
}
}
public void Toggle()
{
if (_container.gameObject.activeSelf)
Hide();
else
Show();
}
public void Hide()
{
_container.gameObject.SetActive(false);
_container.interactable = false;
_container.blocksRaycasts = false;
}
public void Show()
{
_container.gameObject.SetActive(true);
_container.interactable = true;
_container.blocksRaycasts = true;
}
public void ResetApp()
{
if (OnReset != null) OnReset();
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
private void OnDestroy()
{
Application.logMessageReceived -= OnLogMessageReceived;
}
}