ReferenceCollectorEditor.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using Fort23.Mono;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace Fort23.Editor
  8. {
  9. [CustomEditor(typeof(ReferenceCollector))]
  10. public class ReferenceCollectorEditor : UnityEditor.Editor
  11. {
  12. private string searchKey
  13. {
  14. get { return _searchKey; }
  15. set
  16. {
  17. if (_searchKey != value)
  18. {
  19. _searchKey = value;
  20. heroPrefab = referenceCollector.Get<Object>(searchKey);
  21. }
  22. }
  23. }
  24. private ReferenceCollector referenceCollector;
  25. private Object heroPrefab;
  26. private string _searchKey = "";
  27. [SerializeField] public bool isAssetBundle;
  28. private void DelNullReference()
  29. {
  30. var dataProperty = serializedObject.FindProperty("data");
  31. for (int i = dataProperty.arraySize - 1; i >= 0; i--)
  32. {
  33. var gameObjectProperty = dataProperty.GetArrayElementAtIndex(i).FindPropertyRelative("gameObject");
  34. if (gameObjectProperty.objectReferenceValue == null)
  35. {
  36. dataProperty.DeleteArrayElementAtIndex(i);
  37. }
  38. }
  39. }
  40. private void OnEnable()
  41. {
  42. referenceCollector = (ReferenceCollector)target;
  43. }
  44. public override void OnInspectorGUI()
  45. {
  46. //支持撤销,Redo
  47. Undo.RecordObject(referenceCollector, "Changed Settings");
  48. var dataProperty = serializedObject.FindProperty("data");
  49. var isAssetBundle = serializedObject.FindProperty("isAssetBundle");
  50. isAssetBundle.boolValue = GUILayout.Toggle(isAssetBundle.boolValue, "是否显示打包文件夹(只有AB包设置文件用得到这个选项)");
  51. GUILayout.BeginHorizontal();
  52. if (GUILayout.Button("添加引用"))
  53. {
  54. // Guid.NewGuid().GetHashCode().SplitToString() 是新建后默认的key
  55. AddReference(dataProperty, Guid.NewGuid().GetHashCode().ToString(), null);
  56. }
  57. if (GUILayout.Button("全部删除"))
  58. {
  59. dataProperty.ClearArray();
  60. }
  61. if (GUILayout.Button("删除空引用"))
  62. {
  63. DelNullReference();
  64. }
  65. if (GUILayout.Button("排序"))
  66. {
  67. referenceCollector.Sort();
  68. }
  69. EditorGUILayout.EndHorizontal();
  70. EditorGUILayout.BeginHorizontal();
  71. //可以在编辑器中对searchKey进行赋值,只要输入对应的Key值,就可以点后面的删除按钮删除相对应的元素
  72. searchKey = EditorGUILayout.TextField(searchKey);
  73. //添加的可以用于选中Object的框,这里的object也是(UnityEngine.Object
  74. //第三个参数为是否只能引用scene中的Object
  75. EditorGUILayout.ObjectField(heroPrefab, typeof(Object), false);
  76. if (GUILayout.Button("删除"))
  77. {
  78. referenceCollector.Remove(searchKey);
  79. heroPrefab = null;
  80. }
  81. GUILayout.EndHorizontal();
  82. EditorGUILayout.Space();
  83. var delList = new List<int>();
  84. SerializedProperty property;
  85. //遍历ReferenceCollector中data list的所有元素,显示在编辑器中
  86. for (int i = referenceCollector.data.Count - 1; i >= 0; i--)
  87. {
  88. GUILayout.BeginHorizontal();
  89. SerializedProperty rcDataSp = dataProperty.GetArrayElementAtIndex(i);
  90. if (rcDataSp == null)
  91. {
  92. continue;
  93. }
  94. SerializedProperty listSp = rcDataSp.FindPropertyRelative("isList");
  95. //这里的知识点在ReferenceCollector中有说
  96. if (dataProperty.arraySize > i)
  97. {
  98. if (isAssetBundle.boolValue)
  99. {
  100. property = rcDataSp.FindPropertyRelative("isAssetBundle");
  101. property.boolValue = EditorGUILayout.ToggleLeft("文件夹打AB", property.boolValue, GUILayout.Width(100));
  102. listSp.boolValue = EditorGUILayout.ToggleLeft("是否是List", listSp.boolValue, GUILayout.Width(100));
  103. property = rcDataSp.FindPropertyRelative("key");
  104. property.stringValue = EditorGUILayout.TextField(property.stringValue, GUILayout.Width(100));
  105. property = rcDataSp.FindPropertyRelative("gameObject");
  106. property.objectReferenceValue = EditorGUILayout.ObjectField(property.objectReferenceValue, typeof(Object), true);
  107. }
  108. else
  109. {
  110. property = rcDataSp.FindPropertyRelative("key");
  111. property.stringValue = EditorGUILayout.TextField(property.stringValue, GUILayout.Width(150));
  112. property = rcDataSp.FindPropertyRelative("gameObject");
  113. property.objectReferenceValue = EditorGUILayout.ObjectField(property.objectReferenceValue, typeof(Object), true);
  114. }
  115. if (GUILayout.Button("X"))
  116. {
  117. //将元素添加进删除list
  118. delList.Add(i);
  119. }
  120. }
  121. GUILayout.EndHorizontal();
  122. if (listSp.boolValue)
  123. {
  124. string key = rcDataSp.FindPropertyRelative("key").stringValue;
  125. SerializedProperty ListCollectorDatas = rcDataSp.FindPropertyRelative("ListCollectorDatas");
  126. EditorGUILayout.PropertyField(ListCollectorDatas, new GUIContent(key));
  127. }
  128. }
  129. var eventType = Event.current.type;
  130. //在Inspector 窗口上创建区域,向区域拖拽资源对象,获取到拖拽到区域的对象
  131. if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
  132. {
  133. // ShowForHall a copy icon on the drag
  134. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  135. if (eventType == EventType.DragPerform)
  136. {
  137. DragAndDrop.AcceptDrag();
  138. foreach (var o in DragAndDrop.objectReferences)
  139. {
  140. AddReference(dataProperty, o.name, o);
  141. }
  142. }
  143. Event.current.Use();
  144. }
  145. //遍历删除list,将其删除掉
  146. foreach (var i in delList)
  147. {
  148. dataProperty.DeleteArrayElementAtIndex(i);
  149. }
  150. serializedObject.ApplyModifiedProperties();
  151. serializedObject.UpdateIfRequiredOrScript();
  152. }
  153. //添加元素
  154. private void AddReference(SerializedProperty dataProperty, string key, Object obj)
  155. {
  156. int index = dataProperty.arraySize;
  157. dataProperty.InsertArrayElementAtIndex(index);
  158. var element = dataProperty.GetArrayElementAtIndex(index);
  159. element.FindPropertyRelative("key").stringValue = key;
  160. element.FindPropertyRelative("gameObject").objectReferenceValue = obj;
  161. }
  162. }
  163. }