记录下,以后可以有个参考
绘制UI使用的是style.Draw(position, gUIContent, id, DragAndDrop.activeControlID == id);
这个style其实就是EditorStyles.objectField
有ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);的
码块是后面那个圈圈的点击事件,点击后弹出对象选择面板
拖拽处理在EventType.DragUpdated,EventType.DragPerform的case块代码中
public static Object ObjectField(Rect position, GUIContent label, Object obj, Type objType, bool allowSceneObjects) { int controlID = GUIUtility.GetControlID(EditorGUI.s_ObjectFieldHash, FocusType.Keyboard, position); position = EditorGUI.PrefixLabel(position, controlID, label); if (EditorGUIUtility.HasObjectThumbnail(objType) && position.height > 16f) { float num = Mathf.Min(position.width, position.height); position.height = num; position.xMin = position.xMax - num; } return EditorGUI.DoObjectField(position, position, controlID, obj, objType, null, null, allowSceneObjects); } internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, Type objType, SerializedProperty property, EditorGUI.ObjectFieldValidator validator, bool allowSceneObjects) { return EditorGUI.DoObjectField(position, dropRect, id, obj, objType, property, validator, allowSceneObjects, EditorStyles.objectField); } internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, Type objType, SerializedProperty property, EditorGUI.ObjectFieldValidator validator, bool allowSceneObjects, GUIStyle style) { if (validator == null) { if (EditorGUI.<>f__mg$cache5 == null) { EditorGUI.<>f__mg$cache5 = new EditorGUI.ObjectFieldValidator(EditorGUI.ValidateObjectFieldAssignment); } validator = EditorGUI.<>f__mg$cache5; } Event current = Event.current; EventType eventType = current.type; if (!GUI.enabled && GUIClip.enabled && Event.current.rawType == EventType.MouseDown) { eventType = Event.current.rawType; } bool flag = EditorGUIUtility.HasObjectThumbnail(objType); EditorGUI.ObjectFieldVisualType objectFieldVisualType = EditorGUI.ObjectFieldVisualType.IconAndText; if (flag && position.height <= 18f && position.width <= 32f) { objectFieldVisualType = EditorGUI.ObjectFieldVisualType.MiniPreview; } else { if (flag && position.height > 16f) { objectFieldVisualType = EditorGUI.ObjectFieldVisualType.LargePreview; } } Vector2 iconSize = EditorGUIUtility.GetIconSize(); if (objectFieldVisualType == EditorGUI.ObjectFieldVisualType.IconAndText) { EditorGUIUtility.SetIconSize(new Vector2(12f, 12f)); } else { if (objectFieldVisualType == EditorGUI.ObjectFieldVisualType.LargePreview) { EditorGUIUtility.SetIconSize(new Vector2(64f, 64f)); } } Object result; switch (eventType) { case EventType.KeyDown: if (GUIUtility.keyboardControl == id) { if (current.keyCode == KeyCode.Backspace || current.keyCode == KeyCode.Delete) { if (property != null) { property.objectReferenceValue = null; } else { obj = null; } GUI.changed = true; current.Use(); } if (current.MainActionKeyForControl(id)) { ObjectSelector.get.Show(obj, objType, property, allowSceneObjects); ObjectSelector.get.objectSelectorID = id; current.Use(); GUIUtility.ExitGUI(); } } goto IL_664; case EventType.KeyUp: case EventType.ScrollWheel: case EventType.Layout: IL_119: if (eventType != EventType.ExecuteCommand) { if (eventType == EventType.DragExited) { if (GUI.enabled) { HandleUtility.Repaint(); } goto IL_664; } if (eventType != EventType.MouseDown) { goto IL_664; } if (Event.current.button != 0) { goto IL_664; } if (position.Contains(Event.current.mousePosition)) { Rect rect; switch (objectFieldVisualType) { case EditorGUI.ObjectFieldVisualType.IconAndText: case EditorGUI.ObjectFieldVisualType.MiniPreview: rect = new Rect(position.xMax - 15f, position.y, 15f, position.height); break; case EditorGUI.ObjectFieldVisualType.LargePreview: rect = new Rect(position.xMax - 36f, position.yMax - 14f, 36f, 14f); break; default: throw new ArgumentOutOfRangeException(); } EditorGUIUtility.editingTextField = false; if (rect.Contains(Event.current.mousePosition)) { if (GUI.enabled) { GUIUtility.keyboardControl = id; ObjectSelector.get.Show(obj, objType, property, allowSceneObjects); ObjectSelector.get.objectSelectorID = id; current.Use(); GUIUtility.ExitGUI(); } } else { Object @object = (property == null) ? obj : property.objectReferenceValue; Component component = @object as Component; if (component) { @object = component.gameObject; } if (EditorGUI.showMixedValue) { @object = null; } if (Event.current.clickCount == 1) { GUIUtility.keyboardControl = id; EditorGUI.PingObjectOrShowPreviewOnClick(@object, position); current.Use(); } else { if (Event.current.clickCount == 2) { if (@object) { AssetDatabase.OpenAsset(@object); GUIUtility.ExitGUI(); } current.Use(); } } } } goto IL_664; } else { string commandName = current.commandName; if (commandName == "ObjectSelectorUpdated" && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && (property == null || !property.isScript)) { result = EditorGUI.AssignSelectedObject(property, validator, objType, current); return result; } if (!(commandName == "ObjectSelectorClosed") || ObjectSelector.get.objectSelectorID != id || GUIUtility.keyboardControl != id || property == null || !property.isScript) { goto IL_664; } if (ObjectSelector.get.GetInstanceID() == 0) { current.Use(); goto IL_664; } result = EditorGUI.AssignSelectedObject(property, validator, objType, current); return result; } break; case EventType.Repaint: { GUIContent gUIContent; if (EditorGUI.showMixedValue) { gUIContent = EditorGUI.s_MixedValueContent; } else { if (property != null) { gUIContent = EditorGUIUtility.TempContent(property.objectReferenceStringValue, AssetPreview.GetMiniThumbnail(property.objectReferenceValue)); obj = property.objectReferenceValue; if (obj != null) { Object[] references = new Object[] { obj }; if (EditorSceneManager.preventCrossSceneReferences && EditorGUI.CheckForCrossSceneReferencing(obj, property.serializedObject.targetObject)) { if (!EditorApplication.isPlaying) { gUIContent = EditorGUI.s_SceneMismatch; } else { gUIContent.text += string.Format(" ({0})", EditorGUI.GetGameObjectFromObject(obj).scene.name); } } else { if (validator(references, objType, property, EditorGUI.ObjectFieldValidatorOptions.ExactObjectTypeValidation) == null) { gUIContent = EditorGUI.s_TypeMismatch; } } } } else { gUIContent = EditorGUIUtility.ObjectContent(obj, objType); } } switch (objectFieldVisualType) { case EditorGUI.ObjectFieldVisualType.IconAndText: EditorGUI.BeginHandleMixedValueContentColor(); style.Draw(position, gUIContent, id, DragAndDrop.activeControlID == id); EditorGUI.EndHandleMixedValueContentColor(); break; case EditorGUI.ObjectFieldVisualType.LargePreview: EditorGUI.DrawObjectFieldLargeThumb(position, id, obj, gUIContent); break; case EditorGUI.ObjectFieldVisualType.MiniPreview: EditorGUI.DrawObjectFieldMiniThumb(position, id, obj, gUIContent); break; default: throw new ArgumentOutOfRangeException(); } goto IL_664; } case EventType.DragUpdated: case EventType.DragPerform: if (dropRect.Contains(Event.current.mousePosition) && GUI.enabled) { Object[] objectReferences = DragAndDrop.objectReferences; Object object2 = validator(objectReferences, objType, property, EditorGUI.ObjectFieldValidatorOptions.None); if (object2 != null) { if (!allowSceneObjects && !EditorUtility.IsPersistent(object2)) { object2 = null; } } if (object2 != null) { DragAndDrop.visualMode = DragAndDropVisualMode.Generic; if (eventType == EventType.DragPerform) { if (property != null) { property.objectReferenceValue = object2; } else { obj = object2; } GUI.changed = true; DragAndDrop.AcceptDrag(); DragAndDrop.activeControlID = 0; } else { DragAndDrop.activeControlID = id; } Event.current.Use(); } } goto IL_664; } goto IL_119; IL_664: EditorGUIUtility.SetIconSize(iconSize); result = obj; return result; }