DebuggerComponent.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //------------------------------------------------------------
  2. // Game Framework v3.x
  3. // Copyright © 2013-2017 Jiang Yin. All rights reserved.
  4. // Homepage: http://gameframework.cn/
  5. // Feedback: mailto:i@jiangyin.me
  6. //------------------------------------------------------------
  7. using GameFramework.Debugger;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. namespace UnityGameFramework.Runtime
  11. {
  12. /// <summary>
  13. /// 调试组件。
  14. /// </summary>
  15. [DisallowMultipleComponent]
  16. [AddComponentMenu("Game Framework/Debugger")]
  17. public sealed partial class DebuggerComponent : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// 默认调试器漂浮框大小。
  21. /// </summary>
  22. internal static readonly Rect DefaultIconRect = new Rect(10f, 10f, 60f, 60f);
  23. /// <summary>
  24. /// 默认调试器窗口大小。
  25. /// </summary>
  26. internal static readonly Rect DefaultWindowRect = new Rect(10f, 10f, 640f, 480f);
  27. /// <summary>
  28. /// 默认调试器窗口缩放比例。
  29. /// </summary>
  30. internal static readonly float DefaultWindowScale = 1f;
  31. private DebuggerManager m_DebuggerManager = null;
  32. private Rect m_DragRect = new Rect(0f, 0f, float.MaxValue, 25f);
  33. private Rect m_IconRect = DefaultIconRect;
  34. private Rect m_WindowRect = DefaultWindowRect;
  35. private float m_WindowScale = DefaultWindowScale;
  36. [SerializeField]
  37. private GUISkin m_Skin = null;
  38. [SerializeField]
  39. private DebuggerActiveWindowType m_ActiveWindow = DebuggerActiveWindowType.Auto;
  40. [SerializeField]
  41. private bool m_ShowFullWindow = true;
  42. [SerializeField]
  43. private ConsoleWindow m_ConsoleWindow = new ConsoleWindow();
  44. private SystemInformationWindow m_SystemInformationWindow = new SystemInformationWindow();
  45. private EnvironmentInformationWindow m_EnvironmentInformationWindow = new EnvironmentInformationWindow();
  46. private ScreenInformationWindow m_ScreenInformationWindow = new ScreenInformationWindow();
  47. private GraphicsInformationWindow m_GraphicsInformationWindow = new GraphicsInformationWindow();
  48. private InputSummaryInformationWindow m_InputSummaryInformationWindow = new InputSummaryInformationWindow();
  49. private InputTouchInformationWindow m_InputTouchInformationWindow = new InputTouchInformationWindow();
  50. private InputLocationInformationWindow m_InputLocationInformationWindow = new InputLocationInformationWindow();
  51. private InputAccelerationInformationWindow m_InputAccelerationInformationWindow = new InputAccelerationInformationWindow();
  52. private InputGyroscopeInformationWindow m_InputGyroscopeInformationWindow = new InputGyroscopeInformationWindow();
  53. private InputCompassInformationWindow m_InputCompassInformationWindow = new InputCompassInformationWindow();
  54. private PathInformationWindow m_PathInformationWindow = new PathInformationWindow();
  55. private SceneInformationWindow m_SceneInformationWindow = new SceneInformationWindow();
  56. private TimeInformationWindow m_TimeInformationWindow = new TimeInformationWindow();
  57. private QualityInformationWindow m_QualityInformationWindow = new QualityInformationWindow();
  58. private ProfilerInformationWindow m_ProfilerInformationWindow = new ProfilerInformationWindow();
  59. private WebPlayerInformationWindow m_WebPlayerInformationWindow = new WebPlayerInformationWindow();
  60. private RuntimeMemoryInformationWindow<Object> m_RuntimeMemoryAllInformationWindow = new RuntimeMemoryInformationWindow<Object>();
  61. private RuntimeMemoryInformationWindow<Texture> m_RuntimeMemoryTextureInformationWindow = new RuntimeMemoryInformationWindow<Texture>();
  62. private RuntimeMemoryInformationWindow<Mesh> m_RuntimeMemoryMeshInformationWindow = new RuntimeMemoryInformationWindow<Mesh>();
  63. private RuntimeMemoryInformationWindow<Material> m_RuntimeMemoryMaterialInformationWindow = new RuntimeMemoryInformationWindow<Material>();
  64. private RuntimeMemoryInformationWindow<AnimationClip> m_RuntimeMemoryAnimationClipInformationWindow = new RuntimeMemoryInformationWindow<AnimationClip>();
  65. private RuntimeMemoryInformationWindow<AudioClip> m_RuntimeMemoryAudioClipInformationWindow = new RuntimeMemoryInformationWindow<AudioClip>();
  66. private RuntimeMemoryInformationWindow<Font> m_RuntimeMemoryFontInformationWindow = new RuntimeMemoryInformationWindow<Font>();
  67. private RuntimeMemoryInformationWindow<GameObject> m_RuntimeMemoryGameObjectInformationWindow = new RuntimeMemoryInformationWindow<GameObject>();
  68. private RuntimeMemoryInformationWindow<Component> m_RuntimeMemoryComponentInformationWindow = new RuntimeMemoryInformationWindow<Component>();
  69. private GeneralSettingsWindow m_GeneralSettingsWindow = new GeneralSettingsWindow();
  70. private QualitySettingsWindow m_QualitySettingsWindow = new QualitySettingsWindow();
  71. private FpsCounter m_FpsCounter = null;
  72. /// <summary>
  73. /// 获取或设置调试窗口是否激活。
  74. /// </summary>
  75. public bool ActiveWindow
  76. {
  77. get
  78. {
  79. return m_DebuggerManager.ActiveWindow;
  80. }
  81. set
  82. {
  83. m_DebuggerManager.ActiveWindow = value;
  84. enabled = value;
  85. }
  86. }
  87. /// <summary>
  88. /// 获取或设置是否显示完整调试器界面。
  89. /// </summary>
  90. public bool ShowFullWindow
  91. {
  92. get
  93. {
  94. return m_ShowFullWindow;
  95. }
  96. set
  97. {
  98. m_ShowFullWindow = value;
  99. }
  100. }
  101. /// <summary>
  102. /// 获取或设置调试器漂浮框大小。
  103. /// </summary>
  104. public Rect IconRect
  105. {
  106. get
  107. {
  108. return m_IconRect;
  109. }
  110. set
  111. {
  112. m_IconRect = value;
  113. }
  114. }
  115. /// <summary>
  116. /// 获取或设置调试器窗口大小。
  117. /// </summary>
  118. public Rect WindowRect
  119. {
  120. get
  121. {
  122. return m_WindowRect;
  123. }
  124. set
  125. {
  126. m_WindowRect = value;
  127. }
  128. }
  129. /// <summary>
  130. /// 获取或设置调试器窗口缩放比例。
  131. /// </summary>
  132. public float WindowScale
  133. {
  134. get
  135. {
  136. return m_WindowScale;
  137. }
  138. set
  139. {
  140. m_WindowScale = value;
  141. }
  142. }
  143. /// <summary>
  144. /// 游戏框架组件初始化。
  145. /// </summary>
  146. private void Awake()
  147. {
  148. DontDestroyOnLoad(gameObject);
  149. m_DebuggerManager = new DebuggerManager();
  150. if (m_ActiveWindow == DebuggerActiveWindowType.Auto)
  151. {
  152. ActiveWindow = Debug.isDebugBuild;
  153. }
  154. else
  155. {
  156. ActiveWindow = (m_ActiveWindow == DebuggerActiveWindowType.Open);
  157. }
  158. m_FpsCounter = new FpsCounter(0.5f);
  159. }
  160. private void Start()
  161. {
  162. RegisterDebuggerWindow("Console", m_ConsoleWindow);
  163. RegisterDebuggerWindow("Information/System", m_SystemInformationWindow);
  164. RegisterDebuggerWindow("Information/Environment", m_EnvironmentInformationWindow);
  165. RegisterDebuggerWindow("Information/Screen", m_ScreenInformationWindow);
  166. RegisterDebuggerWindow("Information/Graphics", m_GraphicsInformationWindow);
  167. RegisterDebuggerWindow("Information/Input/Summary", m_InputSummaryInformationWindow);
  168. RegisterDebuggerWindow("Information/Input/Touch", m_InputTouchInformationWindow);
  169. RegisterDebuggerWindow("Information/Input/Location", m_InputLocationInformationWindow);
  170. RegisterDebuggerWindow("Information/Input/Acceleration", m_InputAccelerationInformationWindow);
  171. RegisterDebuggerWindow("Information/Input/Gyroscope", m_InputGyroscopeInformationWindow);
  172. RegisterDebuggerWindow("Information/Input/Compass", m_InputCompassInformationWindow);
  173. RegisterDebuggerWindow("Information/Other/Scene", m_SceneInformationWindow);
  174. RegisterDebuggerWindow("Information/Other/Path", m_PathInformationWindow);
  175. RegisterDebuggerWindow("Information/Other/Time", m_TimeInformationWindow);
  176. RegisterDebuggerWindow("Information/Other/Quality", m_QualityInformationWindow);
  177. RegisterDebuggerWindow("Information/Other/Web Player", m_WebPlayerInformationWindow);
  178. RegisterDebuggerWindow("Profiler/Summary", m_ProfilerInformationWindow);
  179. RegisterDebuggerWindow("Profiler/Memory/All", m_RuntimeMemoryAllInformationWindow);
  180. RegisterDebuggerWindow("Profiler/Memory/Texture", m_RuntimeMemoryTextureInformationWindow);
  181. RegisterDebuggerWindow("Profiler/Memory/Mesh", m_RuntimeMemoryMeshInformationWindow);
  182. RegisterDebuggerWindow("Profiler/Memory/Material", m_RuntimeMemoryMaterialInformationWindow);
  183. RegisterDebuggerWindow("Profiler/Memory/AnimationClip", m_RuntimeMemoryAnimationClipInformationWindow);
  184. RegisterDebuggerWindow("Profiler/Memory/AudioClip", m_RuntimeMemoryAudioClipInformationWindow);
  185. RegisterDebuggerWindow("Profiler/Memory/Font", m_RuntimeMemoryFontInformationWindow);
  186. RegisterDebuggerWindow("Profiler/Memory/GameObject", m_RuntimeMemoryGameObjectInformationWindow);
  187. RegisterDebuggerWindow("Profiler/Memory/Component", m_RuntimeMemoryComponentInformationWindow);
  188. RegisterDebuggerWindow("Settings/General", m_GeneralSettingsWindow, this);
  189. RegisterDebuggerWindow("Settings/Quality", m_QualitySettingsWindow);
  190. RegisterExtensionWindows();
  191. }
  192. private void OnDestroy()
  193. {
  194. if (m_DebuggerManager != null)
  195. {
  196. m_DebuggerManager.Shutdown();
  197. m_DebuggerManager = null;
  198. }
  199. }
  200. private void Update()
  201. {
  202. if (m_DebuggerManager == null || !m_DebuggerManager.ActiveWindow)
  203. {
  204. return;
  205. }
  206. m_DebuggerManager.Update(Time.deltaTime, Time.unscaledDeltaTime);
  207. m_FpsCounter.Update(Time.deltaTime, Time.unscaledDeltaTime);
  208. }
  209. private void OnGUI()
  210. {
  211. if (m_DebuggerManager == null || !m_DebuggerManager.ActiveWindow)
  212. {
  213. return;
  214. }
  215. GUISkin cachedGuiSkin = GUI.skin;
  216. Matrix4x4 cachedMatrix = GUI.matrix;
  217. GUI.skin = m_Skin;
  218. GUI.matrix = Matrix4x4.Scale(new Vector3(m_WindowScale, m_WindowScale, 1f));
  219. if (m_ShowFullWindow)
  220. {
  221. m_WindowRect = GUILayout.Window(0, m_WindowRect, DrawWindow, "<b>GAME FRAMEWORK DEBUGGER</b>");
  222. }
  223. else
  224. {
  225. m_IconRect = GUILayout.Window(0, m_IconRect, DrawDebuggerWindowIcon, "<b>DEBUGGER</b>");
  226. }
  227. GUI.matrix = cachedMatrix;
  228. GUI.skin = cachedGuiSkin;
  229. }
  230. /// <summary>
  231. /// 注册调试窗口。
  232. /// </summary>
  233. /// <param name="path">调试窗口路径。</param>
  234. /// <param name="debuggerWindow">要注册的调试窗口。</param>
  235. /// <param name="args">初始化调试窗口参数。</param>
  236. public void RegisterDebuggerWindow(string path, IDebuggerWindow debuggerWindow, params object[] args)
  237. {
  238. m_DebuggerManager.RegisterDebuggerWindow(path, debuggerWindow, args);
  239. }
  240. /// <summary>
  241. /// 获取调试窗口。
  242. /// </summary>
  243. /// <param name="path">调试窗口路径。</param>
  244. /// <returns>要获取的调试窗口。</returns>
  245. public IDebuggerWindow GetDebuggerWindow(string path)
  246. {
  247. return m_DebuggerManager.GetDebuggerWindow(path);
  248. }
  249. private void DrawWindow(int windowId)
  250. {
  251. GUI.DragWindow(m_DragRect);
  252. DrawDebuggerWindowGroup(m_DebuggerManager.DebuggerWindowRoot);
  253. }
  254. private void DrawDebuggerWindowGroup(IDebuggerWindowGroup debuggerWindowGroup)
  255. {
  256. if (debuggerWindowGroup == null)
  257. {
  258. return;
  259. }
  260. List<string> names = new List<string>();
  261. string[] debuggerWindowNames = debuggerWindowGroup.GetDebuggerWindowNames();
  262. for (int i = 0; i < debuggerWindowNames.Length; i++)
  263. {
  264. names.Add(string.Format("<b>{0}</b>", debuggerWindowNames[i]));
  265. }
  266. if (debuggerWindowGroup == m_DebuggerManager.DebuggerWindowRoot)
  267. {
  268. names.Add("<b>Close</b>");
  269. }
  270. int toolbarIndex = GUILayout.Toolbar(debuggerWindowGroup.SelectedIndex, names.ToArray(), GUILayout.Height(30f), GUILayout.MaxWidth(Screen.width));
  271. if (toolbarIndex >= debuggerWindowGroup.DebuggerWindowCount)
  272. {
  273. m_ShowFullWindow = false;
  274. return;
  275. }
  276. if (debuggerWindowGroup.SelectedIndex != toolbarIndex)
  277. {
  278. debuggerWindowGroup.SelectedWindow.OnLeave();
  279. debuggerWindowGroup.SelectedIndex = toolbarIndex;
  280. debuggerWindowGroup.SelectedWindow.OnEnter();
  281. }
  282. IDebuggerWindowGroup subDebuggerWindowGroup = debuggerWindowGroup.SelectedWindow as IDebuggerWindowGroup;
  283. if (subDebuggerWindowGroup != null)
  284. {
  285. DrawDebuggerWindowGroup(subDebuggerWindowGroup);
  286. }
  287. if (debuggerWindowGroup.SelectedWindow != null)
  288. {
  289. debuggerWindowGroup.SelectedWindow.OnDraw();
  290. }
  291. }
  292. private void DrawDebuggerWindowIcon(int windowId)
  293. {
  294. GUI.DragWindow(m_DragRect);
  295. GUILayout.Space(5);
  296. Color32 color = Color.white;
  297. m_ConsoleWindow.RefreshCount();
  298. if (m_ConsoleWindow.FatalCount > 0)
  299. {
  300. color = m_ConsoleWindow.GetLogStringColor(LogType.Exception);
  301. }
  302. else if (m_ConsoleWindow.ErrorCount > 0)
  303. {
  304. color = m_ConsoleWindow.GetLogStringColor(LogType.Error);
  305. }
  306. else if (m_ConsoleWindow.WarningCount > 0)
  307. {
  308. color = m_ConsoleWindow.GetLogStringColor(LogType.Warning);
  309. }
  310. else
  311. {
  312. color = m_ConsoleWindow.GetLogStringColor(LogType.Log);
  313. }
  314. string title = string.Format("<color=#{0}{1}{2}{3}><b>FPS: {4}</b></color>", color.r.ToString("x2"), color.g.ToString("x2"), color.b.ToString("x2"), color.a.ToString("x2"), m_FpsCounter.CurrentFps.ToString("F2"));
  315. if (GUILayout.Button(title, GUILayout.Width(100f), GUILayout.Height(40f)))
  316. {
  317. m_ShowFullWindow = true;
  318. }
  319. }
  320. }
  321. }