ReferencePoolComponentInspector.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using UnityEditor;
  13. using UnityEngine;
  14. using UnityGameFramework.Runtime;
  15. namespace UnityGameFramework.Editor
  16. {
  17. [CustomEditor(typeof(ReferencePoolComponent))]
  18. internal sealed class ReferencePoolComponentInspector : GameFrameworkInspector
  19. {
  20. private readonly Dictionary<string, List<ReferencePoolInfo>> m_ReferencePoolInfos = new Dictionary<string, List<ReferencePoolInfo>>(StringComparer.Ordinal);
  21. private readonly HashSet<string> m_OpenedItems = new HashSet<string>();
  22. private SerializedProperty m_EnableStrictCheck = null;
  23. private bool m_ShowFullClassName = false;
  24. public override void OnInspectorGUI()
  25. {
  26. base.OnInspectorGUI();
  27. serializedObject.Update();
  28. ReferencePoolComponent t = (ReferencePoolComponent)target;
  29. if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
  30. {
  31. bool enableStrictCheck = EditorGUILayout.Toggle("Enable Strict Check", t.EnableStrictCheck);
  32. if (enableStrictCheck != t.EnableStrictCheck)
  33. {
  34. t.EnableStrictCheck = enableStrictCheck;
  35. }
  36. EditorGUILayout.LabelField("Reference Pool Count", ReferencePool.Count.ToString());
  37. m_ShowFullClassName = EditorGUILayout.Toggle("Show Full Class Name", m_ShowFullClassName);
  38. m_ReferencePoolInfos.Clear();
  39. ReferencePoolInfo[] referencePoolInfos = ReferencePool.GetAllReferencePoolInfos();
  40. foreach (ReferencePoolInfo referencePoolInfo in referencePoolInfos)
  41. {
  42. string assemblyName = referencePoolInfo.Type.Assembly.GetName().Name;
  43. List<ReferencePoolInfo> results = null;
  44. if (!m_ReferencePoolInfos.TryGetValue(assemblyName, out results))
  45. {
  46. results = new List<ReferencePoolInfo>();
  47. m_ReferencePoolInfos.Add(assemblyName, results);
  48. }
  49. results.Add(referencePoolInfo);
  50. }
  51. foreach (KeyValuePair<string, List<ReferencePoolInfo>> assemblyReferencePoolInfo in m_ReferencePoolInfos)
  52. {
  53. bool lastState = m_OpenedItems.Contains(assemblyReferencePoolInfo.Key);
  54. bool currentState = EditorGUILayout.Foldout(lastState, assemblyReferencePoolInfo.Key);
  55. if (currentState != lastState)
  56. {
  57. if (currentState)
  58. {
  59. m_OpenedItems.Add(assemblyReferencePoolInfo.Key);
  60. }
  61. else
  62. {
  63. m_OpenedItems.Remove(assemblyReferencePoolInfo.Key);
  64. }
  65. }
  66. if (currentState)
  67. {
  68. EditorGUILayout.BeginVertical("box");
  69. {
  70. EditorGUILayout.LabelField(m_ShowFullClassName ? "Full Class Name" : "Class Name", "Unused\tUsing\tAcquire\tRelease\tAdd\tRemove");
  71. assemblyReferencePoolInfo.Value.Sort(Comparison);
  72. foreach (ReferencePoolInfo referencePoolInfo in assemblyReferencePoolInfo.Value)
  73. {
  74. DrawReferencePoolInfo(referencePoolInfo);
  75. }
  76. if (GUILayout.Button("Export CSV Data"))
  77. {
  78. string exportFileName = EditorUtility.SaveFilePanel("Export CSV Data", string.Empty, Utility.Text.Format("Reference Pool Data - {0}.csv", assemblyReferencePoolInfo.Key), string.Empty);
  79. if (!string.IsNullOrEmpty(exportFileName))
  80. {
  81. try
  82. {
  83. int index = 0;
  84. string[] data = new string[assemblyReferencePoolInfo.Value.Count + 1];
  85. data[index++] = "Class Name,Full Class Name,Unused,Using,Acquire,Release,Add,Remove";
  86. foreach (ReferencePoolInfo referencePoolInfo in assemblyReferencePoolInfo.Value)
  87. {
  88. data[index++] = Utility.Text.Format("{0},{1},{2},{3},{4},{5},{6},{7}", referencePoolInfo.Type.Name, referencePoolInfo.Type.FullName, referencePoolInfo.UnusedReferenceCount.ToString(), referencePoolInfo.UsingReferenceCount.ToString(), referencePoolInfo.AcquireReferenceCount.ToString(), referencePoolInfo.ReleaseReferenceCount.ToString(), referencePoolInfo.AddReferenceCount.ToString(), referencePoolInfo.RemoveReferenceCount.ToString());
  89. }
  90. File.WriteAllLines(exportFileName, data, Encoding.UTF8);
  91. Debug.Log(Utility.Text.Format("Export reference pool CSV data to '{0}' success.", exportFileName));
  92. }
  93. catch (Exception exception)
  94. {
  95. Debug.LogError(Utility.Text.Format("Export reference pool CSV data to '{0}' failure, exception is '{1}'.", exportFileName, exception.ToString()));
  96. }
  97. }
  98. }
  99. }
  100. EditorGUILayout.EndVertical();
  101. EditorGUILayout.Separator();
  102. }
  103. }
  104. }
  105. else
  106. {
  107. EditorGUILayout.PropertyField(m_EnableStrictCheck);
  108. }
  109. serializedObject.ApplyModifiedProperties();
  110. Repaint();
  111. }
  112. private void OnEnable()
  113. {
  114. m_EnableStrictCheck = serializedObject.FindProperty("m_EnableStrictCheck");
  115. }
  116. private void DrawReferencePoolInfo(ReferencePoolInfo referencePoolInfo)
  117. {
  118. EditorGUILayout.LabelField(m_ShowFullClassName ? referencePoolInfo.Type.FullName : referencePoolInfo.Type.Name, Utility.Text.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", referencePoolInfo.UnusedReferenceCount.ToString(), referencePoolInfo.UsingReferenceCount.ToString(), referencePoolInfo.AcquireReferenceCount.ToString(), referencePoolInfo.ReleaseReferenceCount.ToString(), referencePoolInfo.AddReferenceCount.ToString(), referencePoolInfo.RemoveReferenceCount.ToString()));
  119. }
  120. private int Comparison(ReferencePoolInfo a, ReferencePoolInfo b)
  121. {
  122. if (m_ShowFullClassName)
  123. {
  124. return a.Type.FullName.CompareTo(b.Type.FullName);
  125. }
  126. else
  127. {
  128. return a.Type.Name.CompareTo(b.Type.Name);
  129. }
  130. }
  131. }
  132. }