Unity 输出调试信息到界面

来源:互联网 发布:淘宝竹鸡打笼 编辑:程序博客网 时间:2024/06/05 14:21

http://blog.csdn.net/abcdtty/article/details/18862265

Unity里自带的Debug输出信息的功能非常好用, 但是在实机上不那么好用了, 不能直观的看见输出的信息. 这时就使用把信息输出到界面上的方法. 特别是给非开发人员看的时候就非常方便了,下面这样:


绘制方式用的是自带的GUI, 使用时首先把脚本拖到对象, 然后设置GUI Style :


然后在代码中这样使用就可以了.

[csharp] view plain copy
  1. void Update()  
  2. {  
  3.     MyDebug.Add("MyDebug""ver 2014-1-29 10:21");  
  4.     MyDebug.Add("XXX", 0.1F);  
  5. }  

源代码:

[csharp] view plain copy
  1. /// <summary>  
  2. /// 用于在屏幕上输出调试信息.  
  3. /// 版本 : 2014-1-29 10:21  
  4. /// </summary>  
  5. public  class MyDebug : MonoBehaviour  
  6. {  
  7.     static List<string> messages = new List<string>();  
  8.     static List<string> names = new List<string>();  
  9.       
  10.     public GUIStyle style = null;  
  11.     public Rect rect;  
  12.       
  13.     public float IntervalSize = 16;  
  14.     //绘制持续时间(秒)  
  15.     public float ClearTime = 1;  
  16.     float nowTime = 0;  
  17.       
  18.     void Start()  
  19.     {   
  20.     }  
  21.       
  22.     void Update()  
  23.     {  
  24.         if(nowTime < ClearTime)  
  25.            nowTime+=Time.deltaTime;  
  26.         else  
  27.         {  
  28.             messages.Clear();  
  29.             names.Clear();  
  30.             nowTime = 0;  
  31.         }     
  32.     }  
  33.       
  34.     void OnGUI()  
  35.     {  
  36.         Display();  
  37.     }  
  38.       
  39.     void Display()  
  40.     {  
  41.         for(int i=0;i<names.Count;i++)  
  42.         {  
  43.             GUI.Box(new Rect(0,i*IntervalSize,rect.width,rect.height),  
  44.                 names[i] +" : "+messages[i],style);  
  45.         }  
  46.           
  47.     }  
  48.       
  49.     public static void Add(string name, string message)  
  50.     {  
  51.         if(names.Contains(name) == false)  
  52.         {  
  53.             names.Add(name);  
  54.             messages.Add(message);  
  55.         }  
  56.         else  
  57.         {  
  58.             for(int i=0;i<names.Count;i++)  
  59.             {  
  60.                 if(names[i] == name)  
  61.                 {  
  62.                     messages[i] = message;  
  63.                     break;  
  64.                 }  
  65.             }  
  66.           
  67.         }  
  68.     }  
  69.       
  70.     public static void Add(string name, object mess)  
  71.     {  
  72.         string message = mess.ToString();  
  73.         Add(name,message);  
  74.     }  
  75.       
  76.     public static void Add(string name, bool mess)  
  77.     {  
  78.         string message;  
  79.           
  80.         if(mess == true)  
  81.             message = mess.ToString()+"~~~~~~~";  
  82.         else  
  83.             message = mess.ToString()+".....";  
  84.           
  85.         Add(name,message);  
  86.     }  
  87.     
  88. }  


原创粉丝点击