/// <summary>
        /// Initialize the window
        /// </summary>
        /// <param name="scriptableObject">ScriptableObject the window is connected to.</param>
        public static void Init(ScriptableObject scriptableObject)
        {
            for (int i = 0; i < openWindows.Count; i++)
            {
                var currentWindow = openWindows[i];
                // Check for the name to offer solution when the editor was closed
                if (currentWindow.titleContent.text == scriptableObject.name)
                {
                    if (currentWindow.scriptableObject == null)
                    {
                        currentWindow.SetScriptableObject(scriptableObject);
                    }

                    currentWindow.Focus();
                    currentWindow.Show();
                    return;
                }
            }

            ScriptableObjectInspectorWindow window = (ScriptableObjectInspectorWindow)EditorWindow.CreateInstance(typeof(ScriptableObjectInspectorWindow));
            var titleText    = scriptableObject.name;
            var titleContent = new GUIContent(titleText);

            if (titleText.Length < 12)
            {
                titleContent.image = AssetPreview.GetMiniThumbnail(scriptableObject);
            }

            window.titleContent = titleContent;
            window.SetScriptableObject(scriptableObject);
            openWindows.Add(window);
            window.Show();
        }
Пример #2
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;
        }