Unity 输出调试信息到界面
http://blog.****.net/abcdtty/article/details/18862265
Unity里自带的Debug输出信息的功能非常好用, 但是在实机上不那么好用了, 不能直观的看见输出的信息. 这时就使用把信息输出到界面上的方法. 特别是给非开发人员看的时候就非常方便了,下面这样:
绘制方式用的是自带的GUI, 使用时首先把脚本拖到对象, 然后设置GUI Style :
然后在代码中这样使用就可以了.
- void Update()
- {
- MyDebug.Add("MyDebug", "ver 2014-1-29 10:21");
- MyDebug.Add("XXX", 0.1F);
- }
源代码:
- /// <summary>
- /// 用于在屏幕上输出调试信息.
- /// 版本 : 2014-1-29 10:21
- /// </summary>
- public class MyDebug : MonoBehaviour
- {
- static List<string> messages = new List<string>();
- static List<string> names = new List<string>();
- public GUIStyle style = null;
- public Rect rect;
- public float IntervalSize = 16;
- //绘制持续时间(秒)
- public float ClearTime = 1;
- float nowTime = 0;
- void Start()
- {
- }
- void Update()
- {
- if(nowTime < ClearTime)
- nowTime+=Time.deltaTime;
- else
- {
- messages.Clear();
- names.Clear();
- nowTime = 0;
- }
- }
- void OnGUI()
- {
- Display();
- }
- void Display()
- {
- for(int i=0;i<names.Count;i++)
- {
- GUI.Box(new Rect(0,i*IntervalSize,rect.width,rect.height),
- names[i] +" : "+messages[i],style);
- }
- }
- public static void Add(string name, string message)
- {
- if(names.Contains(name) == false)
- {
- names.Add(name);
- messages.Add(message);
- }
- else
- {
- for(int i=0;i<names.Count;i++)
- {
- if(names[i] == name)
- {
- messages[i] = message;
- break;
- }
- }
- }
- }
- public static void Add(string name, object mess)
- {
- string message = mess.ToString();
- Add(name,message);
- }
- public static void Add(string name, bool mess)
- {
- string message;
- if(mess == true)
- message = mess.ToString()+"~~~~~~~";
- else
- message = mess.ToString()+".....";
- Add(name,message);
- }
- }