XMUnityDebug.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. namespace CommonUnity3D.XMUnity.DebugUtil
  6. {
  7. public class XMUnityDebug : MonoBehaviour
  8. {
  9. //调试模式开关,控制是否显示GUI调试信息,内外网切换开关等,线上正式版本打包要设为false.
  10. #if HZDSB_SH || HZDSB_CJ || HZDSB_OT || HZDSB_OF || HZDSB_APP
  11. public const bool DEBUG_MODE = false;
  12. #else
  13. public const bool DEBUG_MODE = true;
  14. #endif
  15. public bool ShowDebugInfo { get { return bDebug; } }
  16. private static XMUnityDebug mInstance = null;
  17. private StringBuilder mSB = null;
  18. private bool mShowWindow = false;
  19. private const string BtnName = "日志";
  20. private GUIStyle labStyle = new GUIStyle();
  21. private Rect BtnDebugPos = new Rect(10, 10, 0.15f * Screen.width, 0.08f * Screen.height);
  22. private Rect mWindowRect = new Rect(0.02f * Screen.width,
  23. 0.02f * Screen.height,
  24. Screen.width - (2 * 0.02f * Screen.width),
  25. Screen.height - (2 * 0.02f * Screen.height));
  26. private GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
  27. private GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
  28. private const int WINDOW_ID = 121121;
  29. private Vector2 mScrollPos;
  30. private Rect DRAG_WINDOW = new Rect(0, 0, 10000, 20);
  31. private Object mLockSign = new Object();
  32. private bool mCollapse = false;
  33. private List<ConsoleMessage> mMessageList = new List<ConsoleMessage>();
  34. private bool bDebug = false;
  35. //bool bIsShowRz = false;
  36. private bool mShowBodySize = false;
  37. private bool mShowGuard = false;
  38. private bool mShowAttack = false;
  39. void OnEnable()
  40. {
  41. labStyle.fontSize = Screen.width / 40;
  42. //Application.RegisterLogCallback(OnHandleLog);
  43. Application.logMessageReceived += Application_logMessageReceived;
  44. }
  45. private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
  46. {
  47. OnHandleLog(condition, stackTrace, type);
  48. }
  49. void OnDisable()
  50. {
  51. //Application.RegisterLogCallback(null);
  52. Application.logMessageReceived -= Application_logMessageReceived;
  53. }
  54. void OnGUI()
  55. {
  56. if (!DEBUG_MODE) return;
  57. bDebug = GUI.Toggle(new Rect(0, 300, 100, 20), bDebug, "Debug");
  58. if (bDebug == false) { return; }
  59. if (GUI.Button(BtnDebugPos, BtnName))
  60. {
  61. mShowWindow = !mShowWindow;
  62. }
  63. if (mShowWindow)
  64. {
  65. mWindowRect = GUILayout.Window(WINDOW_ID, mWindowRect, UpdateWindow, BtnName);
  66. }
  67. if (mDebugInterfaceIml == null) { return; }
  68. bool body = GUI.Toggle(new Rect(0, 320, 100, 20), mShowBodySize, "BodySize");
  69. if (mShowBodySize != body)
  70. {
  71. mShowBodySize = body;
  72. mDebugInterfaceIml.ShowBodySize(mShowBodySize);
  73. }
  74. bool guard = GUI.Toggle(new Rect(0, 340, 100, 20), mShowGuard, "Guard");
  75. if (guard != mShowGuard)
  76. {
  77. mShowGuard = guard;
  78. mDebugInterfaceIml.ShowGuard(mShowGuard);
  79. }
  80. bool attack = GUI.Toggle(new Rect(0, 360, 100, 20), mShowAttack, "Attack");
  81. if (mShowAttack != attack)
  82. {
  83. mShowAttack = attack;
  84. mDebugInterfaceIml.ShowAttack(mShowAttack);
  85. }
  86. //if(GUI.Button(new Rect(0, 250, 50, 30), "Near"))
  87. //{
  88. // mDebugInterfaceIml.DoNear();
  89. //}
  90. //if(GUI.Button(new Rect(70, 250, 50, 30), "Far"))
  91. //{
  92. // mDebugInterfaceIml.DoFar();
  93. //}
  94. //if(GUI.Button(new Rect(0, 210, 50, 30), "-H"))
  95. //{
  96. // mDebugInterfaceIml.DoSub_H();
  97. //}
  98. //if(GUI.Button(new Rect(70, 210, 50, 30), "+H"))
  99. //{
  100. // mDebugInterfaceIml.DoAdd_H();
  101. //}
  102. //if(GUI.Button(new Rect(0, 170, 50, 30), "-RX"))
  103. //{
  104. // mDebugInterfaceIml.DoSub_RX();
  105. //}
  106. //if(GUI.Button(new Rect(70, 170, 50, 30), "+RX"))
  107. //{
  108. // mDebugInterfaceIml.DoAdd_RX();
  109. //}
  110. //if(GUI.Button(new Rect(0, 130, 50, 30), "-RY"))
  111. //{
  112. // mDebugInterfaceIml.DoSub_RY();
  113. //}
  114. //if(GUI.Button(new Rect(70, 130, 50, 30), "+RY"))
  115. //{
  116. // mDebugInterfaceIml.DoAdd_RY();
  117. //}
  118. }
  119. void UpdateWindow(int windowID)
  120. {
  121. lock (mLockSign)
  122. {
  123. mScrollPos = GUILayout.BeginScrollView(mScrollPos);
  124. // Go through each logged entry
  125. for (int i = 0; i < mMessageList.Count; i++)
  126. {
  127. ConsoleMessage entry = mMessageList[i];
  128. // If this message is the same as the last one and the collapse feature is chosen, skip it
  129. if (mCollapse && i > 0 && entry.message == mMessageList[i - 1].message)
  130. {
  131. continue;
  132. }
  133. // Change the text colour according to the log type
  134. switch (entry.type)
  135. {
  136. case LogType.Error:
  137. case LogType.Exception:
  138. GUI.contentColor = Color.green;
  139. labStyle.normal.textColor = Color.green;
  140. break;
  141. case LogType.Warning:
  142. GUI.contentColor = Color.yellow;
  143. labStyle.normal.textColor = Color.yellow;
  144. break;
  145. default:
  146. GUI.contentColor = Color.white;
  147. labStyle.normal.textColor = Color.white;
  148. break;
  149. }
  150. GUILayout.Label(entry.message, labStyle);
  151. }
  152. GUI.contentColor = Color.white;
  153. GUILayout.EndScrollView();
  154. GUILayout.BeginHorizontal();
  155. // Clear button
  156. if (GUILayout.Button(clearLabel))
  157. {
  158. mMessageList.Clear();
  159. }
  160. // Collapse toggle
  161. mCollapse = GUILayout.Toggle(mCollapse, collapseLabel, GUILayout.ExpandWidth(false));
  162. GUILayout.EndHorizontal();
  163. GUI.DragWindow(DRAG_WINDOW);
  164. }
  165. }
  166. private IDebugCameraInterface mDebugInterfaceIml = null;
  167. public void SetInterface(IDebugCameraInterface iml)
  168. {
  169. mDebugInterfaceIml = iml;
  170. }
  171. private XMUnityDebug()
  172. {
  173. mInstance = this;
  174. Init();
  175. }
  176. public static XMUnityDebug GetInstance()
  177. {
  178. if (mInstance == null)
  179. {
  180. new XMUnityDebug();
  181. }
  182. return mInstance;
  183. }
  184. private void Init()
  185. {
  186. mSB = new StringBuilder();
  187. }
  188. public void Log(string txt)
  189. {
  190. }
  191. public void ErrorLog(string txt)
  192. {
  193. }
  194. private void OnHandleLog(string message, string stackTrace, LogType type)
  195. {
  196. AutoClear();
  197. mSB.Length = 0;
  198. mSB.Append("\n------------------------------------------------------------\n");
  199. mSB.Append(message);
  200. mSB.Append("\n");
  201. mSB.Append(stackTrace);
  202. lock (mLockSign)
  203. {
  204. mMessageList.Add(new ConsoleMessage(mSB.ToString(), type));
  205. }
  206. }
  207. private void AutoClear()
  208. {
  209. lock (mLockSign)
  210. {
  211. if (mMessageList.Count > 200)
  212. {
  213. mMessageList.RemoveRange(0, 100);
  214. }
  215. }
  216. }
  217. internal struct ConsoleMessage
  218. {
  219. public readonly string message;
  220. public readonly LogType type;
  221. public ConsoleMessage(string message, LogType type)
  222. {
  223. this.message = message;
  224. this.type = type;
  225. }
  226. }
  227. public interface IDebugCameraInterface
  228. {
  229. void DoNear();
  230. void DoFar();
  231. void DoSub_H();
  232. void DoAdd_H();
  233. void DoSub_RX();
  234. void DoAdd_RX();
  235. void DoSub_RY();
  236. void DoAdd_RY();
  237. void ShowBodySize(bool flag);
  238. void ShowGuard(bool flag);
  239. void ShowAttack(bool flag);
  240. }
  241. }
  242. }