Skip to content

Commit 2e05add

Browse files
dustindustin
authored andcommitted
Added Simple Console Debug Messages to ConsolePanel
1 parent 8f47195 commit 2e05add

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

Assets/UIController/ConsolePanel.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,35 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
using UnityEngine;
2323
using UnityEngine.UI;
2424
using System;
25-
using System.Collections.Generic;
2625

2726
namespace Experica.Command
2827
{
29-
public enum LogType
30-
{
31-
Log,
32-
Warning,
33-
Error
34-
}
3528

3629
public class ConsolePanel : MonoBehaviour
3730
{
3831
public UIController uicontroller;
39-
public GameObject content, logprefab, warningprefab, errorprefab;
32+
public GameObject content, logprefab, warningprefab, errorprefab ;
4033

4134
public int maxentry;
4235
int entrycount;
4336

37+
/// <summary>
38+
/// A log handler is added to the Unity application that calls ConsolePanel.Log() for every Debug message.
39+
/// The handler is added whent the consolepanel is createdt
40+
/// </summary>
41+
void Awake()
42+
{
43+
// logMessageReceived is an event that is called when messages are logged, which a handler is added to.
44+
Application.logMessageReceived += delegate(string logString, string stackTrace, LogType type)
45+
{
46+
Log(type, logString, true);
47+
};
48+
}
49+
50+
/// <summary>
51+
/// Remove all the text prefab objects from the content box in the consolePanel. Basically,
52+
/// Get rid of all the text. This is called when the Clear button is pressed.
53+
/// </summary>
4454
public void Clear()
4555
{
4656
for (var i = 0; i < content.transform.childCount; i++)
@@ -51,33 +61,29 @@ public void Clear()
5161
UpdateViewRect();
5262
}
5363

54-
public void LogError(object msg, bool istimestamp = true)
55-
{
56-
Log(LogType.Error, msg, istimestamp);
57-
}
58-
59-
public void LogWarn(object msg, bool istimestamp = true)
60-
{
61-
Log(LogType.Warning, msg, istimestamp);
62-
}
63-
64-
public void Log(object msg, bool istimestamp = true)
65-
{
66-
Log(LogType.Log, msg, istimestamp);
67-
}
68-
64+
/// <summary>
65+
/// Adds a text GameObject to the content GameObject in the Console Window.
66+
/// </summary>
67+
/// <param name="logtype">log, Warning, Error, Assert, or Exception</param>
68+
/// <param name="msg">Text to be displayed</param>
69+
/// <param name="istimestamp">True displays time, False doesn't</param>
6970
public void Log(LogType logtype, object msg, bool istimestamp = true)
7071
{
7172
var v = msg.Convert<string>();
73+
v = v.Replace('\n', ' '); // With wrapping text and newline characters, text is small. Replace with just a space
74+
75+
// Add time to text
7276
if (istimestamp)
7377
{
7478
v = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + ": " + v;
7579
}
7680

81+
// Add Text to GameObject
7782
var text = Instantiate(ChoosePrefab(logtype));
7883
text.GetComponent<Text>().text = v;
7984
text.transform.SetParent(content.transform, false);
8085

86+
// Delete
8187
if (entrycount > maxentry)
8288
{
8389
Destroy(content.transform.GetChild(0).gameObject);
@@ -105,6 +111,10 @@ GameObject ChoosePrefab(LogType logtype)
105111
return warningprefab;
106112
case LogType.Error:
107113
return errorprefab;
114+
case LogType.Assert:
115+
return warningprefab;
116+
case LogType.Exception:
117+
return errorprefab;
108118
default:
109119
return logprefab;
110120
}

Assets/UIController/UIController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void OnBeginStartExperiment()
374374
deleteex.interactable = false;
375375
startstoptext.text = "Stop";
376376
pause.interactable = true;
377-
consolepanel.Log("Experiment Started.");
377+
UnityEngine.Debug.Log("Experiment Started.");
378378

379379
// Get Highest Performance
380380
QualitySettings.vSyncCount = 0;
@@ -431,7 +431,7 @@ public void OnBeginStopExperiment()
431431
}
432432
startstoptext.text = "Start";
433433
pause.interactable = false;
434-
consolepanel.Log("Experiment Stoped.");
434+
UnityEngine.Debug.Log("Experiment Stoped.");
435435

436436
// Return Normal Performance
437437
QualitySettings.vSyncCount = 1;
@@ -470,7 +470,7 @@ public void SaveData()
470470
public void OnBeginPauseExperiment()
471471
{
472472
pauseresumetext.text = "Resume";
473-
consolepanel.LogWarn("Experiment Paused.");
473+
UnityEngine.Debug.LogWarning("Experiment Paused.");
474474
}
475475

476476
public void OnEndPauseExperiment()
@@ -492,7 +492,7 @@ public void TogglePauseResumeExperiment(bool ispause)
492492
public void OnBeginResumeExperiment()
493493
{
494494
pauseresumetext.text = "Pause";
495-
consolepanel.LogWarn("Experiment Resumed.");
495+
UnityEngine.Debug.LogWarning("Experiment Resumed.");
496496
}
497497

498498
public void OnEndResumeExpeirment()

0 commit comments

Comments
 (0)