Пример #1
0
    private static void CreateScriptableObject()
    {
        ClassSelectionEditorWindow.Show(Vector2.zero, typeof(SerializedScriptableObject), (Type selectedType) => { CreateAsset(selectedType); });

        /// <summary>
        //	This makes it easy to create, name and place unique new ScriptableObject asset files.
        /// </summary>
        void CreateAsset(Type soType)
        {
            ScriptableObject asset = ScriptableObject.CreateInstance(soType);

            string path = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (path == "")
            {
                path = "Assets";
            }
            else if (Path.GetExtension(path) != "")
            {
                path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
            }

            string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + soType.ToString() + ".asset");

            AssetDatabase.CreateAsset(asset, assetPathAndName);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = asset;
        }
    }
Пример #2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        try
        {
            EditorPersistantBoolVariable.Initialize(ref this.folded, EditorPersistantBoolVariable.BuildKeyFromObject(property.serializedObject.targetObject, property.name));

            Inline byEnumProperty = (Inline)attribute;
            EditorGUI.BeginChangeCheck();
            string propertyName = "";
            for (var i = 0; i < EditorGUI.indentLevel; i++)
            {
                propertyName += " ";
            }

            propertyName += property.name;

            this.folded.SetValue(EditorGUILayout.BeginFoldoutHeaderGroup(this.folded.GetValue(), propertyName));
            EditorGUILayout.EndFoldoutHeaderGroup();

            if (EditorGUI.EndChangeCheck())
            {
                if (!this.folded.GetValue())
                {
                    if (this.inlineEditor != null)
                    {
                        Editor.DestroyImmediate(inlineEditor);
                        this.inlineEditor = null;
                    }
                }
            }

            if (this.folded.GetValue())
            {
                EditorGUI.indentLevel += 1;
                EditorGUILayout.PropertyField(property);
                EditorGUI.indentLevel -= 1;

                if (property.propertyType == SerializedPropertyType.ObjectReference && property.objectReferenceValue != null)
                {
                    if (this.inlineEditor == null)
                    {
                        inlineEditor = DynamicEditorCreation.Get().CreateEditor(property.objectReferenceValue);
                    }

                    if (this.inlineEditor != null)
                    {
                        EditorGUI.indentLevel += 1;
                        this.inlineEditor.OnInspectorGUI();
                        EditorGUI.indentLevel -= 1;
                    }
                }
                else
                {
                    if (EditorUtility.IsPersistent(property.serializedObject.targetObject))
                    {
                        if (byEnumProperty.CreateAtSameLevelIfAbsent)
                        {
                            if (GUILayout.Button(new GUIContent("CREATE")))
                            {
                                var fieldType = SerializableObjectHelper.GetPropertyFieldInfo(property).FieldType;
                                if (fieldType.IsAbstract || fieldType.IsInterface)
                                {
                                    //Open popup to select implementation
                                    ClassSelectionEditorWindow.Show(Event.current.mousePosition, fieldType, (Type selectedType) =>
                                    {
                                        var createdAsset = AssetHelper.CreateAssetAtSameDirectoryLevel((ScriptableObject)property.serializedObject.targetObject, selectedType.Name, property.name);
                                        property.objectReferenceValue = (Object)createdAsset;
                                    });
                                }
                                else
                                {
                                    var createdAsset = AssetHelper.CreateAssetAtSameDirectoryLevel((ScriptableObject)property.serializedObject.targetObject, property.type.Replace("PPtr<$", "").Replace(">", ""), property.name);
                                    property.objectReferenceValue = (Object)createdAsset;
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
        }
    }
Пример #3
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        try
        {
            Inline byEnumProperty = (Inline)attribute;

            EditorGUI.BeginProperty(position, label, property);
            EditorGUILayout.BeginVertical(EditorStyles.textArea);

            EditorGUI.BeginChangeCheck();
            this.folded = EditorGUILayout.Foldout(this.folded, property.name, true);
            if (EditorGUI.EndChangeCheck())
            {
                if (!this.folded)
                {
                    if (this.inlineEditor != null)
                    {
                        Editor.DestroyImmediate(inlineEditor);
                        this.inlineEditor = null;
                    }
                }
            }

            if (this.folded)
            {
                EditorGUI.indentLevel += 1;
                EditorGUILayout.PropertyField(property);
                EditorGUI.indentLevel -= 1;

                if (property.objectReferenceValue != null)
                {
                    if (this.inlineEditor == null)
                    {
                        inlineEditor = DynamicEditorCreation.Get().CreateEditor(property.objectReferenceValue);
                    }

                    if (this.inlineEditor != null)
                    {
                        EditorGUI.indentLevel += 1;
                        this.inlineEditor.OnInspectorGUI();
                        EditorGUI.indentLevel -= 1;
                    }
                }
                else
                {
                    if (EditorUtility.IsPersistent(property.serializedObject.targetObject))
                    {
                        if (byEnumProperty.CreateAtSameLevelIfAbsent)
                        {
                            if (GUILayout.Button(new GUIContent("CREATE")))
                            {
                                var fieldType = SerializableObjectHelper.GetPropertyFieldInfo(property).FieldType;
                                if (fieldType.IsAbstract || fieldType.IsInterface)
                                {
                                    //Open popup to select implementation
                                    ClassSelectionEditorWindow.Show(Event.current.mousePosition, fieldType, (Type selectedType) =>
                                    {
                                        var createdAsset = AssetHelper.CreateAssetAtSameDirectoryLevel((ScriptableObject)property.serializedObject.targetObject, selectedType.Name, property.name);
                                        property.objectReferenceValue = (Object)createdAsset;
                                    });
                                }
                                else
                                {
                                    var createdAsset = AssetHelper.CreateAssetAtSameDirectoryLevel((ScriptableObject)property.serializedObject.targetObject, property.type.Replace("PPtr<$", "").Replace(">", ""), property.name);
                                    property.objectReferenceValue = (Object)createdAsset;
                                }
                            }
                        }
                    }
                }
            }

            EditorGUILayout.EndVertical();
            EditorGUI.EndProperty();
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }