Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I display the equivalent of Debug.Log("move Right" + speed) on Screen in C#

Discussion in 'Immediate Mode GUI (IMGUI)' started by hollywoodcolor, Sep 22, 2014.

  1. hollywoodcolor

    hollywoodcolor

    Joined:
    Jul 12, 2014
    Posts:
    35
    I just need a simple bullet proof way to see values on a mobile device

    everything I find is way to may step so it doesn't if you just drop it in.
    or it in Java.
     
  2. zugsoft

    zugsoft

    Joined:
    Apr 23, 2014
    Posts:
    453
    Code (CSharp):
    1. /*==== DebugConsole.cs ====================================================
    2. * Class for handling multi-line, multi-color debugging messages.
    3. * Original Author: Jeremy Hollingsworth
    4. * Based On: Version 1.2.1 Mar 02, 2006
    5. *
    6. * Modified: Simon Waite
    7. * Date: 22 Feb 2007
    8. *
    9. * Modification to original script to allow pixel-correct line spacing
    10. *
    11. * Setting the boolean pixelCorrect changes the units in lineSpacing property
    12. * to pixels, so you have a pixel correct gui font in your console.
    13. *
    14. * It also checks every frame if the screen is resized to make sure the
    15. * line spacing is correct (To see this; drag and let go in the editor
    16. * and the text spacing will snap back)
    17. *
    18. * USAGE:
    19. * ::Drop in your standard assets folder (if you want to change any of the
    20. * default settings in the inspector, create an empty GameObject and attach
    21. * this script to it from you standard assets folder.  That will provide
    22. * access to the default settings in the inspector)
    23. *
    24. * ::To use, call DebugConsole.functionOrProperty() where
    25. * functionOrProperty = one of the following:
    26. *
    27. * -Log(string message, string color)  Adds "message" to the list with the
    28. * "color" color. Color is optional and can be any of the following: "error",
    29. * "warning", or "normal".  Default is normal.
    30. *
    31. * Clear() Clears all messages
    32. *
    33. * isVisible (true,false)  Toggles the visibility of the output.  Does _not_
    34. * clear the messages.
    35. *
    36. * isDraggable (true, false)  Toggles mouse drag functionality
    37. * =========================================================================*/
    38.  
    39.  
    40. using UnityEngine;
    41. using System.Collections;
    42.  
    43.  
    44. public class DebugConsole : MonoBehaviour
    45. {
    46.     public GameObject DebugGui = null;             // The GUI that will be duplicated
    47.     public Vector3 defaultGuiPosition = new Vector3(0.01F, 0.98F, 10F);
    48.     public Vector3 defaultGuiScale = new Vector3(0.5F, 0.5F, 1F);
    49.     public Color normal = Color.white;
    50.     public Color warning = Color.yellow;
    51.     public Color error = Color.red;
    52.     public int maxMessages = 30;                   // The max number of messages displayed
    53.     public float lineSpacing = 0.02F;              // The amount of space between lines
    54.     public ArrayList messages = new ArrayList();
    55.     public ArrayList guis = new ArrayList();
    56.     public ArrayList colors = new ArrayList();
    57.     public bool draggable = true;                  // Can the output be dragged around at runtime by default?
    58.     public bool visible = true;                    // Does output show on screen by default or do we have to enable it with code?
    59.     public bool pixelCorrect = false; // set to be pixel Correct linespacing
    60.     public static bool isVisible
    61.     {                                    
    62.         get
    63.         {
    64.             return DebugConsole.instance.visible;
    65.         }
    66.        
    67.         set
    68.         {
    69.             DebugConsole.instance.visible = value;
    70.             if (value == true)
    71.             {
    72.                 DebugConsole.instance.Display();
    73.             }
    74.             else if (value == false)
    75.             {
    76.                 DebugConsole.instance.ClearScreen();
    77.             }
    78.         }
    79.     }
    80.    
    81.     public static bool isDraggable
    82.     {                                    
    83.         get
    84.         {
    85.             return DebugConsole.instance.draggable;
    86.         }
    87.        
    88.         set
    89.         {
    90.             DebugConsole.instance.draggable = value;
    91.            
    92.         }
    93.     }
    94.    
    95.    
    96.     private static DebugConsole s_Instance = null;   // Our instance to allow this script to be called without a direct connection.
    97.     public static DebugConsole instance
    98.     {
    99.         get
    100.         {
    101.             if (s_Instance == null)
    102.             {
    103.                 s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
    104.                 if (s_Instance == null)
    105.                 {
    106.                     GameObject console = new GameObject();
    107.                     console.AddComponent("DebugConsole");
    108.                     console.name = "DebugConsoleController";
    109.                     s_Instance = FindObjectOfType(typeof(DebugConsole)) as DebugConsole;
    110.                     DebugConsole.instance.InitGuis();
    111.                 }
    112.                
    113.             }
    114.            
    115.             return s_Instance;
    116.         }
    117.     }
    118.    
    119.     void Awake()
    120.     {
    121.         s_Instance = this;
    122.         InitGuis();
    123.        
    124.     }
    125.    
    126.     protected bool guisCreated = false;
    127.     protected float screenHeight =-1;
    128.     public void InitGuis()
    129.     {
    130.         float usedLineSpacing = lineSpacing;
    131.         screenHeight = Screen.height;
    132.         if(pixelCorrect)
    133.             usedLineSpacing = 1.0F / screenHeight * usedLineSpacing;
    134.        
    135.         if (guisCreated == false)
    136.         {
    137.             if (DebugGui == null)  // If an external GUIText is not set, provide the default GUIText
    138.             {
    139.                 DebugGui = new GameObject();
    140.                 DebugGui.AddComponent("GUIText");
    141.                 DebugGui.name = "DebugGUI(0)";
    142.                 DebugGui.transform.position = defaultGuiPosition;
    143.                 DebugGui.transform.localScale = defaultGuiScale;
    144.             }
    145.            
    146.             // Create our GUI objects to our maxMessages count
    147.             Vector3 position = DebugGui.transform.position;
    148.             guis.Add(DebugGui);
    149.             int x = 1;
    150.            
    151.             while (x < maxMessages)
    152.             {
    153.                 position.y -= usedLineSpacing;
    154.                 GameObject clone = null;
    155.                 clone = (GameObject)Instantiate(DebugGui, position, transform.rotation);
    156.                 clone.name = string.Format("DebugGUI({0})", x);
    157.                 guis.Add(clone);
    158.                 position = clone.transform.position;
    159.                 position.z=10;
    160.                 x += 1;
    161.             }
    162.            
    163.             x = 0;
    164.             while (x < guis.Count)
    165.             {
    166.                 GameObject temp = (GameObject)guis[x];
    167.                 temp.transform.parent = DebugGui.transform;
    168.                 x++;
    169.             }
    170.             guisCreated = true;
    171.         } else {
    172.             // we're called on a screensize change, so fiddle with sizes
    173.             Vector3 position = DebugGui.transform.position;
    174.             for(int x=0;x < guis.Count; x++)
    175.             {
    176.                 position.y -= usedLineSpacing;
    177.                 GameObject temp = (GameObject)guis[x];
    178.                 temp.transform.position= position;
    179.             }      
    180.         }
    181.     }
    182.    
    183.    
    184.    
    185.     bool connectedToMouse = false;
    186.     void Update()
    187.     {
    188.         // If we are visible and the screenHeight has changed, reset linespacing
    189.         if (visible == true && screenHeight != Screen.height)
    190.         {
    191.             InitGuis();
    192.         }
    193.         if (draggable == true)
    194.         {
    195.             if (Input.GetMouseButtonDown(0))
    196.             {
    197.                 if (connectedToMouse == false && DebugGui.guiText.HitTest((Vector3)Input.mousePosition) == true)
    198.                 {
    199.                     connectedToMouse = true;
    200.                 }
    201.                 else if (connectedToMouse == true)
    202.                 {
    203.                     connectedToMouse = false;
    204.                 }
    205.                
    206.             }
    207.            
    208.             if (connectedToMouse == true)
    209.             {
    210.                 float posX = DebugGui.transform.position.x;
    211.                 float posY = DebugGui.transform.position.y;
    212.                 posX = Input.mousePosition.x / Screen.width;
    213.                 posY = Input.mousePosition.y / Screen.height;
    214.                 DebugGui.transform.position = new Vector3(posX, posY, 10);
    215.             }
    216.         }
    217.        
    218.     }
    219.     //+++++++++ INTERFACE FUNCTIONS ++++++++++++++++++++++++++++++++
    220.     public static void Log(string message, string color)
    221.     {
    222.         DebugConsole.instance.AddMessage(message, color);
    223.        
    224.     }
    225.     //++++ OVERLOAD ++++
    226.     public static void Log(string message)
    227.     {
    228.         DebugConsole.instance.AddMessage(message);
    229.     }
    230.    
    231.     public static void Clear()
    232.     {
    233.         DebugConsole.instance.ClearMessages();
    234.     }
    235.     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    236.    
    237.    
    238.     //---------- void AddMesage(string message, string color) ------
    239.     //Adds a mesage to the list
    240.     //--------------------------------------------------------------
    241.    
    242.     public void AddMessage(string message, string color)
    243.     {
    244.         messages.Add(message);
    245.         colors.Add(color);
    246.         Display();
    247.     }
    248.     //++++++++++ OVERLOAD for AddMessage ++++++++++++++++++++++++++++
    249.     // Overloads AddMessage to only require one argument(message)
    250.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    251.     public void AddMessage(string message)
    252.     {
    253.         messages.Add(message);
    254.         colors.Add("normal");
    255.         Display();
    256.     }
    257.    
    258.    
    259.     //----------- void ClearMessages() ------------------------------
    260.     // Clears the messages from the screen and the lists
    261.     //---------------------------------------------------------------
    262.     public void ClearMessages()
    263.     {
    264.         messages.Clear();
    265.         colors.Clear();
    266.         ClearScreen();
    267.     }
    268.    
    269.    
    270.     //-------- void ClearScreen() ----------------------------------
    271.     // Clears all output from all GUI objects
    272.     //--------------------------------------------------------------
    273.     void ClearScreen()
    274.     {
    275.         if (guis.Count < maxMessages)
    276.         {
    277.             //do nothing as we haven't created our guis yet
    278.         }
    279.         else
    280.         {
    281.             int x = 0;
    282.             while (x < guis.Count)
    283.             {
    284.                 GameObject gui = (GameObject)guis[x];  
    285.                 gui.guiText.text = "";
    286.                 //increment and loop
    287.                 x += 1;
    288.             }
    289.         }
    290.     }  
    291.    
    292.    
    293.     //---------- void Prune() ---------------------------------------
    294.     // Prunes the array to fit within the maxMessages limit
    295.     //---------------------------------------------------------------
    296.     void Prune()
    297.     {
    298.         int diff;
    299.         if (messages.Count > maxMessages)
    300.         {
    301.             if (messages.Count <= 0)
    302.             {
    303.                 diff = 0;
    304.             }
    305.             else
    306.             {
    307.                 diff = messages.Count - maxMessages;
    308.             }
    309.             messages.RemoveRange(0, (int)diff);
    310.             colors.RemoveRange(0, (int)diff);
    311.         }
    312.        
    313.     }
    314.    
    315.     //---------- void Display() -------------------------------------
    316.     // Displays the list and handles coloring
    317.     //---------------------------------------------------------------
    318.     void Display()
    319.     {
    320.         //check if we are set to display
    321.         if (visible == false)
    322.         {
    323.             ClearScreen();
    324.         }
    325.         else if (visible == true)
    326.         {
    327.            
    328.            
    329.             if (messages.Count > maxMessages)
    330.             {
    331.                 Prune();
    332.             }
    333.            
    334.             // Carry on with display
    335.             int x = 0;
    336.             if (guis.Count < maxMessages)
    337.             {
    338.                 //do nothing as we havent created our guis yet
    339.             }
    340.             else
    341.             {
    342.                 while (x < messages.Count)
    343.                 {
    344.                     GameObject gui = (GameObject)guis[x];  
    345.                    
    346.                     //set our color
    347.                     switch ((string)colors[x])
    348.                     {
    349.                     case "normal": gui.guiText.material.color = normal;
    350.                         break;
    351.                     case "warning": gui.guiText.material.color = warning;
    352.                         break;
    353.                     case "error": gui.guiText.material.color = error;
    354.                         break;
    355.                     }
    356. //                    gui.transform.position.z=10;
    357.                    
    358.                     //now set the text for this element
    359.                     gui.guiText.text = (string)messages[x];
    360.                    
    361.                     //increment and loop
    362.                     x += 1;
    363.                 }
    364.             }
    365.            
    366.         }
    367.     }
    368.    
    369.    
    370. }// End DebugConsole Class
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    What type of variable is speed?