@@ -22,25 +22,35 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222using UnityEngine ;
2323using UnityEngine . UI ;
2424using System ;
25- using System . Collections . Generic ;
2625
2726namespace 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 }
0 commit comments