ObjectPoolComponentInspector.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.ObjectPool;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Text;
  13. using UnityEditor;
  14. using UnityEngine;
  15. using UnityGameFramework.Runtime;
  16. namespace UnityGameFramework.Editor
  17. {
  18. [CustomEditor(typeof(ObjectPoolComponent))]
  19. internal sealed class ObjectPoolComponentInspector : GameFrameworkInspector
  20. {
  21. private readonly HashSet<string> m_OpenedItems = new HashSet<string>();
  22. public override void OnInspectorGUI()
  23. {
  24. base.OnInspectorGUI();
  25. if (!EditorApplication.isPlaying)
  26. {
  27. EditorGUILayout.HelpBox("Available during runtime only.", MessageType.Info);
  28. return;
  29. }
  30. ObjectPoolComponent t = (ObjectPoolComponent)target;
  31. if (IsPrefabInHierarchy(t.gameObject))
  32. {
  33. EditorGUILayout.LabelField("Object Pool Count", t.Count.ToString());
  34. ObjectPoolBase[] objectPools = t.GetAllObjectPools(true);
  35. foreach (ObjectPoolBase objectPool in objectPools)
  36. {
  37. DrawObjectPool(objectPool);
  38. }
  39. }
  40. Repaint();
  41. }
  42. private void OnEnable()
  43. {
  44. }
  45. private void DrawObjectPool(ObjectPoolBase objectPool)
  46. {
  47. bool lastState = m_OpenedItems.Contains(objectPool.FullName);
  48. bool currentState = EditorGUILayout.Foldout(lastState, objectPool.FullName);
  49. if (currentState != lastState)
  50. {
  51. if (currentState)
  52. {
  53. m_OpenedItems.Add(objectPool.FullName);
  54. }
  55. else
  56. {
  57. m_OpenedItems.Remove(objectPool.FullName);
  58. }
  59. }
  60. if (currentState)
  61. {
  62. EditorGUILayout.BeginVertical("box");
  63. {
  64. EditorGUILayout.LabelField("Name", objectPool.Name);
  65. EditorGUILayout.LabelField("Type", objectPool.ObjectType.FullName);
  66. EditorGUILayout.LabelField("Auto Release Interval", objectPool.AutoReleaseInterval.ToString());
  67. EditorGUILayout.LabelField("Capacity", objectPool.Capacity.ToString());
  68. EditorGUILayout.LabelField("Used Count", objectPool.Count.ToString());
  69. EditorGUILayout.LabelField("Can Release Count", objectPool.CanReleaseCount.ToString());
  70. EditorGUILayout.LabelField("Expire Time", objectPool.ExpireTime.ToString());
  71. EditorGUILayout.LabelField("Priority", objectPool.Priority.ToString());
  72. ObjectInfo[] objectInfos = objectPool.GetAllObjectInfos();
  73. if (objectInfos.Length > 0)
  74. {
  75. EditorGUILayout.LabelField("Name", objectPool.AllowMultiSpawn ? "Locked\tCount\tFlag\tPriority\tLast Use Time" : "Locked\tIn Use\tFlag\tPriority\tLast Use Time");
  76. foreach (ObjectInfo objectInfo in objectInfos)
  77. {
  78. EditorGUILayout.LabelField(string.IsNullOrEmpty(objectInfo.Name) ? "<None>" : objectInfo.Name, Utility.Text.Format("{0}\t{1}\t{2}\t{3}\t{4}", objectInfo.Locked.ToString(), objectPool.AllowMultiSpawn ? objectInfo.SpawnCount.ToString() : objectInfo.IsInUse.ToString(), objectInfo.CustomCanReleaseFlag.ToString(), objectInfo.Priority.ToString(), objectInfo.LastUseTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")));
  79. }
  80. if (GUILayout.Button("Release"))
  81. {
  82. objectPool.Release();
  83. }
  84. if (GUILayout.Button("Release All Unused"))
  85. {
  86. objectPool.ReleaseAllUnused();
  87. }
  88. if (GUILayout.Button("Export CSV Data"))
  89. {
  90. string exportFileName = EditorUtility.SaveFilePanel("Export CSV Data", string.Empty, Utility.Text.Format("Object Pool Data - {0}.csv", objectPool.Name), string.Empty);
  91. if (!string.IsNullOrEmpty(exportFileName))
  92. {
  93. try
  94. {
  95. int index = 0;
  96. string[] data = new string[objectInfos.Length + 1];
  97. data[index++] = Utility.Text.Format("Name,Locked,{0},Custom Can Release Flag,Priority,Last Use Time", objectPool.AllowMultiSpawn ? "Count" : "In Use");
  98. foreach (ObjectInfo objectInfo in objectInfos)
  99. {
  100. data[index++] = Utility.Text.Format("{0},{1},{2},{3},{4},{5}", objectInfo.Name, objectInfo.Locked.ToString(), objectPool.AllowMultiSpawn ? objectInfo.SpawnCount.ToString() : objectInfo.IsInUse.ToString(), objectInfo.CustomCanReleaseFlag.ToString(), objectInfo.Priority.ToString(), objectInfo.LastUseTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"));
  101. }
  102. File.WriteAllLines(exportFileName, data, Encoding.UTF8);
  103. Debug.Log(Utility.Text.Format("Export object pool CSV data to '{0}' success.", exportFileName));
  104. }
  105. catch (Exception exception)
  106. {
  107. Debug.LogError(Utility.Text.Format("Export object pool CSV data to '{0}' failure, exception is '{1}'.", exportFileName, exception.ToString()));
  108. }
  109. }
  110. }
  111. }
  112. else
  113. {
  114. GUILayout.Label("Object Pool is Empty ...");
  115. }
  116. }
  117. EditorGUILayout.EndVertical();
  118. EditorGUILayout.Separator();
  119. }
  120. }
  121. }
  122. }