NGUIDebug.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Internal/Debug")]
  5. public class NGUIDebug : MonoBehaviour
  6. {
  7. public static bool debugRaycast
  8. {
  9. get
  10. {
  11. return NGUIDebug.mRayDebug;
  12. }
  13. set
  14. {
  15. if (Application.isPlaying)
  16. {
  17. NGUIDebug.mRayDebug = value;
  18. if (value)
  19. {
  20. NGUIDebug.CreateInstance();
  21. }
  22. }
  23. }
  24. }
  25. public static void CreateInstance()
  26. {
  27. if (NGUIDebug.mInstance == null)
  28. {
  29. GameObject gameObject = new GameObject("_NGUI Debug");
  30. NGUIDebug.mInstance = gameObject.AddComponent<NGUIDebug>();
  31. UnityEngine.Object.DontDestroyOnLoad(gameObject);
  32. }
  33. }
  34. private static void LogString(string text)
  35. {
  36. if (Application.isPlaying)
  37. {
  38. if (NGUIDebug.mLines.Count > 20)
  39. {
  40. NGUIDebug.mLines.RemoveAt(0);
  41. }
  42. NGUIDebug.mLines.Add(text);
  43. NGUIDebug.CreateInstance();
  44. }
  45. else
  46. {
  47. Debug.Log(text);
  48. }
  49. }
  50. public static void Log(params object[] objs)
  51. {
  52. string text = string.Empty;
  53. for (int i = 0; i < objs.Length; i++)
  54. {
  55. if (i == 0)
  56. {
  57. text += objs[i].ToString();
  58. }
  59. else
  60. {
  61. text = text + ", " + objs[i].ToString();
  62. }
  63. }
  64. NGUIDebug.LogString(text);
  65. }
  66. public static void Clear()
  67. {
  68. NGUIDebug.mLines.Clear();
  69. }
  70. public static void DrawBounds(Bounds b)
  71. {
  72. Vector3 center = b.center;
  73. Vector3 vector = b.center - b.extents;
  74. Vector3 vector2 = b.center + b.extents;
  75. Debug.DrawLine(new Vector3(vector.x, vector.y, center.z), new Vector3(vector2.x, vector.y, center.z), Color.red);
  76. Debug.DrawLine(new Vector3(vector.x, vector.y, center.z), new Vector3(vector.x, vector2.y, center.z), Color.red);
  77. Debug.DrawLine(new Vector3(vector2.x, vector.y, center.z), new Vector3(vector2.x, vector2.y, center.z), Color.red);
  78. Debug.DrawLine(new Vector3(vector.x, vector2.y, center.z), new Vector3(vector2.x, vector2.y, center.z), Color.red);
  79. }
  80. private void OnGUI()
  81. {
  82. if (NGUIDebug.mLines.Count == 0)
  83. {
  84. if (NGUIDebug.mRayDebug && UICamera.hoveredObject != null && Application.isPlaying)
  85. {
  86. GUILayout.Label("Last Hit: " + NGUITools.GetHierarchy(UICamera.hoveredObject).Replace("\"", string.Empty), new GUILayoutOption[0]);
  87. }
  88. }
  89. else
  90. {
  91. int i = 0;
  92. int count = NGUIDebug.mLines.Count;
  93. while (i < count)
  94. {
  95. GUILayout.Label(NGUIDebug.mLines[i], new GUILayoutOption[0]);
  96. i++;
  97. }
  98. }
  99. }
  100. private static bool mRayDebug = false;
  101. private static List<string> mLines = new List<string>();
  102. private static NGUIDebug mInstance = null;
  103. }