|
|
@@ -1,14 +1,18 @@
|
|
|
-using System;
|
|
|
+using System;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEditor;
|
|
|
using UnityEngine;
|
|
|
+//Object并非C#基础中的Object,而是 UnityEngine.Object
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
|
+//自定义ReferenceCollector类在界面中的显示与功能
|
|
|
[CustomEditor(typeof (ReferenceCollector))]
|
|
|
+//没有该属性的编辑器在选中多个物体时会提示“Multi-object editing not supported”
|
|
|
[CanEditMultipleObjects]
|
|
|
public class ReferenceCollectorEditor: Editor
|
|
|
{
|
|
|
- private string searchKey
|
|
|
+ //输入在textfield中的字符串
|
|
|
+ private string searchKey
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
@@ -45,17 +49,23 @@ public class ReferenceCollectorEditor: Editor
|
|
|
|
|
|
private void OnEnable()
|
|
|
{
|
|
|
- referenceCollector = (ReferenceCollector) target;
|
|
|
+ //将被选中的gameobject所挂载的ReferenceCollector赋值给编辑器类中的ReferenceCollector,方便操作
|
|
|
+ referenceCollector = (ReferenceCollector) target;
|
|
|
}
|
|
|
|
|
|
public override void OnInspectorGUI()
|
|
|
{
|
|
|
- Undo.RecordObject(referenceCollector, "Changed Settings");
|
|
|
+ //使ReferenceCollector支持撤销操作,还有Redo,不过没有在这里使用
|
|
|
+ Undo.RecordObject(referenceCollector, "Changed Settings");
|
|
|
var dataProperty = serializedObject.FindProperty("data");
|
|
|
+ //开始水平布局,如果是比较新版本学习U3D的,可能不知道这东西,这个是老GUI系统的知识,除了用在编辑器里,还可以用在生成的游戏中
|
|
|
GUILayout.BeginHorizontal();
|
|
|
+ //下面几个if都是点击按钮就会返回true调用里面的东西
|
|
|
if (GUILayout.Button("添加引用"))
|
|
|
{
|
|
|
- AddReference(dataProperty, Guid.NewGuid().GetHashCode().ToString(), null);
|
|
|
+ //添加新的元素,具体的函数注释
|
|
|
+ // Guid.NewGuid().GetHashCode().ToString() 就是新建后默认的key
|
|
|
+ AddReference(dataProperty, Guid.NewGuid().GetHashCode().ToString(), null);
|
|
|
}
|
|
|
if (GUILayout.Button("全部删除"))
|
|
|
{
|
|
|
@@ -71,8 +81,11 @@ public class ReferenceCollectorEditor: Editor
|
|
|
}
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
- searchKey = EditorGUILayout.TextField(searchKey);
|
|
|
- EditorGUILayout.ObjectField(heroPrefab, typeof (Object), false);
|
|
|
+ //可以在编辑器中对searchKey进行赋值,只要输入对应的Key值,就可以点后面的删除按钮删除相对应的元素
|
|
|
+ searchKey = EditorGUILayout.TextField(searchKey);
|
|
|
+ //添加的可以用于选中Object的框,这里的object也是(UnityEngine.Object
|
|
|
+ //第三个参数为是否只能引用scene中的Object
|
|
|
+ EditorGUILayout.ObjectField(heroPrefab, typeof (Object), false);
|
|
|
if (GUILayout.Button("删除"))
|
|
|
{
|
|
|
referenceCollector.Remove(searchKey);
|
|
|
@@ -83,21 +96,25 @@ public class ReferenceCollectorEditor: Editor
|
|
|
|
|
|
var delList = new List<int>();
|
|
|
SerializedProperty property;
|
|
|
+ //遍历ReferenceCollector中data list的所有元素,显示在编辑器中
|
|
|
for (int i = referenceCollector.data.Count - 1; i >= 0; i--)
|
|
|
{
|
|
|
GUILayout.BeginHorizontal();
|
|
|
+ //这里的知识点在ReferenceCollector中有说
|
|
|
property = dataProperty.GetArrayElementAtIndex(i).FindPropertyRelative("key");
|
|
|
EditorGUILayout.TextField(property.stringValue, GUILayout.Width(150));
|
|
|
property = dataProperty.GetArrayElementAtIndex(i).FindPropertyRelative("gameObject");
|
|
|
EditorGUILayout.ObjectField(property.objectReferenceValue, typeof(Object), true);
|
|
|
if (GUILayout.Button("X"))
|
|
|
{
|
|
|
+ //将元素添加进删除list
|
|
|
delList.Add(i);
|
|
|
}
|
|
|
GUILayout.EndHorizontal();
|
|
|
}
|
|
|
var eventType = Event.current.type;
|
|
|
- if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
|
|
|
+ //在Inspector 窗口上创建区域,向区域拖拽资源对象,获取到拖拽到区域的对象
|
|
|
+ if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
|
|
|
{
|
|
|
// Show a copy icon on the drag
|
|
|
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
|
|
|
@@ -113,6 +130,8 @@ public class ReferenceCollectorEditor: Editor
|
|
|
|
|
|
Event.current.Use();
|
|
|
}
|
|
|
+
|
|
|
+ //遍历删除list,将其删除掉
|
|
|
foreach (var i in delList)
|
|
|
{
|
|
|
dataProperty.DeleteArrayElementAtIndex(i);
|
|
|
@@ -121,7 +140,8 @@ public class ReferenceCollectorEditor: Editor
|
|
|
serializedObject.UpdateIfRequiredOrScript();
|
|
|
}
|
|
|
|
|
|
- private void AddReference(SerializedProperty dataProperty, string key, Object obj)
|
|
|
+ //添加元素,具体知识点在ReferenceCollector中说了
|
|
|
+ private void AddReference(SerializedProperty dataProperty, string key, Object obj)
|
|
|
{
|
|
|
int index = dataProperty.arraySize;
|
|
|
dataProperty.InsertArrayElementAtIndex(index);
|