-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleToGUI.cs
More file actions
182 lines (140 loc) · 5.03 KB
/
ConsoleToGUI.cs
File metadata and controls
182 lines (140 loc) · 5.03 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
using System.IO;
using UnityEngine;
public class ConsoleToGUI : MonoBehaviour
{
//#if !UNITY_EDITOR
static string myLog = "";
private string output;
private string stack;
private Vector2 scrollPosition;
[Tooltip("Show/hide Stack Trace in Log")]
public bool ShowStack = true;
[Tooltip("Show/hide FPS Count")]
public bool ShowFPS = true;
[Tooltip("Number of characters that can be displayed")]
public int LogBuffer = 5000;
[Tooltip("Log Height on Buttom Screen")]
public int LogHeight = 250;
[Tooltip("No show on build version")]
public bool OnlyInEditor = false;
[Tooltip("Save file with log")]
public bool SaveLogFile = true;
public bool DisplayInUi = false;
private bool exitClicked = false;
[Tooltip("Log textbox show on start")]
public bool ShowLog = true;
public string logPath = "";
public bool logPathInApplicationPath = true;
private float deltaTime = 0.0f;
[Tooltip("Font log size")]
public int FontLogSize = 25;
private void Start()
{
if (logPathInApplicationPath)
{
logPath = Utils.GetAppPath() + "/Log";
}
else
{
logPath = Utils.GetPath() + "/Log";
}
EvaluatePath();
}
private void EvaluatePath()
{
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
Debug.Log("Log path in: " + logPath);
}
void OnEnable()
{
Application.logMessageReceived += Log;
Application.logMessageReceivedThreaded += Log;
}
void OnDisable()
{
Application.logMessageReceived -= Log;
Application.logMessageReceivedThreaded -= Log;
}
void Update()
{
if (ShowFPS && ShowLog)
deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
}
public void WriteLog(string logString)
{
if (logPath != "" && SaveLogFile)
{
TextWriter tw = new StreamWriter(logPath + "/log.txt", true);
tw.WriteLine(logString);
tw.Close();
}
}
public void Log(string logString, string stackTrace, LogType type)
{
output = logString;
stack = stackTrace;
// write a line of text to the file
// close the stream
myLog = output + "\n" + myLog;
if (stack.Length > 0 && ShowStack)
{
myLog = stack + "\n" + myLog;
WriteLog(System.DateTime.Now.ToString("yyyyMMddHHmmss") + ": " + output + "\n" + stack + "\n");
}
else
{
WriteLog(System.DateTime.Now.ToString("yyyyMMddHHmmss") + ": " + output + "\n");
}
if (myLog.Length > LogBuffer)
{
myLog = myLog.Substring(0, LogBuffer);
}
scrollPosition = new Vector2(scrollPosition.x, 0);
}
void OnGUI()
{
if (DisplayInUi)
{
if (!Application.isEditor || !OnlyInEditor) //Do not display in editor ( or you can use the UNITY_EDITOR macro to also disable the rest)
{
if (GUI.Button(new Rect(new Rect(10, Screen.height - LogHeight - 40, 100, 40)), "L"))
exitClicked = true;
if (ShowLog)
{
if (GUI.Button(new Rect(new Rect(130, Screen.height - LogHeight - 40, 100, 40)), "S"))
{
ShowStack = !ShowStack;
}
if (GUI.Button(new Rect(new Rect(250, Screen.height - LogHeight - 40, 100, 40)), "C"))
{
myLog = "";
}
// we want to place the TextArea in a particular location - use BeginArea and provide Rect
GUILayout.BeginArea(new Rect(10, Screen.height - LogHeight, Screen.width - 40, Screen.height - 10));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width - 40), GUILayout.Height(LogHeight));
// We just add a single label to go inside the scroll view. Note how the
// scrollbars will work correctly with wordwrap.
GUIStyle textStyle = new GUIStyle(GUI.skin.textArea);
textStyle.fontSize = FontLogSize;
// GUILayout.TextArea(myLog, textStyle);
GUILayout.Label(myLog, textStyle);
// End the scrollview we began above.
GUILayout.EndScrollView();
GUILayout.EndArea();
if (ShowFPS)
{
string FPS = string.Format("{0:0.0} ms ({1:0.} fps)", deltaTime * 1000.0f, 1.0f / deltaTime);
GUI.Label(new Rect(new Rect(370, Screen.height - LogHeight - 40, 250, 40)), FPS, textStyle);
}
}
if (exitClicked)
{
ShowLog = !ShowLog;
exitClicked = false;
}
}
}
}
//#endif
}