DebugConsole

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Updated script to newest version and reworked the usage.)
Line 5: Line 5:
 
Author: Jeremy Hollingsworth (jeremyace)
 
Author: Jeremy Hollingsworth (jeremyace)
 
== Description ==
 
== Description ==
This script will control a guiText object to generate a scrolling console-like debug feeback pane.  Allows you to set one of three colors (normal, warning, error) to each line to get more feedback.
+
This script will control a GUIText object to generate a scrolling console-like debug feeback pane.  Allows you to set one of three colors (normal, warning, error) to each line to get more feedback.
  
 
== Usage ==
 
== Usage ==
<b>This C# script _must_ be placed in the Standard Assets folder to be called from Jscript.</b>
+
<b>This C# script _must_ be placed in the Standard Assets folder to be called from JavaScript.</b>
  
<BR><b>First create a new guiText (GameObject->Create Other->Text).</bThis guiText will be duplicated and moved by the script to form your debug output, so set it up as you want the individual output lines to appear.
+
<BR><b>UPDATED:</b> You no longer have to set your own GUIText in the scene.  This script will create a GUIText at <i>Vector3 defaultGuiPosition</i> with a scale of <i>Vector3 defaultGuiScale</i>. If you wish to provide your own GUIText object to set your own font, etc, then drag your GUIText to the Debug GUI field of the DebugGUIController script.
* Make sure the Text field of the guiText is blank.
+
* Make sure you set a material (the orange font material) as the code will be modifying the color property.
+
* Make sure you set the position of this object to where you want the first line of output to be (code duplicate down).
+
* I recommend you cut the scale down so you can fit more lines in the output.
+
  
<BR><b>Now create a new blank GameObject (GameObject->Create Empty) and then drag the DebugGUIController.cs script to it.</b>
+
<BR><b>Create a new blank GameObject (GameObject->Create Empty) and then drag the DebugGUIController.cs script to it.  It is now fully usable with the default settings.</b>
  
 
<BR><b>Settings:</b>
 
<BR><b>Settings:</b>
* Debug GUI : Drag the guiText you created here.
+
* Debug GUI : Drag the guiText you created here if you wish to override the default.
 +
* defaultGuiPosition : A vector for the starting position of the first line of output.  If you provide your own GUIText then it's position overrides this property.
 +
* defaultGuiScale : A vector for the scale of the individual lines.  Again, this is overridden if you provide your own GUIText.  
 
* The three colors are for normal, warning, and error.  Set to whatever you like or leave alone.
 
* The three colors are for normal, warning, and error.  Set to whatever you like or leave alone.
 
* Max messages : How many message lines we will put on the screen at once.  This number depends on the size of your guiText.
 
* Max messages : How many message lines we will put on the screen at once.  This number depends on the size of your guiText.
* Line Spacing : A float for how far down we will move each new line.  Again, this depends on the size of your guiText.
+
* Line Spacing : A float for how far down we will move each new line.
 
* Show On Screen : The default setting for visibility of the debug output.  If set to false (unchecked), you can send data but it wont show until you tell it to, if set to true (checked) it will show all debug output until you tell it not to.
 
* Show On Screen : The default setting for visibility of the debug output.  If set to false (unchecked), you can send data but it wont show until you tell it to, if set to true (checked) it will show all debug output until you tell it not to.
 
<BR>The setup is done!
 
  
 
<BR><b>Using the Debug script from your scripts:</b>
 
<BR><b>Using the Debug script from your scripts:</b>
 
<BR>To call the DebugGUIController script from your scripts, just type:
 
<BR>To call the DebugGUIController script from your scripts, just type:
<BR>DebugGUIController.instance.<i>someFunction()</i>
 
  
<BR>Replace <i>someFunction()</i> with any of the following:
+
<BR>DebugGUIController.instance.<i><b>someFunction()</b></i>
 +
 
 +
<BR>Replace <i><b>someFunction()</b></i> with any of the following:
  
 
<BR><b>AddMessage</b>(<b>string</b> <i>message</i>, <b>string</b> <i>color</i>)
 
<BR><b>AddMessage</b>(<b>string</b> <i>message</i>, <b>string</b> <i>color</i>)
Line 51: Line 48:
 
The script _must_ be named DebugGUIController.cs
 
The script _must_ be named DebugGUIController.cs
 
<csharp>
 
<csharp>
 +
//------ DebugGUIController.cs -------------------
 +
// Script for handling multi-line, multi-colored
 +
// GUI debugging output.
 +
// Author: Jeremy Hollingsworth (jeremyace)
 +
// Version: 1.0
 +
//------------------------------------------------
 
using UnityEngine;
 
using UnityEngine;
 
using System.Collections;
 
using System.Collections;
  
  
public class DebugGUIController : MonoBehaviour {
+
public class DebugGUIController : MonoBehaviour
 +
{
  
public GameObject DebugGui = null;            // The GUI that will be duplicated
+
    public GameObject DebugGui = null;            // The GUI that will be duplicated
public Color normal = Color.green;
+
    public Vector3 defaultGuiPosition = new Vector3(0.01F, 0.98F, 0F);
public Color warning = Color.yellow;  
+
    public Vector3 defaultGuiScale = new Vector3(0.7F, 0.7F, 0F);
public Color error = Color.red;
+
    public Color normal = Color.green;
public int maxMessages = 20;                  // The max number of messages displayed
+
    public Color warning = Color.yellow;
public float lineSpacing = 0.02F;              // The amount of space between lines
+
    public Color error = Color.red;
public ArrayList messages = new ArrayList(); // The messages array
+
    public int maxMessages = 20;                  // The max number of messages displayed
public ArrayList names = new ArrayList();     // The GUI names array.  This holds the names of our duplicated GUIs
+
    public float lineSpacing = 0.02F;              // The amount of space between lines
public ArrayList colors = new ArrayList();   // The array that holds the user selection of color for each message. normal, warning, error.
+
    public ArrayList messages = new ArrayList();
public bool showOnScreen = true;             // Does the output actually get displayed on screen or not?
+
    public ArrayList names = new ArrayList();
 +
    public ArrayList colors = new ArrayList();
 +
    public bool showOnScreen = true;
  
  
// s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
+
    // s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
 
     private static DebugGUIController s_Instance = null;
 
     private static DebugGUIController s_Instance = null;
+
 
 
     // This defines a static instance property that attempts to find the manager object in the scene and
 
     // This defines a static instance property that attempts to find the manager object in the scene and
 
     // returns it to the caller.
 
     // returns it to the caller.
     public static DebugGUIController instance {
+
     public static DebugGUIController instance
         get {
+
    {
             if (s_Instance == null) {
+
         get
                // This is where the magic happens.
+
        {
                //  FindObjectOfType(...) returns the first AManager object in the scene.
+
             if (s_Instance == null)
                 s_Instance = FindObjectOfType(typeof (DebugGUIController)) as DebugGUIController;
+
            {
 +
                 s_Instance = FindObjectOfType(typeof(DebugGUIController)) as DebugGUIController;
 
                 if (s_Instance == null)
 
                 if (s_Instance == null)
                     Debug.Log ("Could not locate a DebugGUIController object.  You have to have exactly one DebugGUIController in the scene.");
+
                     Debug.Log("Could not locate a DebugGUIController object.  You have to have exactly one DebugGUIController in the scene.");
 
             }
 
             }
           
+
 
 
             return s_Instance;
 
             return s_Instance;
 
         }
 
         }
 
     }
 
     }
   
+
 
     void Awake() {
+
     void Awake()
 +
    {
 
         s_Instance = this;
 
         s_Instance = this;
          
+
         if (DebugGui == null)
 +
        {
 +
            DebugGui = new GameObject();
 +
            DebugGui.AddComponent("GUIText");
 +
            DebugGui.name = "DebugGUI";
 +
            DebugGui.transform.position = defaultGuiPosition;
 +
            DebugGui.transform.localScale = defaultGuiScale;
 +
        }
 +
 
 
         // Create our GUI objects to our maxMessages count
 
         // Create our GUI objects to our maxMessages count
 
         Vector3 position = new Vector3(0.01F, DebugGui.transform.position.y, 0F);
 
         Vector3 position = new Vector3(0.01F, DebugGui.transform.position.y, 0F);
 
         int x = 0;
 
         int x = 0;
         while(x < maxMessages)
+
         while (x < maxMessages)
 
         {
 
         {
             position.y -= lineSpacing; //new position moved down from parent
+
             position.y -= lineSpacing;
 
             Object clone = null;
 
             Object clone = null;
 
             clone = Instantiate(DebugGui, position, transform.rotation);
 
             clone = Instantiate(DebugGui, position, transform.rotation);
             string name = "DebugGui(" + x + ")";    // Add the current loop number to the DebugGui() name string
+
             string name = "DebugGui(" + x + ")";     
             names.Add(name);   // add the name to our name array
+
             names.Add(name);                      
             clone.name = name; // Set the name of our newly instantiated GUI object.
+
             clone.name = name;                    
             x+=1;  
+
             x += 1;
        }
+
        }
  
 
     }
 
     }
Line 112: Line 128:
 
     //Adds a mesage to the list
 
     //Adds a mesage to the list
 
     //--------------------------------------------------------------
 
     //--------------------------------------------------------------
+
 
public void AddMessage(string message, string color)
+
    public void AddMessage(string message, string color)
{
+
    {
  messages.Add(message);
+
        messages.Add(message);
  colors.Add(color);   
+
        colors.Add(color);   
  Display();
+
        Display();
}
+
//++++++++++ OVERLOAD for AddMessage ++++++++++++++++++++++++++++
+
// Overloads AddMessage to only require one argument(message)
+
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
public void AddMessage(string message)
+
{
+
  messages.Add(message);
+
  colors.Add("normal");
+
  Display();
+
 
     }
 
     }
 +
    //++++++++++ OVERLOAD for AddMessage ++++++++++++++++++++++++++++
 +
    // Overloads AddMessage to only require one argument(message)
 +
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +
    public void AddMessage(string message)
 +
    {
 +
        messages.Add(message);
 +
        colors.Add("normal");
 +
        Display();
 +
    }
 +
 +
  
 
 
 
     //----------- void ClearMessages() ------------------------------
 
     //----------- void ClearMessages() ------------------------------
 
     // Clears the messages from the screen and the lists
 
     // Clears the messages from the screen and the lists
Line 140: Line 156:
 
         ClearScreen();
 
         ClearScreen();
 
     }
 
     }
   
+
 
   
+
 
 
     //--------- void Hide() -----------------------------------------
 
     //--------- void Hide() -----------------------------------------
 
     // Hides the display with a variable, but does NOT clear arrays
 
     // Hides the display with a variable, but does NOT clear arrays
Line 150: Line 166:
 
         ClearScreen();
 
         ClearScreen();
 
     }
 
     }
   
+
 
   
+
 
 
     //--------- void Show() ----------------------------------------
 
     //--------- void Show() ----------------------------------------
 
     // Shows the hidden display on the screen
 
     // Shows the hidden display on the screen
Line 160: Line 176:
 
         Display();
 
         Display();
 
     }
 
     }
   
+
 
   
+
 
 
+
 
 
     //-------- void ClearScreen() ----------------------------------
 
     //-------- void ClearScreen() ----------------------------------
 
     // Clears all output from all GUI objects
 
     // Clears all output from all GUI objects
Line 171: Line 187:
 
         while (x < names.Count)
 
         while (x < names.Count)
 
         {
 
         {
             GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
+
             GameObject gui = GameObject.Find((string)names[x]);   
 
             gui.guiText.text = "";
 
             gui.guiText.text = "";
 
             //increment and loop
 
             //increment and loop
 
             x += 1;
 
             x += 1;
         
+
 
 
         }//end loop
 
         }//end loop
 
     }
 
     }
  
   
+
 
   
+
 
 
     //---------- void Prune() ---------------------------------------
 
     //---------- void Prune() ---------------------------------------
 
     // Prunes the array to fit within the maxMessages limit
 
     // Prunes the array to fit within the maxMessages limit
Line 186: Line 202:
 
     void Prune()
 
     void Prune()
 
     {
 
     {
    int diff;
+
        int diff;
 
         if (messages.Count > maxMessages)
 
         if (messages.Count > maxMessages)
 
         {
 
         {
          if (messages.Count <= 0)
+
            if (messages.Count <= 0)
          {
+
            {
            diff = 0;
+
                diff = 0;
          }
+
            }
          else
+
            else
          {
+
            {
            diff = messages.Count - maxMessages;
+
                diff = messages.Count - maxMessages;
          }
+
            }
          messages.RemoveRange(0, (int)diff);
+
            messages.RemoveRange(0, (int)diff);
          colors.RemoveRange(0, (int)diff);
+
            colors.RemoveRange(0, (int)diff);
 
         }
 
         }
   
+
 
 
     }
 
     }
   
+
 
 
     //---------- void Display() -------------------------------------
 
     //---------- void Display() -------------------------------------
 
     // Displays the list and handles coloring
 
     // Displays the list and handles coloring
Line 211: Line 227:
 
         if (showOnScreen == false)
 
         if (showOnScreen == false)
 
         {
 
         {
          ClearScreen();
+
            ClearScreen();
 
         }
 
         }
         else  
+
         else
 
         {
 
         {
 
             if (messages.Count > maxMessages)
 
             if (messages.Count > maxMessages)
 
             {
 
             {
              Prune();
+
                Prune();
            }           
+
       
+
        // Carry on with display
+
               
+
        int x = 0;
+
        while (x < messages.Count)
+
        {
+
     
+
            GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
+
            //set our color
+
           
+
            switch ((string) colors[x])
+
            {
+
                case "normal": gui.guiText.material.color = normal;
+
                break;
+
                case "warning": gui.guiText.material.color = warning;
+
                break;
+
                case "error": gui.guiText.material.color = error;
+
                break;
+
 
             }
 
             }
             //now set the text for this element
+
 
            gui.guiText.text = (string)messages[x];
+
             // Carry on with display
           
+
            int x = 0;
            //increment and loop
+
            while (x < messages.Count)
            x += 1;
+
            {
         
+
 
        }//end loop
+
                GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
           
+
 
       
+
                //set our color
       
+
                switch ((string)colors[x])
 +
                {
 +
                    case "normal": gui.guiText.material.color = normal;
 +
                        break;
 +
                    case "warning": gui.guiText.material.color = warning;
 +
                        break;
 +
                    case "error": gui.guiText.material.color = error;
 +
                        break;
 +
                }
 +
                //now set the text for this element
 +
                gui.guiText.text = (string)messages[x];
 +
 
 +
                //increment and loop
 +
                x += 1;
 +
 
 +
            }//end loop
 +
 
 +
 
 +
 
 
         }//end display check
 
         }//end display check
   
 
    }
 
  
}// End Class
+
    }
  
 +
}// End Class
 
</csharp>
 
</csharp>

Revision as of 02:49, 10 February 2006

Author: Jeremy Hollingsworth (jeremyace)

Description

This script will control a GUIText object to generate a scrolling console-like debug feeback pane. Allows you to set one of three colors (normal, warning, error) to each line to get more feedback.

Usage

This C# script _must_ be placed in the Standard Assets folder to be called from JavaScript.


UPDATED: You no longer have to set your own GUIText in the scene. This script will create a GUIText at Vector3 defaultGuiPosition with a scale of Vector3 defaultGuiScale. If you wish to provide your own GUIText object to set your own font, etc, then drag your GUIText to the Debug GUI field of the DebugGUIController script.


Create a new blank GameObject (GameObject->Create Empty) and then drag the DebugGUIController.cs script to it. It is now fully usable with the default settings.


Settings:

  • Debug GUI : Drag the guiText you created here if you wish to override the default.
  • defaultGuiPosition : A vector for the starting position of the first line of output. If you provide your own GUIText then it's position overrides this property.
  • defaultGuiScale : A vector for the scale of the individual lines. Again, this is overridden if you provide your own GUIText.
  • The three colors are for normal, warning, and error. Set to whatever you like or leave alone.
  • Max messages : How many message lines we will put on the screen at once. This number depends on the size of your guiText.
  • Line Spacing : A float for how far down we will move each new line.
  • Show On Screen : The default setting for visibility of the debug output. If set to false (unchecked), you can send data but it wont show until you tell it to, if set to true (checked) it will show all debug output until you tell it not to.


Using the Debug script from your scripts:
To call the DebugGUIController script from your scripts, just type:


DebugGUIController.instance.someFunction()


Replace someFunction() with any of the following:


AddMessage(string message, string color)

  • Adds a message to the list. The color is a string either "normal", "warning" or "error". The color argument is optional and if omitted, the color will default to "normal".


ClearMessages()

  • Clears all of the messages from the list and the screen.


Hide()

  • Hides the display of the output. Does not reset the contents of the list (same as setting Show On Screen to false in the inspector)


Show()

  • Shows the previously hidden display.


So for a simple example, to add hello world in warning color (yellow), we type: DebugGUIController.instance.AddMessage("hello world", "warning");

C# - DebugGUIController.cs

The script _must_ be named DebugGUIController.cs <csharp> //------ DebugGUIController.cs ------------------- // Script for handling multi-line, multi-colored // GUI debugging output. // Author: Jeremy Hollingsworth (jeremyace) // Version: 1.0 //------------------------------------------------ using UnityEngine; using System.Collections;


public class DebugGUIController : MonoBehaviour {

   public GameObject DebugGui = null;             // The GUI that will be duplicated
   public Vector3 defaultGuiPosition = new Vector3(0.01F, 0.98F, 0F);
   public Vector3 defaultGuiScale = new Vector3(0.7F, 0.7F, 0F);
   public Color normal = Color.green;
   public Color warning = Color.yellow;
   public Color error = Color.red;
   public int maxMessages = 20;                   // The max number of messages displayed
   public float lineSpacing = 0.02F;              // The amount of space between lines
   public ArrayList messages = new ArrayList();
   public ArrayList names = new ArrayList();
   public ArrayList colors = new ArrayList();
   public bool showOnScreen = true;


   // s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
   private static DebugGUIController s_Instance = null;
   // This defines a static instance property that attempts to find the manager object in the scene and
   // returns it to the caller.
   public static DebugGUIController instance
   {
       get
       {
           if (s_Instance == null)
           {
               s_Instance = FindObjectOfType(typeof(DebugGUIController)) as DebugGUIController;
               if (s_Instance == null)
                   Debug.Log("Could not locate a DebugGUIController object.  You have to have exactly one DebugGUIController in the scene.");
           }
           return s_Instance;
       }
   }
   void Awake()
   {
       s_Instance = this;
       if (DebugGui == null)
       {
           DebugGui = new GameObject();
           DebugGui.AddComponent("GUIText");
           DebugGui.name = "DebugGUI";
           DebugGui.transform.position = defaultGuiPosition;
           DebugGui.transform.localScale = defaultGuiScale;
       }
       // Create our GUI objects to our maxMessages count
       Vector3 position = new Vector3(0.01F, DebugGui.transform.position.y, 0F);
       int x = 0;
       while (x < maxMessages)
       {
           position.y -= lineSpacing;
           Object clone = null;
           clone = Instantiate(DebugGui, position, transform.rotation);
           string name = "DebugGui(" + x + ")";    
           names.Add(name);                        
           clone.name = name;                      
           x += 1;
       }
   }


   //---------- void AddMesage(string message, string color) ------
   //Adds a mesage to the list
   //--------------------------------------------------------------
   public void AddMessage(string message, string color)
   {
       messages.Add(message);
       colors.Add(color);  
       Display();
   }
   //++++++++++ OVERLOAD for AddMessage ++++++++++++++++++++++++++++
   // Overloads AddMessage to only require one argument(message)
   //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   public void AddMessage(string message)
   {
       messages.Add(message);
       colors.Add("normal");
       Display();
   }


   //----------- void ClearMessages() ------------------------------
   // Clears the messages from the screen and the lists
   //---------------------------------------------------------------
   public void ClearMessages()
   {
       messages.Clear();
       colors.Clear();
       ClearScreen();
   }


   //--------- void Hide() -----------------------------------------
   // Hides the display with a variable, but does NOT clear arrays
   //---------------------------------------------------------------
   public void Hide()
   {
       showOnScreen = false;
       ClearScreen();
   }


   //--------- void Show() ----------------------------------------
   // Shows the hidden display on the screen
   //--------------------------------------------------------------
   public void Show()
   {
       showOnScreen = true;
       Display();
   }


   //-------- void ClearScreen() ----------------------------------
   // Clears all output from all GUI objects
   //--------------------------------------------------------------
   void ClearScreen()
   {
       int x = 0;
       while (x < names.Count)
       {
           GameObject gui = GameObject.Find((string)names[x]);   
           gui.guiText.text = "";
           //increment and loop
           x += 1;
       }//end loop
   }


   //---------- void Prune() ---------------------------------------
   // Prunes the array to fit within the maxMessages limit
   //---------------------------------------------------------------
   void Prune()
   {
       int diff;
       if (messages.Count > maxMessages)
       {
           if (messages.Count <= 0)
           {
               diff = 0;
           }
           else
           {
               diff = messages.Count - maxMessages;
           }
           messages.RemoveRange(0, (int)diff);
           colors.RemoveRange(0, (int)diff);
       }
   }
   //---------- void Display() -------------------------------------
   // Displays the list and handles coloring
   //---------------------------------------------------------------
   public void Display()
   {
       //check if we are set to display
       if (showOnScreen == false)
       {
           ClearScreen();
       }
       else
       {
           if (messages.Count > maxMessages)
           {
               Prune();
           }
           // Carry on with display
           int x = 0;
           while (x < messages.Count)
           {
               GameObject gui = GameObject.Find((string)names[x]);   //find our gui object
               //set our color
               switch ((string)colors[x])
               {
                   case "normal": gui.guiText.material.color = normal;
                       break;
                   case "warning": gui.guiText.material.color = warning;
                       break;
                   case "error": gui.guiText.material.color = error;
                       break;
               }
               //now set the text for this element
               gui.guiText.text = (string)messages[x];
               //increment and loop
               x += 1;
           }//end loop


       }//end display check
   }

}// End Class </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox