DebuggerComponent.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.Debugger;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. namespace UnityGameFramework.Runtime
  12. {
  13. /// <summary>
  14. /// 调试器组件。
  15. /// </summary>
  16. [DisallowMultipleComponent]
  17. [AddComponentMenu("Game Framework/Debugger")]
  18. public sealed partial class DebuggerComponent : GameFrameworkComponent
  19. {
  20. /// <summary>
  21. /// 默认调试器漂浮框大小。
  22. /// </summary>
  23. internal static readonly Rect DefaultIconRect = new Rect(10f, 10f, 60f, 60f);
  24. /// <summary>
  25. /// 默认调试器窗口大小。
  26. /// </summary>
  27. internal static readonly Rect DefaultWindowRect = new Rect(10f, 10f, 640f, 480f);
  28. /// <summary>
  29. /// 默认调试器窗口缩放比例。
  30. /// </summary>
  31. internal static readonly float DefaultWindowScale = 1f;
  32. private static readonly TextEditor s_TextEditor = new TextEditor();
  33. private IDebuggerManager m_DebuggerManager = null;
  34. private Rect m_DragRect = new Rect(0f, 0f, float.MaxValue, 25f);
  35. private Rect m_IconRect = DefaultIconRect;
  36. private Rect m_WindowRect = DefaultWindowRect;
  37. private float m_WindowScale = DefaultWindowScale;
  38. [SerializeField]
  39. private GUISkin m_Skin = null;
  40. [SerializeField]
  41. private DebuggerActiveWindowType m_ActiveWindow = DebuggerActiveWindowType.AlwaysOpen;
  42. [SerializeField]
  43. private bool m_ShowFullWindow = false;
  44. [SerializeField]
  45. private ConsoleWindow m_ConsoleWindow = new ConsoleWindow();
  46. private SystemInformationWindow m_SystemInformationWindow = new SystemInformationWindow();
  47. private EnvironmentInformationWindow m_EnvironmentInformationWindow = new EnvironmentInformationWindow();
  48. private ScreenInformationWindow m_ScreenInformationWindow = new ScreenInformationWindow();
  49. private GraphicsInformationWindow m_GraphicsInformationWindow = new GraphicsInformationWindow();
  50. private InputSummaryInformationWindow m_InputSummaryInformationWindow = new InputSummaryInformationWindow();
  51. private InputTouchInformationWindow m_InputTouchInformationWindow = new InputTouchInformationWindow();
  52. private InputLocationInformationWindow m_InputLocationInformationWindow = new InputLocationInformationWindow();
  53. private InputAccelerationInformationWindow m_InputAccelerationInformationWindow = new InputAccelerationInformationWindow();
  54. private InputGyroscopeInformationWindow m_InputGyroscopeInformationWindow = new InputGyroscopeInformationWindow();
  55. private InputCompassInformationWindow m_InputCompassInformationWindow = new InputCompassInformationWindow();
  56. private PathInformationWindow m_PathInformationWindow = new PathInformationWindow();
  57. private SceneInformationWindow m_SceneInformationWindow = new SceneInformationWindow();
  58. private TimeInformationWindow m_TimeInformationWindow = new TimeInformationWindow();
  59. private QualityInformationWindow m_QualityInformationWindow = new QualityInformationWindow();
  60. private ProfilerInformationWindow m_ProfilerInformationWindow = new ProfilerInformationWindow();
  61. private WebPlayerInformationWindow m_WebPlayerInformationWindow = new WebPlayerInformationWindow();
  62. private RuntimeMemorySummaryWindow m_RuntimeMemorySummaryWindow = new RuntimeMemorySummaryWindow();
  63. private RuntimeMemoryInformationWindow<Object> m_RuntimeMemoryAllInformationWindow = new RuntimeMemoryInformationWindow<Object>();
  64. private RuntimeMemoryInformationWindow<Texture> m_RuntimeMemoryTextureInformationWindow = new RuntimeMemoryInformationWindow<Texture>();
  65. private RuntimeMemoryInformationWindow<Mesh> m_RuntimeMemoryMeshInformationWindow = new RuntimeMemoryInformationWindow<Mesh>();
  66. private RuntimeMemoryInformationWindow<Material> m_RuntimeMemoryMaterialInformationWindow = new RuntimeMemoryInformationWindow<Material>();
  67. private RuntimeMemoryInformationWindow<Shader> m_RuntimeMemoryShaderInformationWindow = new RuntimeMemoryInformationWindow<Shader>();
  68. private RuntimeMemoryInformationWindow<AnimationClip> m_RuntimeMemoryAnimationClipInformationWindow = new RuntimeMemoryInformationWindow<AnimationClip>();
  69. private RuntimeMemoryInformationWindow<AudioClip> m_RuntimeMemoryAudioClipInformationWindow = new RuntimeMemoryInformationWindow<AudioClip>();
  70. private RuntimeMemoryInformationWindow<Font> m_RuntimeMemoryFontInformationWindow = new RuntimeMemoryInformationWindow<Font>();
  71. private RuntimeMemoryInformationWindow<TextAsset> m_RuntimeMemoryTextAssetInformationWindow = new RuntimeMemoryInformationWindow<TextAsset>();
  72. private RuntimeMemoryInformationWindow<ScriptableObject> m_RuntimeMemoryScriptableObjectInformationWindow = new RuntimeMemoryInformationWindow<ScriptableObject>();
  73. private ObjectPoolInformationWindow m_ObjectPoolInformationWindow = new ObjectPoolInformationWindow();
  74. private ReferencePoolInformationWindow m_ReferencePoolInformationWindow = new ReferencePoolInformationWindow();
  75. private NetworkInformationWindow m_NetworkInformationWindow = new NetworkInformationWindow();
  76. private SettingsWindow m_SettingsWindow = new SettingsWindow();
  77. private OperationsWindow m_OperationsWindow = new OperationsWindow();
  78. private FpsCounter m_FpsCounter = null;
  79. /// <summary>
  80. /// 获取或设置调试器窗口是否激活。
  81. /// </summary>
  82. public bool ActiveWindow
  83. {
  84. get
  85. {
  86. return m_DebuggerManager.ActiveWindow;
  87. }
  88. set
  89. {
  90. m_DebuggerManager.ActiveWindow = value;
  91. enabled = value;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取或设置是否显示完整调试器界面。
  96. /// </summary>
  97. public bool ShowFullWindow
  98. {
  99. get
  100. {
  101. return m_ShowFullWindow;
  102. }
  103. set
  104. {
  105. m_ShowFullWindow = value;
  106. }
  107. }
  108. /// <summary>
  109. /// 获取或设置调试器漂浮框大小。
  110. /// </summary>
  111. public Rect IconRect
  112. {
  113. get
  114. {
  115. return m_IconRect;
  116. }
  117. set
  118. {
  119. m_IconRect = value;
  120. }
  121. }
  122. /// <summary>
  123. /// 获取或设置调试器窗口大小。
  124. /// </summary>
  125. public Rect WindowRect
  126. {
  127. get
  128. {
  129. return m_WindowRect;
  130. }
  131. set
  132. {
  133. m_WindowRect = value;
  134. }
  135. }
  136. /// <summary>
  137. /// 获取或设置调试器窗口缩放比例。
  138. /// </summary>
  139. public float WindowScale
  140. {
  141. get
  142. {
  143. return m_WindowScale;
  144. }
  145. set
  146. {
  147. m_WindowScale = value;
  148. }
  149. }
  150. /// <summary>
  151. /// 游戏框架组件初始化。
  152. /// </summary>
  153. protected override void Awake()
  154. {
  155. base.Awake();
  156. m_DebuggerManager = GameFrameworkEntry.GetModule<IDebuggerManager>();
  157. if (m_DebuggerManager == null)
  158. {
  159. Log.Fatal("Debugger manager is invalid.");
  160. return;
  161. }
  162. m_FpsCounter = new FpsCounter(0.5f);
  163. }
  164. private void Start()
  165. {
  166. RegisterDebuggerWindow("Console", m_ConsoleWindow);
  167. RegisterDebuggerWindow("Information/System", m_SystemInformationWindow);
  168. RegisterDebuggerWindow("Information/Environment", m_EnvironmentInformationWindow);
  169. RegisterDebuggerWindow("Information/Screen", m_ScreenInformationWindow);
  170. RegisterDebuggerWindow("Information/Graphics", m_GraphicsInformationWindow);
  171. RegisterDebuggerWindow("Information/Input/Summary", m_InputSummaryInformationWindow);
  172. RegisterDebuggerWindow("Information/Input/Touch", m_InputTouchInformationWindow);
  173. RegisterDebuggerWindow("Information/Input/Location", m_InputLocationInformationWindow);
  174. RegisterDebuggerWindow("Information/Input/Acceleration", m_InputAccelerationInformationWindow);
  175. RegisterDebuggerWindow("Information/Input/Gyroscope", m_InputGyroscopeInformationWindow);
  176. RegisterDebuggerWindow("Information/Input/Compass", m_InputCompassInformationWindow);
  177. RegisterDebuggerWindow("Information/Other/Scene", m_SceneInformationWindow);
  178. RegisterDebuggerWindow("Information/Other/Path", m_PathInformationWindow);
  179. RegisterDebuggerWindow("Information/Other/Time", m_TimeInformationWindow);
  180. RegisterDebuggerWindow("Information/Other/Quality", m_QualityInformationWindow);
  181. RegisterDebuggerWindow("Information/Other/Web Player", m_WebPlayerInformationWindow);
  182. RegisterDebuggerWindow("Profiler/Summary", m_ProfilerInformationWindow);
  183. RegisterDebuggerWindow("Profiler/Memory/Summary", m_RuntimeMemorySummaryWindow);
  184. RegisterDebuggerWindow("Profiler/Memory/All", m_RuntimeMemoryAllInformationWindow);
  185. RegisterDebuggerWindow("Profiler/Memory/Texture", m_RuntimeMemoryTextureInformationWindow);
  186. RegisterDebuggerWindow("Profiler/Memory/Mesh", m_RuntimeMemoryMeshInformationWindow);
  187. RegisterDebuggerWindow("Profiler/Memory/Material", m_RuntimeMemoryMaterialInformationWindow);
  188. RegisterDebuggerWindow("Profiler/Memory/Shader", m_RuntimeMemoryShaderInformationWindow);
  189. RegisterDebuggerWindow("Profiler/Memory/AnimationClip", m_RuntimeMemoryAnimationClipInformationWindow);
  190. RegisterDebuggerWindow("Profiler/Memory/AudioClip", m_RuntimeMemoryAudioClipInformationWindow);
  191. RegisterDebuggerWindow("Profiler/Memory/Font", m_RuntimeMemoryFontInformationWindow);
  192. RegisterDebuggerWindow("Profiler/Memory/TextAsset", m_RuntimeMemoryTextAssetInformationWindow);
  193. RegisterDebuggerWindow("Profiler/Memory/ScriptableObject", m_RuntimeMemoryScriptableObjectInformationWindow);
  194. RegisterDebuggerWindow("Profiler/Object Pool", m_ObjectPoolInformationWindow);
  195. RegisterDebuggerWindow("Profiler/Reference Pool", m_ReferencePoolInformationWindow);
  196. RegisterDebuggerWindow("Profiler/Network", m_NetworkInformationWindow);
  197. RegisterDebuggerWindow("Other/Settings", m_SettingsWindow);
  198. RegisterDebuggerWindow("Other/Operations", m_OperationsWindow);
  199. switch (m_ActiveWindow)
  200. {
  201. case DebuggerActiveWindowType.AlwaysOpen:
  202. ActiveWindow = true;
  203. break;
  204. case DebuggerActiveWindowType.OnlyOpenWhenDevelopment:
  205. ActiveWindow = Debug.isDebugBuild;
  206. break;
  207. case DebuggerActiveWindowType.OnlyOpenInEditor:
  208. ActiveWindow = Application.isEditor;
  209. break;
  210. default:
  211. ActiveWindow = false;
  212. break;
  213. }
  214. }
  215. private void Update()
  216. {
  217. m_FpsCounter.Update(Time.deltaTime, Time.unscaledDeltaTime);
  218. }
  219. private void OnGUI()
  220. {
  221. if (m_DebuggerManager == null || !m_DebuggerManager.ActiveWindow)
  222. {
  223. return;
  224. }
  225. GUISkin cachedGuiSkin = GUI.skin;
  226. Matrix4x4 cachedMatrix = GUI.matrix;
  227. GUI.skin = m_Skin;
  228. GUI.matrix = Matrix4x4.Scale(new Vector3(m_WindowScale, m_WindowScale, 1f));
  229. if (m_ShowFullWindow)
  230. {
  231. m_WindowRect = GUILayout.Window(0, m_WindowRect, DrawWindow, "<b>GAME FRAMEWORK DEBUGGER</b>");
  232. }
  233. else
  234. {
  235. m_IconRect = GUILayout.Window(0, m_IconRect, DrawDebuggerWindowIcon, "<b>DEBUGGER</b>");
  236. }
  237. GUI.matrix = cachedMatrix;
  238. GUI.skin = cachedGuiSkin;
  239. }
  240. /// <summary>
  241. /// 注册调试器窗口。
  242. /// </summary>
  243. /// <param name="path">调试器窗口路径。</param>
  244. /// <param name="debuggerWindow">要注册的调试器窗口。</param>
  245. /// <param name="args">初始化调试器窗口参数。</param>
  246. public void RegisterDebuggerWindow(string path, IDebuggerWindow debuggerWindow, params object[] args)
  247. {
  248. m_DebuggerManager.RegisterDebuggerWindow(path, debuggerWindow, args);
  249. }
  250. /// <summary>
  251. /// 解除注册调试器窗口。
  252. /// </summary>
  253. /// <param name="path">调试器窗口路径。</param>
  254. /// <returns>是否解除注册调试器窗口成功。</returns>
  255. public bool UnregisterDebuggerWindow(string path)
  256. {
  257. return m_DebuggerManager.UnregisterDebuggerWindow(path);
  258. }
  259. /// <summary>
  260. /// 获取调试器窗口。
  261. /// </summary>
  262. /// <param name="path">调试器窗口路径。</param>
  263. /// <returns>要获取的调试器窗口。</returns>
  264. public IDebuggerWindow GetDebuggerWindow(string path)
  265. {
  266. return m_DebuggerManager.GetDebuggerWindow(path);
  267. }
  268. /// <summary>
  269. /// 选中调试器窗口。
  270. /// </summary>
  271. /// <param name="path">调试器窗口路径。</param>
  272. /// <returns>是否成功选中调试器窗口。</returns>
  273. public bool SelectDebuggerWindow(string path)
  274. {
  275. return m_DebuggerManager.SelectDebuggerWindow(path);
  276. }
  277. /// <summary>
  278. /// 还原调试器窗口布局。
  279. /// </summary>
  280. public void ResetLayout()
  281. {
  282. IconRect = DefaultIconRect;
  283. WindowRect = DefaultWindowRect;
  284. WindowScale = DefaultWindowScale;
  285. }
  286. /// <summary>
  287. /// 获取记录的所有日志。
  288. /// </summary>
  289. /// <param name="results">要获取的日志。</param>
  290. public void GetRecentLogs(List<LogNode> results)
  291. {
  292. m_ConsoleWindow.GetRecentLogs(results);
  293. }
  294. /// <summary>
  295. /// 获取记录的最近日志。
  296. /// </summary>
  297. /// <param name="results">要获取的日志。</param>
  298. /// <param name="count">要获取最近日志的数量。</param>
  299. public void GetRecentLogs(List<LogNode> results, int count)
  300. {
  301. m_ConsoleWindow.GetRecentLogs(results, count);
  302. }
  303. private void DrawWindow(int windowId)
  304. {
  305. GUI.DragWindow(m_DragRect);
  306. DrawDebuggerWindowGroup(m_DebuggerManager.DebuggerWindowRoot);
  307. }
  308. private void DrawDebuggerWindowGroup(IDebuggerWindowGroup debuggerWindowGroup)
  309. {
  310. if (debuggerWindowGroup == null)
  311. {
  312. return;
  313. }
  314. List<string> names = new List<string>();
  315. string[] debuggerWindowNames = debuggerWindowGroup.GetDebuggerWindowNames();
  316. for (int i = 0; i < debuggerWindowNames.Length; i++)
  317. {
  318. names.Add(Utility.Text.Format("<b>{0}</b>", debuggerWindowNames[i]));
  319. }
  320. if (debuggerWindowGroup == m_DebuggerManager.DebuggerWindowRoot)
  321. {
  322. names.Add("<b>Close</b>");
  323. }
  324. int toolbarIndex = GUILayout.Toolbar(debuggerWindowGroup.SelectedIndex, names.ToArray(), GUILayout.Height(30f), GUILayout.MaxWidth(Screen.width));
  325. if (toolbarIndex >= debuggerWindowGroup.DebuggerWindowCount)
  326. {
  327. m_ShowFullWindow = false;
  328. return;
  329. }
  330. if (debuggerWindowGroup.SelectedWindow == null)
  331. {
  332. return;
  333. }
  334. if (debuggerWindowGroup.SelectedIndex != toolbarIndex)
  335. {
  336. debuggerWindowGroup.SelectedWindow.OnLeave();
  337. debuggerWindowGroup.SelectedIndex = toolbarIndex;
  338. debuggerWindowGroup.SelectedWindow.OnEnter();
  339. }
  340. IDebuggerWindowGroup subDebuggerWindowGroup = debuggerWindowGroup.SelectedWindow as IDebuggerWindowGroup;
  341. if (subDebuggerWindowGroup != null)
  342. {
  343. DrawDebuggerWindowGroup(subDebuggerWindowGroup);
  344. }
  345. debuggerWindowGroup.SelectedWindow.OnDraw();
  346. }
  347. private void DrawDebuggerWindowIcon(int windowId)
  348. {
  349. GUI.DragWindow(m_DragRect);
  350. GUILayout.Space(5);
  351. Color32 color = Color.white;
  352. m_ConsoleWindow.RefreshCount();
  353. if (m_ConsoleWindow.FatalCount > 0)
  354. {
  355. color = m_ConsoleWindow.GetLogStringColor(LogType.Exception);
  356. }
  357. else if (m_ConsoleWindow.ErrorCount > 0)
  358. {
  359. color = m_ConsoleWindow.GetLogStringColor(LogType.Error);
  360. }
  361. else if (m_ConsoleWindow.WarningCount > 0)
  362. {
  363. color = m_ConsoleWindow.GetLogStringColor(LogType.Warning);
  364. }
  365. else
  366. {
  367. color = m_ConsoleWindow.GetLogStringColor(LogType.Log);
  368. }
  369. string title = Utility.Text.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"));
  370. if (GUILayout.Button(title, GUILayout.Width(100f), GUILayout.Height(40f)))
  371. {
  372. m_ShowFullWindow = true;
  373. }
  374. }
  375. private static void CopyToClipboard(string content)
  376. {
  377. s_TextEditor.text = content;
  378. s_TextEditor.OnFocus();
  379. s_TextEditor.Copy();
  380. s_TextEditor.text = string.Empty;
  381. }
  382. }
  383. }