Exemplo n.º 1
0
        public static bool HandleOpenAsset(int instanceID, int line)
        {
            Object obj = EditorUtility.InstanceIDToObject(instanceID);

            if (obj == null)
            {
                return(false);
            }

            Type type = obj.GetType();

            if (type.IsSubclassOf(typeof(ScriptableObject)))
            {
                ScriptableObjectInspectorWindow.Init((ScriptableObject)obj);
                return(true);
            }

            return(false);            // Not a scriptable object
        }
        /// <inheritdoc />
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            assetType = fieldInfo.DeclaringType;

            // Field with wrong type
            if (assetType == null || !assetType.IsSubclassOf(typeof(ScriptableObject)))
            {
                EditorGUI.PropertyField(position, property);
                return;
            }

            Event currentEvent = Event.current;

            // Empty field
            if (property.objectReferenceValue == null)
            {
                if (currentEvent.type == EventType.ContextClick && position.Contains(currentEvent.mousePosition))
                {
                    CreateAndShowMenuContext(property);
                    currentEvent.Use();
                }

                EditorGUI.PropertyField(position, property);
                return;
            }

            var indentLevel = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            position.xMin        += indentLevel * EditorGUIUtility.singleLineHeight;
            var rect = position;

            // Drag Area
            rect.width = DragButtonWidth;
            GUI.Box(rect, GUIContent.none, Styles.SelectButtonStyle);
            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);
            switch (currentEvent.type)
            {
            case EventType.MouseDown:
                // Handle object dragging
                if (rect.Contains(currentEvent.mousePosition))
                {
                    DragAndDrop.PrepareStartDrag();
                    activeSelection[0]           = property.objectReferenceValue;
                    DragAndDrop.objectReferences = activeSelection;
                    DragAndDrop.StartDrag("Drag scriptableObject");
                    currentEvent.Use();
                }

                break;

            case EventType.DragUpdated:
            case EventType.DragPerform:
                // Handle clicking the icon
                if (rect.Contains(currentEvent.mousePosition))
                {
                    break;
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (currentEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    EditorGUIUtility.PingObject(DragAndDrop.objectReferences[0]);
                }

                break;
            }

            // Property field
            rect       = position;
            rect.xMin += DragButtonWidth + 2f;
            rect.xMax -= GotoButtonWidth;
            EditorGUI.PropertyField(rect, property, GUIContent.none, false);

            // Goto button
            var goToItemRect = rect;

            goToItemRect.xMin   = rect.xMax + 2;
            goToItemRect.width  = GotoButtonWidth;
            goToItemRect.y     += (rect.height - GotoButtonHeight) / 2.0f;
            goToItemRect.height = GotoButtonHeight;

            if (GUI.Button(goToItemRect, GUIContent.none, Styles.GotoSubAssetStyle))
            {
                if (draggableAttribute == null)
                {
                    draggableAttribute = attribute as DraggableAttribute;
                }

                if (draggableAttribute != null && draggableAttribute.OpenInNewWindow)
                {
                    ScriptableObjectInspectorWindow.Init(property.objectReferenceValue as ScriptableObject);
                }
                else
                {
                    Selection.activeObject = property.objectReferenceValue;
                }
            }

            EditorGUI.indentLevel = indentLevel;
        }