Search Unity

Text not displaying with custom GUIStyle

Discussion in 'Immediate Mode GUI (IMGUI)' started by mkolb, Jan 10, 2008.

  1. mkolb

    mkolb

    Joined:
    Dec 17, 2007
    Posts:
    48
    Hi all,
    I tried to make something like an event log which, lists nicely the thrown events together with an icon.

    So I defined a GUIStyle in my script and changed the values accordingly in the Inspector.

    That's what I ended up with:
    Code (csharp):
    1.  
    2. var InfoIcon : Texture2D;
    3. private var EventMessages : ArrayList;
    4. var ConsoleGUIStyle : GUIStyle;
    5.  
    6. function Start() {
    7.     EventMessages = new ArrayList();
    8. }
    9.  
    10. function OnGUI() {
    11.     if (GUI.RepeatButton(rel(0.18,0.76,0.02,0.02),"+")) {
    12.         GameObject.Find("Bert450_Boot").GetComponent("Boat").SendMessage("zoomMap",5);
    13.     }
    14.     if (GUI.RepeatButton(rel(0.197,0.777,0.02,0.02),"-")) {
    15.         GameObject.Find("Bert450_Boot").GetComponent("Boat").SendMessage("zoomMap",-5);
    16.     }
    17.     Console();
    18. }
    19.  
    20. function rel(x : float, y: float, w: float, h: float) : Rect {
    21.     return Rect(Screen.width*x, Screen.height*y, Screen.width*w, Screen.height*h);
    22. }
    23.  
    24. function LogMessage(message: String) {
    25.     EventMessages.Add(message);
    26.     if (EventMessages.Count > 10) {
    27.         EventMessages.RemoveAt(EventMessages.Count - 1);
    28.     }
    29. }
    30.  
    31. function Console() {
    32.     GUILayout.BeginArea(rel(0.05,0.4,0.2,0.2));
    33.     GUILayout.Label("");
    34.         GUILayout.BeginVertical();
    35.     if (EventMessages.Count > 0) {
    36.         for (var Message : String in EventMessages) {
    37.             GUILayout.Label(GUIContent(InfoIcon,Message),ConsoleGUIStyle);
    38.         }
    39.     }
    40.         GUILayout.EndVertical();
    41.     GUILayout.EndArea();
    42. }
    43.  
    So I try, unless the Messages are empty, to create a custom label with an icon and some info text, but the text is not displayed. I think I sticked to the scripting manual and tried a few things but nothing worked.
    Can anyone help me please?
    Thank you in advance!
    Regards
     
  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    I've done the same thing except I don't use icons, but you could easily add them by modifying the MyDebugItem struct. (Example is C#)

    In your "global' class, create a struct(which defines each debug list item) and an ArrayList (our Debug Log) - we overload the method to allow for a title item or title+details items:

    Code (csharp):
    1. //My Debug Log Struct
    2.     public struct MyDebugItem
    3.     {
    4.         public bool ShowDetails;
    5.         public string TimeStamp;
    6.         public string ItemHeader;
    7.         public string ItemDetails;
    8.         public MyDebugItem(string mItemHeader, string mItemDetails)
    9.         {
    10.             this.ShowDetails = false;
    11.             this.TimeStamp = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
    12.             this.ItemHeader = mItemHeader;
    13.             this.ItemDetails = mItemDetails;
    14.         }
    15.         public MyDebugItem(string mItemHeader)
    16.         {
    17.             this.ShowDetails = false;
    18.             this.TimeStamp = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
    19.             this.ItemHeader = mItemHeader;
    20.             this.ItemDetails = "No Additional Information";
    21.         }
    22.     }
    23.     public static ArrayList MyDebugLog = new ArrayList();
    To add stuff to the debug log, you have two options (with details or just the title):
    Code (csharp):
    1.         MyGlobalScript.MyDebugLog.Add(new MyGlobalScript.MyDebugItem("App Initialized Successfullly"));
    2.         MyGlobalScript.MyDebugLog.Add(new MyGlobalScript.MyDebugItem("Data Path", Application.dataPath));
    This is the GUI used to display the debug log - mine is also setup for a custom GUISKin so it will probably look strange using the default. However, it does show a nice way to create a collapsable debug log.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class DebugWindow : MonoBehaviour
    6. {
    7.     private Rect DebugWindowPosition;
    8.     private Vector2 DebugWindowLogScrollPos;
    9.     private bool FocusDebugWindow = false;
    10.     void OnGUI()
    11.     {
    12.  
    13.         DebugWindowPosition = GUI.Window(1, DebugWindowPosition, DoDebugLogWindow, "My Debug Log");
    14.     }
    15.  
    16.     void DoDebugLogWindow(int mWindowID)
    17.     {
    18.         if (FocusDebugWindow)
    19.         {
    20.             GUI.BringWindowToFront(mWindowID);
    21.             GUI.FocusWindow(mWindowID);
    22.             FocusDebugWindow = false;
    23.         }
    24.         if (GUI.Button(new Rect(284, 10, 16, 16), "", GUI.skin.GetStyle("closebutton16x16"))) ShowDebugWindow = false;
    25.         GUILayout.BeginVertical();
    26.         GUILayout.Space(25);
    27.         DebugWindowLogScrollPos = GUILayout.BeginScrollView(DebugWindowLogScrollPos);
    28.         for (int i = 0; i < MyGlobalScript.MyDebugLog.Count; i++)
    29.         {
    30.             MyGlobalScript.MyDebugItem thisDI = new MyGlobalScript.MyDebugItem();
    31.             thisDI = (MyGlobalScript.MyDebugItem)MyGlobalScript.MyDebugLog[i];
    32.             if (!thisDI.ShowDetails)
    33.             {
    34.                 GUILayout.BeginVertical();
    35.                 GUILayout.BeginHorizontal();
    36.                 thisDI.ShowDetails = GUILayout.Toggle(thisDI.ShowDetails, "", GUILayout.Width(28), GUILayout.Height(28));
    37.                 MyGlobalScript.MyDebugLog[i] = thisDI;
    38.                 string DebugSummaryItem = thisDI.ItemHeader + ": " + thisDI.ItemDetails;
    39.                 if (DebugSummaryItem.Length > 32) DebugSummaryItem = DebugSummaryItem.Substring(0, 29) + "...";
    40.                 GUILayout.Label(DebugSummaryItem, GUILayout.Height(28));
    41.                 GUILayout.EndHorizontal();
    42.             }
    43.             else
    44.             {
    45.                 GUILayout.BeginVertical();
    46.                 GUILayout.BeginHorizontal();
    47.                 thisDI.ShowDetails = GUILayout.Toggle(thisDI.ShowDetails, "", GUILayout.Width(32), GUILayout.Height(28));
    48.                 MyGlobalScript.MyDebugLog[i] = thisDI;
    49.                 GUILayout.TextArea(thisDI.TimeStamp + "\n" + thisDI.ItemHeader + "\n" + thisDI.ItemDetails, GUILayout.Width(220));
    50.                 GUILayout.EndHorizontal();
    51.             }
    52.             GUILayout.EndVertical();
    53.         }
    54.         GUILayout.EndScrollView();
    55.         GUILayout.EndVertical();
    56.         GUI.DragWindow();
    57.     }
    58. }