using TyphoonGUIStyle; using UnityEditor; using UnityEngine; namespace TyphoonPool { [CustomEditor(typeof(Pool))] public class PoolInspector : Editor { private const float INFO_TITLE_WIDTH = 120; private const float NUMBER_WIDTH = 120; private const float STATUS_WIDTH = 80; private Pool Target => target as Pool; private static GUIStyle _styleStatusOn = null; private static GUIStyle StyleStatusOn { get { if (_styleStatusOn == null) { _styleStatusOn = new GUIStyle(Styles.TitleBar); _styleStatusOn.normal.textColor = EditorGUIUtility.isProSkin ? Color.green : new Color(0f, 0.8f, 0f, 1f); } return _styleStatusOn; } } private static GUIStyle _styleStatusMissing = null; private static GUIStyle StyleStatusMissing { get { if (_styleStatusMissing == null) { _styleStatusMissing = new GUIStyle(Styles.TitleBar); _styleStatusMissing.normal.textColor = EditorGUIUtility.isProSkin ? Color.red : new Color(0.8f, 0f, 0f, 1f); } return _styleStatusMissing; } } private static GUIStyle _elementLabel = null; private static GUIStyle ElementLabel { get { if (_elementLabel == null) { _elementLabel = new GUIStyle(Styles.TitleBar); _elementLabel.fontStyle = FontStyle.Normal; } return _elementLabel; } } private int _total; private int _busyCount; private int _missingCount; public override void OnInspectorGUI() { base.OnInspectorGUI(); //绘制池对象大小 var map = Target.MapEnumerator; GUILayout.BeginHorizontal(); GUILayout.Label($"名称", ElementLabel, GUILayout.Width(INFO_TITLE_WIDTH)); GUILayout.TextArea($"{Target.name}", ElementLabel); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label($"Pool ID", ElementLabel, GUILayout.Width(INFO_TITLE_WIDTH)); GUILayout.TextArea($"{Target.PoolID}", ElementLabel); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label($"永不销毁", ElementLabel, GUILayout.Width(INFO_TITLE_WIDTH)); GUILayout.TextArea($"{Target.IsNeverDestroyInLoad}", ElementLabel); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label($"数量状况", ElementLabel, GUILayout.Width(INFO_TITLE_WIDTH)); GUILayout.TextArea($"{_busyCount}/{_total}", ElementLabel); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label($"丢失数", ElementLabel, GUILayout.Width(INFO_TITLE_WIDTH)); GUILayout.TextArea($"{_missingCount}", ElementLabel); GUILayout.EndHorizontal(); GUILayout.Space(10); //标题栏 GUILayout.BeginHorizontal(); GUILayout.Label("Object ID", Styles.TitleBar, GUILayout.Width(NUMBER_WIDTH)); GUILayout.Label("PoolObject", Styles.TitleBar); GUILayout.Label("状态", Styles.TitleBar, GUILayout.Width(STATUS_WIDTH)); GUILayout.EndHorizontal(); _total = 0; _busyCount = 0; _missingCount = 0; while (map.MoveNext()) { _total += 1; var obj = map.Current; var poolObject = obj.Value; var objectID = obj.Key; GUILayout.BeginHorizontal(); GUILayout.TextArea(objectID.ToString(), ElementLabel, GUILayout.Width(NUMBER_WIDTH)); GUILayout.Label("", ElementLabel); var last = GUILayoutUtility.GetLastRect(); var component = poolObject as Component; if (component != null) { EditorGUI.ObjectField(last, component, typeof(Object)); } else { GUI.Label(last, poolObject.ToString()); } if (poolObject != null) { var operation = poolObject.PoolObjectOperation; if (operation.IsBusy) { _busyCount += 1; } GUILayout.Label(operation.IsBusy ? " 使用中" : "空闲", operation.IsBusy ? StyleStatusOn : ElementLabel, GUILayout.Width(STATUS_WIDTH)); } else { GUILayout.Label("丢失", StyleStatusMissing, GUILayout.Width(STATUS_WIDTH)); _missingCount += 1; } GUILayout.EndHorizontal(); } Repaint(); } } }