DebugConsole

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Updated script to newest version and reworked the usage.)
(Updated to version 1.0.1)
Line 10: Line 10:
 
<b>This C# script _must_ be placed in the Standard Assets folder to be called from JavaScript.</b>
 
<b>This C# script _must_ be placed in the Standard Assets folder to be called from JavaScript.</b>
  
<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.
+
<BR>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.
  
 
<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>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>
Line 21: Line 21:
 
* 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.
 
* 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.
+
* Visible : 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><b>Using the Debug script from your scripts:</b>
 
<BR><b>Using the Debug script from your scripts:</b>
Line 36: Line 36:
 
* Clears all of the messages from the list and the screen.
 
* Clears all of the messages from the list and the screen.
  
<BR><b>Hide()</b>
+
<BR><B>To toggle the visibility use:</b>
* 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)
+
<BR><b>isVisible</b>
 +
* The property to set the visiblility of the debug output. Set to either true or false (isVisible = true;).  Does _not_ clear the message list, just hides it. NOTE: The <b>show()</b> and <b>Hide()</b> methods have been replaced with this property.
  
<BR><b>Show()</b>
 
* Shows the previously hidden display.
 
  
 
<BR>So for a simple example, to add hello world in warning color (yellow), we type:
 
<BR>So for a simple example, to add hello world in warning color (yellow), we type:
 
DebugGUIController.instance.AddMessage("hello world", "warning");
 
DebugGUIController.instance.AddMessage("hello world", "warning");
 +
 +
<BR>To hide the output we type:
 +
DebugGUIController.instance.isVisible = false;
  
 
== C# - DebugGUIController.cs ==
 
== C# - DebugGUIController.cs ==
 
The script _must_ be named DebugGUIController.cs
 
The script _must_ be named DebugGUIController.cs
 
<csharp>
 
<csharp>
//------ DebugGUIController.cs -------------------
+
//========== DebugGUIController.cs ================
// Script for handling multi-line, multi-colored
+
// Class for formatting and displaying multi-line,
// GUI debugging output.
+
// user debug messages with individual line
// Author: Jeremy Hollingsworth (jeremyace)
+
// coloring.
// Version: 1.0
+
// Author: Jeremy Hollingsworth
//------------------------------------------------
+
// Version 1.0.1
 +
// Feb-10-2006
 +
//
 +
// USAGE:
 +
// Attach to empty GO then call from any script:
 +
// DebugGUIController.instance.myFunction();
 +
//=================================================
 +
 
 
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 Vector3 defaultGuiPosition = new Vector3(0.01F, 0.98F, 0F);
+
public Vector3 defaultGuiPosition = new Vector3(0.01F, 0.98F, 0F);
    public Vector3 defaultGuiScale = new Vector3(0.7F, 0.7F, 0F);
+
public Vector3 defaultGuiScale = new Vector3 (0.5F, 0.5F, 0F);
    public Color normal = Color.green;
+
public Color normal = Color.green;
    public Color warning = Color.yellow;
+
public Color warning = Color.yellow;  
    public Color error = Color.red;
+
public Color error = Color.red;
    public int maxMessages = 20;                  // The max number of messages displayed
+
public int maxMessages = 30;                  // The max number of messages displayed
    public float lineSpacing = 0.02F;              // The amount of space between lines
+
public float lineSpacing = 0.02F;              // The amount of space between lines
    public ArrayList messages = new ArrayList();
+
public ArrayList messages = new ArrayList();
    public ArrayList names = new ArrayList();
+
public ArrayList names = new ArrayList();     // The GUI names array.  This holds the names of our duplicated GUIs
    public ArrayList colors = new ArrayList();
+
public ArrayList colors = new ArrayList();    // The array that holds the user selection of color for each message. normal, warning, error.
     public bool showOnScreen = true;
+
public bool visible = true;                   // Does output show on screen by default or do we have to enable it with code?
 +
public bool isVisible {                        // Accessor property for visiblility (bool visible)             
 +
  get {
 +
          return visible;
 +
      }
 +
 
 +
  set {
 +
          visible = value;
 +
          if (value == true)
 +
          {
 +
              Display();
 +
          }
 +
          else if (value == false)
 +
          {
 +
              ClearScreen();
 +
          }
 +
      }
 +
}
  
 
+
private static DebugGUIController s_Instance = null;   // Our instance to allow this script to be called without a direct connection.
    // s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
+
public static DebugGUIController instance {
    private static DebugGUIController s_Instance = null;
+
     get {
 
+
         if (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
+
 
         {
 
         {
 +
            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.");
                 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;
 
             return s_Instance;
 
         }
 
         }
 
     }
 
     }
 
+
   
     void Awake()
+
     void Awake() {
    {
+
 
         s_Instance = this;
 
         s_Instance = this;
         if (DebugGui == null)
+
         if (DebugGui == null) // If an external GUIText is not set, provide the default GUIText
 
         {
 
         {
 
             DebugGui = new GameObject();
 
             DebugGui = new GameObject();
            DebugGui.AddComponent("GUIText");
+
        DebugGui.AddComponent("GUIText");
            DebugGui.name = "DebugGUI";
+
        DebugGui.name = "DebugGUI";
            DebugGui.transform.position = defaultGuiPosition;
+
        DebugGui.transform.position = defaultGuiPosition;
            DebugGui.transform.localScale = defaultGuiScale;
+
        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;
+
             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 + ")";     
+
             string name = "DebugGui(" + x + ")";    // Add the current loop number to the DebugGui() name string
             names.Add(name);                         
+
             names.Add(name);                        // add the name to our name array
             clone.name = name;                       
+
             clone.name = name;                      // Set the name of our newly instantiated GUI object.
             x += 1;
+
             x+=1;  
        }
+
        }  
 
+
   
 
     }
 
     }
 
  
  
Line 128: Line 144:
 
     //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 156: Line 171:
 
         ClearScreen();
 
         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() ----------------------------------
 
     //-------- void ClearScreen() ----------------------------------
 
     // Clears all output from all GUI objects
 
     // Clears all output from all GUI objects
Line 187: Line 181:
 
         while (x < names.Count)
 
         while (x < names.Count)
 
         {
 
         {
             GameObject gui = GameObject.Find((string)names[x]);   
+
             GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
 
             gui.guiText.text = "";
 
             gui.guiText.text = "";
 
             //increment and loop
 
             //increment and loop
 
             x += 1;
 
             x += 1;
 
+
         }
         }//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 202: Line 194:
 
     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
 
     //---------------------------------------------------------------
 
     //---------------------------------------------------------------
     public void Display()
+
     void Display()
 
     {
 
     {
 
         //check if we are set to display
 
         //check if we are set to display
         if (showOnScreen == false)
+
         if (visible == false)
 
         {
 
         {
            ClearScreen();
+
          ClearScreen();
 
         }
 
         }
         else
+
         else if (visible == true)
 
         {
 
         {
 
             if (messages.Count > maxMessages)
 
             if (messages.Count > maxMessages)
 
             {
 
             {
                Prune();
+
              Prune();
             }
+
             }          
 
+
       
 
             // Carry on with display
 
             // Carry on with display
 
             int x = 0;
 
             int x = 0;
 
             while (x < messages.Count)
 
             while (x < messages.Count)
 
             {
 
             {
 
 
                 GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
 
                 GameObject gui = GameObject.Find((string)names[x]);  //find our gui object
 
+
           
 
                 //set our color
 
                 //set our color
                 switch ((string)colors[x])
+
                 switch ((string) colors[x])
 
                 {
 
                 {
 
                     case "normal": gui.guiText.material.color = normal;
 
                     case "normal": gui.guiText.material.color = normal;
                        break;
+
                    break;
 
                     case "warning": gui.guiText.material.color = warning;
 
                     case "warning": gui.guiText.material.color = warning;
                        break;
+
                    break;
 
                     case "error": gui.guiText.material.color = error;
 
                     case "error": gui.guiText.material.color = error;
                        break;
+
                    break;
 
                 }
 
                 }
                //now set the text for this element
+
            //now set the text for this element
                gui.guiText.text = (string)messages[x];
+
            gui.guiText.text = (string)messages[x];
 
+
           
                //increment and loop
+
            //increment and loop
                x += 1;
+
            x += 1;
 
+
             }
             }//end loop
+
               
 
+
         }  
 
+
 
+
         }//end display check
+
 
+
 
     }
 
     }
 
+
   
}// End Class
+
   
 +
}// End DebugGUIController Class
 
</csharp>
 
</csharp>

Revision as of 03:10, 14 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.


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.
  • Visible : 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.


To toggle the visibility use:
isVisible

  • The property to set the visiblility of the debug output. Set to either true or false (isVisible = true;). Does _not_ clear the message list, just hides it. NOTE: The show() and Hide() methods have been replaced with this property.



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


To hide the output we type: DebugGUIController.instance.isVisible = false;

C# - DebugGUIController.cs

The script _must_ be named DebugGUIController.cs <csharp> //========== DebugGUIController.cs ================ // Class for formatting and displaying multi-line, // user debug messages with individual line // coloring. // Author: Jeremy Hollingsworth // Version 1.0.1 // Feb-10-2006 // // USAGE: // Attach to empty GO then call from any script: // DebugGUIController.instance.myFunction(); //=================================================

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.5F, 0.5F, 0F); public Color normal = Color.green; public Color warning = Color.yellow; public Color error = Color.red; public int maxMessages = 30; // 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(); // The GUI names array. This holds the names of our duplicated GUIs public ArrayList colors = new ArrayList(); // The array that holds the user selection of color for each message. normal, warning, error. public bool visible = true; // Does output show on screen by default or do we have to enable it with code? public bool isVisible { // Accessor property for visiblility (bool visible)

  get {
          return visible;
      }
  
  set {
          visible = value;
          if (value == true)
          {
              Display();
          }
          else if (value == false)
          {
              ClearScreen();
          }
      }

}

private static DebugGUIController s_Instance = null; // Our instance to allow this script to be called without a direct connection. 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)  // If an external GUIText is not set, provide the default GUIText
       {
           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 + ")";    // Add the current loop number to the DebugGui() name string
           names.Add(name);                        // add the name to our name array
           clone.name = name;                      // Set the name of our newly instantiated GUI object.
           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 ClearScreen() ----------------------------------
   // Clears all output from all GUI objects
   //--------------------------------------------------------------
   void ClearScreen()
   {
       int x = 0;
       while (x < names.Count)
       {
           GameObject gui = GameObject.Find((string)names[x]);   //find our gui object
           gui.guiText.text = "";
           //increment and loop
           x += 1;
       }
   }
   
   
   //---------- 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
   //---------------------------------------------------------------
   void Display()
   {
       //check if we are set to display
       if (visible == false)
       {
          ClearScreen();
       }
       else if (visible == true) 
       {
           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 DebugGUIController Class </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox