DebugConsole

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Updated to version 1.1)
(Updated to 1.2)
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 and is mouse draggable at runtime.
+
This script will create a scrolling debug console.  Allows you to set one of three colors (normal, warning, error) to each line to get more feedback and is mouse draggable at runtime.
  
 
== Usage ==
 
== Usage ==
<b>This C# script _must_ be placed in the Standard Assets folder to be called from JavaScript.</b>
+
Drop the DebugConsole script into your Standard Assets folder. It is now fully usable.
  
<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.
+
If you want to change the default settings (like colors, or change the GUIText object), then create an empty GameObject and drag this script onto it from the standard assets folderYou now have access to all of the default settings in the inspector. The settings are explained below. 
 
+
<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>
Line 24: Line 22:
 
* <b>Visible</b> : 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.
 
* <b>Visible</b> : 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 DebugConsole script from your scripts:</b>
<BR>To call the DebugGUIController script from your scripts, just type:
+
<BR>To call the DebugConsole script from your scripts, just type:
  
<BR>DebugGUIController.instance.<i><b>someFunction()</b></i>
+
<BR><b>DebugConsole</b>.<i>someFunction()</i>
  
<BR>Replace <i><b>someFunction()</b></i> with any of the following:
+
<BR>Replace <i>someFunction()</i> with any of the following:
  
<BR><b>AddMessage</b>(<b>string</b> <i>message</i>, <b>string</b> <i>color</i>)
+
<BR><b>Log</b> (<b>string</b> <i>message</i>, <b>string</b> <i>color</i>)
 
* Adds a message to the list.  The color is a string either <i>"normal", "warning" or "error"</i>.  The color argument is optional and if omitted, the color will default to "normal".
 
* Adds a message to the list.  The color is a string either <i>"normal", "warning" or "error"</i>.  The color argument is optional and if omitted, the color will default to "normal".
  
<BR><b>ClearMessages()</b>
+
<BR><b>Clear()</b>
 
* Clears all of the messages from the list and the screen.
 
* Clears all of the messages from the list and the screen.
  
 
<BR><B>To toggle the visibility use:</b>
 
<BR><B>To toggle the visibility use:</b>
<BR><b>isVisible</b>
+
<BR><b>isVisible</b> (true, false)
*  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.
+
*  The property to set the visiblility of the debug output. This property _not_ clear the message list, just hides it. You need to use the Clear() method to clear the list.
  
 +
<BR><B>To toggle the mouse drag functionality use:</b>
 +
<BR><b>isDraggable</b> (true, false)
  
 
<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");
+
DebugConsole.Log("hello world", "warning");
  
 
<BR>To hide the output we type:
 
<BR>To hide the output we type:
DebugGUIController.instance.isVisible = false;
+
DebugConsole.isVisible = false;
  
== C# - DebugGUIController.cs ==
+
== C# - DebugConsole.cs ==
The script _must_ be named DebugGUIController.cs
+
The script _must_ be named DebugConsole.cs
 
<csharp>
 
<csharp>
//========== DebugGUIController.cs ================
+
/*==== DebugConsole.cs ====================================================
// Class for formatting and displaying multi-line,
+
* Class for handling multi-line, multi-color debugging messages.
// user debug messages with individual line
+
* Author: Jeremy Hollingsworth
// coloring.
+
* Version 1.2
// Author: Jeremy Hollingsworth
+
* Mar 02, 2006
// Version 1.1
+
*
// Feb-20-2006
+
* USAGE:
//
+
* ::Drop in your standard assets folder (if you want to change any of the
// USAGE:
+
* default settings in the inspector, create an empty GameObject and attach
// Attach to empty GO then call from any script:
+
* this script to it from you standard assets folder.  That will provide
// DebugGUIController.instance.myFunction();
+
* access to the default settings in the inspector)
//=================================================
+
*
 +
* ::To use, call DebugConsole.functionOrProperty() where
 +
* functionOrProperty = one of the following:
 +
*
 +
* -Log(string message, string color)  Adds "message" to the list with the
 +
* "color" color. Color is optional and can be any of the following: "error",
 +
* "warning", or "normal". Default is normal.
 +
*
 +
* Clear() Clears all messages
 +
*
 +
* isVisible (true,false)  Toggles the visibility of the output.  Does _not_
 +
* clear the messages.
 +
*
 +
* isDraggable (true, false)  Toggles mouse drag functionality
 +
* =========================================================================*/
 +
  
 
using UnityEngine;
 
using UnityEngine;
Line 68: Line 83:
  
  
public class DebugGUIController : MonoBehaviour {
+
public class DebugConsole : 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.5F, 0.5F, 1F);
+
    public Vector3 defaultGuiScale = new Vector3(0.5F, 0.5F, 1F);
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 = 30;                  // 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 guis = new ArrayList();    
+
    public ArrayList guis = new ArrayList();
public ArrayList colors = new ArrayList();    
+
    public ArrayList colors = new ArrayList();
public bool draggable = true;                  // Can the output be dragged around at runtime?  
+
    public bool draggable = true;                  // Can the output be dragged around at runtime by default?  
public bool visible = true;                    // Does output show on screen by default or do we have to enable it with code?  
+
    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)             
+
    public static bool isVisible
  get {
+
    {                                    
          return visible;
+
        get
      }
+
        {
 
+
            return DebugConsole.instance.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.
+
         set
public static DebugGUIController instance {
+
    get {
+
         if (s_Instance == null)
+
 
         {
 
         {
             s_Instance = FindObjectOfType(typeof (DebugGUIController)) as DebugGUIController;
+
             DebugConsole.instance.visible = value;
             if (s_Instance == null)
+
             if (value == true)
                 Debug.Log ("Could not locate a DebugGUIController object.  You have to have exactly one DebugGUIController in the scene.");
+
            {
 +
                 DebugConsole.instance.Display();
 
             }
 
             }
 +
            else if (value == false)
 +
            {
 +
                DebugConsole.instance.ClearScreen();
 +
            }
 +
        }
 +
    }
 +
 +
    public static bool isDraggable
 +
    {                                     
 +
        get
 +
        {
 +
            return DebugConsole.instance.draggable;
 +
        }
 +
 +
        set
 +
        {
 +
            DebugConsole.instance.draggable = value;
 
              
 
              
 +
        }
 +
    }
 +
 +
 +
    private static DebugConsole s_Instance = null;  // Our instance to allow this script to be called without a direct connection.
 +
    public static DebugConsole instance
 +
    {
 +
        get
 +
        {
 +
            if (s_Instance == null)
 +
            {
 +
                s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
 +
                if (s_Instance == null)
 +
                {
 +
                    GameObject console = new GameObject();
 +
                    console.AddComponent("DebugConsole");
 +
                    console.name = "DebugConsoleController";
 +
                    s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
 +
                }
 +
                   
 +
            }
 +
 
             return s_Instance;
 
             return s_Instance;
 
         }
 
         }
 
     }
 
     }
   
+
 
     void Awake() {
+
     void Awake()
 +
    {
 
         s_Instance = this;
 
         s_Instance = this;
 
         if (DebugGui == null)  // If an external GUIText is not set, provide the default GUIText
 
         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(0)";
+
            DebugGui.name = "DebugGUI(0)";
        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 = DebugGui.transform.position;
+
        Vector3 position = DebugGui.transform.position;
        guis.Add(DebugGui);
+
        guis.Add(DebugGui);
        int x = 1;
+
        int x = 1;
        while(x < maxMessages)
+
        while (x < maxMessages)
        {
+
        {
             position.y -= lineSpacing;
+
             position.y -= lineSpacing;
 
             GameObject clone = null;
 
             GameObject clone = null;
 
             clone = (GameObject)Instantiate(DebugGui, position, transform.rotation);
 
             clone = (GameObject)Instantiate(DebugGui, position, transform.rotation);
             clone.name = string.Format("DebugGUI({0})", x);    // Add the current loop number to the DebugGui() name string
+
             clone.name = string.Format("DebugGUI({0})", x);     
             guis.Add(clone);                                  // add the gui object to our guis array
+
             guis.Add(clone);                                   
 
             position = clone.transform.position;
 
             position = clone.transform.position;
             x+=1;  
+
             x += 1;
        }
+
        }
 +
 
 +
        x = 0;
 +
        while (x < guis.Count)
 +
        {
 +
            GameObject temp = (GameObject)guis[x];
 +
            temp.transform.parent = DebugGui.transform;
 +
            x++;
 +
        }
 +
 
  
        x = 0;
 
        while (x < guis.Count)
 
        {
 
            GameObject temp = (GameObject)guis[x];
 
            temp.transform.parent = DebugGui.transform;
 
            x++;
 
        }
 
       
 
         
 
 
     }
 
     }
   
+
 
     bool connectedToMouse = false;  //is the gui is attached to the mouse?
+
     bool connectedToMouse = false;   
 
     void Update()
 
     void Update()
 
     {
 
     {
Line 178: Line 221:
 
                 DebugGui.transform.position = new Vector3(posX, posY, 0F);
 
                 DebugGui.transform.position = new Vector3(posX, posY, 0F);
 
             }
 
             }
         }  
+
         }
      
+
 
 +
     }
 +
    //+++++++++ INTERFACE FUNCTIONS ++++++++++++++++++++++++++++++++
 +
    public static void Log(string message, string color)
 +
    {
 +
        DebugConsole.instance.AddMessage(message, color);
 +
    }
 +
    //++++ OVERLOAD ++++
 +
    public static void Log(string message)
 +
    {
 +
        DebugConsole.instance.AddMessage(message);
 
     }
 
     }
 
+
 
+
    public static void Clear()
 +
    {
 +
        DebugConsole.instance.ClearMessages();
 +
    }
 +
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +
 
  
 
     //---------- void AddMesage(string message, string color) ------
 
     //---------- void AddMesage(string message, string color) ------
 
     //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 ++++++++++++++++++++++++++++
+
    //++++++++++ OVERLOAD for AddMessage ++++++++++++++++++++++++++++
// Overloads AddMessage to only require one argument(message)
+
    // Overloads AddMessage to only require one argument(message)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void AddMessage(string message)
+
    public void AddMessage(string message)
{
+
    {
  messages.Add(message);
+
        messages.Add(message);
  colors.Add("normal");
+
        colors.Add("normal");
  Display();
+
        Display();
 
     }
 
     }
  
+
 
 
     //----------- void ClearMessages() ------------------------------
 
     //----------- void ClearMessages() ------------------------------
 
     // Clears the messages from the screen and the lists
 
     // Clears the messages from the screen and the lists
Line 214: Line 272:
 
         ClearScreen();
 
         ClearScreen();
 
     }
 
     }
   
+
 
   
+
 
 
     //-------- void ClearScreen() ----------------------------------
 
     //-------- void ClearScreen() ----------------------------------
 
     // Clears all output from all GUI objects
 
     // Clears all output from all GUI objects
Line 221: Line 279:
 
     void ClearScreen()
 
     void ClearScreen()
 
     {
 
     {
         int x = 0;
+
         if (guis.Count < maxMessages)
        while (x < guis.Count)
+
 
         {
 
         {
             GameObject gui = (GameObject)guis[x];  //find our gui object
+
             //do nothing as we haven't created our guis yet
            gui.guiText.text = "";
+
            //increment and loop
+
            x+=1;
+
 
         }
 
         }
    }
+
        else
   
+
        {
      
+
            int x = 0;
 +
            while (x < guis.Count)
 +
            {
 +
                GameObject gui = (GameObject)guis[x]; 
 +
                gui.guiText.text = "";
 +
                //increment and loop
 +
                x += 1;
 +
            }
 +
        }
 +
    
 +
 
 +
 
 
     //---------- void Prune() ---------------------------------------
 
     //---------- void Prune() ---------------------------------------
 
     // Prunes the array to fit within the maxMessages limit
 
     // Prunes the array to fit within the maxMessages limit
Line 237: Line 302:
 
     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 263: Line 328:
 
         if (visible == false)
 
         if (visible == false)
 
         {
 
         {
          ClearScreen();
+
            ClearScreen();
 
         }
 
         }
         else if (visible == true)  
+
         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)
+
             if (guis.Count < maxMessages)
 
             {
 
             {
                 GameObject gui = (GameObject)guis[x];  //find our gui object
+
                 //do nothing as we havent created our guis yet
              
+
             }
                //set our color
+
            else
                 switch ((string) colors[x])
+
            {
 +
                 while (x < messages.Count)
 
                 {
 
                 {
                     case "normal": gui.guiText.material.color = normal;
+
                     GameObject gui = (GameObject)guis[x]; 
                    break;
+
               
                    case "warning": gui.guiText.material.color = warning;
+
                    //set our color
                    break;
+
                    switch ((string)colors[x])
                    case "error": gui.guiText.material.color = error;
+
                    {
                    break;
+
                        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;
 
                 }
 
                 }
            //now set the text for this element
 
            gui.guiText.text = (string)messages[x];
 
           
 
            //increment and loop
 
            x += 1;
 
 
             }
 
             }
               
+
 
         }  
+
         }
 
     }
 
     }
   
+
 
   
+
 
}// End DebugGUIController Class
+
}// End DebugConsole Class
 
</csharp>
 
</csharp>

Revision as of 23:32, 2 March 2006

Author: Jeremy Hollingsworth (jeremyace)

Description

This script will create a scrolling debug console. Allows you to set one of three colors (normal, warning, error) to each line to get more feedback and is mouse draggable at runtime.

Usage

Drop the DebugConsole script into your Standard Assets folder. It is now fully usable.

If you want to change the default settings (like colors, or change the GUIText object), then create an empty GameObject and drag this script onto it from the standard assets folder. You now have access to all of the default settings in the inspector. The settings are explained below.


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.
  • Line Spacing : A float for how far down we will move each new line.
  • Draggable : True means you can drag the output around with the mouse at runtime, false means position is locked from mouse input. To drag output at runtime, click once on the uppermost output line to connect it to the mouse, then move to where you want the output displayed and click the mouse again to release it.
  • 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 DebugConsole script from your scripts:
To call the DebugConsole script from your scripts, just type:


DebugConsole.someFunction()


Replace someFunction() with any of the following:


Log (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".


Clear()

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


To toggle the visibility use:
isVisible (true, false)

  • The property to set the visiblility of the debug output. This property _not_ clear the message list, just hides it. You need to use the Clear() method to clear the list.


To toggle the mouse drag functionality use:
isDraggable (true, false)


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


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

C# - DebugConsole.cs

The script _must_ be named DebugConsole.cs <csharp> /*==== DebugConsole.cs ====================================================

* Class for handling multi-line, multi-color debugging messages.
* Author: Jeremy Hollingsworth
* Version 1.2
* Mar 02, 2006
* 
* USAGE:
* ::Drop in your standard assets folder (if you want to change any of the
* default settings in the inspector, create an empty GameObject and attach
* this script to it from you standard assets folder.  That will provide
* access to the default settings in the inspector)
* 
* ::To use, call DebugConsole.functionOrProperty() where 
* functionOrProperty = one of the following:
* 
* -Log(string message, string color)  Adds "message" to the list with the
* "color" color. Color is optional and can be any of the following: "error",
* "warning", or "normal".  Default is normal.
* 
* Clear() Clears all messages
* 
* isVisible (true,false)  Toggles the visibility of the output.  Does _not_
* clear the messages.
* 
* isDraggable (true, false)  Toggles mouse drag functionality
* =========================================================================*/

using UnityEngine; using System.Collections;


public class DebugConsole : 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, 1F);
   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 guis = new ArrayList();
   public ArrayList colors = new ArrayList();
   public bool draggable = true;                  // Can the output be dragged around at runtime by default? 
   public bool visible = true;                    // Does output show on screen by default or do we have to enable it with code? 
   public static bool isVisible
   {                                      
       get
       {
           return DebugConsole.instance.visible;
       }
       set
       {
           DebugConsole.instance.visible = value;
           if (value == true)
           {
               DebugConsole.instance.Display();
           }
           else if (value == false)
           {
               DebugConsole.instance.ClearScreen();
           }
       }
   }
   public static bool isDraggable
   {                                      
       get
       {
           return DebugConsole.instance.draggable;
       }
       set
       {
           DebugConsole.instance.draggable = value;
           
       }
   }


   private static DebugConsole s_Instance = null;   // Our instance to allow this script to be called without a direct connection.
   public static DebugConsole instance
   {
       get
       {
           if (s_Instance == null)
           {
               s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
               if (s_Instance == null)
               {
                   GameObject console = new GameObject();
                   console.AddComponent("DebugConsole");
                   console.name = "DebugConsoleController";
                   s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
               }
                   
           }
           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(0)";
           DebugGui.transform.position = defaultGuiPosition;
           DebugGui.transform.localScale = defaultGuiScale;
       }
       // Create our GUI objects to our maxMessages count
       Vector3 position = DebugGui.transform.position;
       guis.Add(DebugGui);
       int x = 1;
       while (x < maxMessages)
       {
           position.y -= lineSpacing;
           GameObject clone = null;
           clone = (GameObject)Instantiate(DebugGui, position, transform.rotation);
           clone.name = string.Format("DebugGUI({0})", x);    
           guis.Add(clone);                                   
           position = clone.transform.position;
           x += 1;
       }
       x = 0;
       while (x < guis.Count)
       {
           GameObject temp = (GameObject)guis[x];
           temp.transform.parent = DebugGui.transform;
           x++;
       }


   }
   bool connectedToMouse = false;  
   void Update()
   {
       if (draggable == true)
       {
           if (Input.GetMouseButtonDown(0))
           {
               if (connectedToMouse == false && DebugGui.guiText.HitTest((Vector3)Input.mousePosition) == true)
               {
                   connectedToMouse = true;
               }
               else if (connectedToMouse == true)
               {
                   connectedToMouse = false;
               }
           }
           if (connectedToMouse == true)
           {
               float posX = DebugGui.transform.position.x;
               float posY = DebugGui.transform.position.y;
               posX = Input.mousePosition.x / Screen.width;
               posY = Input.mousePosition.y / Screen.height;
               DebugGui.transform.position = new Vector3(posX, posY, 0F);
           }
       }
   }
   //+++++++++ INTERFACE FUNCTIONS ++++++++++++++++++++++++++++++++
   public static void Log(string message, string color)
   {
       DebugConsole.instance.AddMessage(message, color);
   }
   //++++ OVERLOAD ++++
   public static void Log(string message)
   {
       DebugConsole.instance.AddMessage(message);
   }
   public static void Clear()
   {
       DebugConsole.instance.ClearMessages();
   }
   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


   //---------- 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()
   {
       if (guis.Count < maxMessages)
       {
           //do nothing as we haven't created our guis yet
       }
       else
       {
           int x = 0;
           while (x < guis.Count)
           {
               GameObject gui = (GameObject)guis[x];   
               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;
           if (guis.Count < maxMessages)
           {
               //do nothing as we havent created our guis yet
           }
           else
           {
               while (x < messages.Count)
               {
                   GameObject gui = (GameObject)guis[x];   
               
                   //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 DebugConsole Class </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox