/// <summary>
        /// Draw a SerializedProperty as a ReorderableList if it was found during
        /// initialization, otherwise use EditorGUILayout.PropertyField
        /// </summary>
        /// <param name="property"></param>
        protected void DrawPropertySortableArray(SerializedProperty property)
        {
            // Try to get the sortable list this property belongs to
            SortableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.propertyPath.StartsWith(data.Parent));
            }

            if (listData != null)
            {
                // Try to show the list
                if (listData.DoLayoutProperty(property) == false)
                {
                    EditorGUILayout.PropertyField(property, false);
                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);
                        IterateSerializedProp(targetProp);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);
                bool restoreEnable            = GUI.enabled;
                if (targetProp.propertyPath.StartsWith("m_"))
                {
                    GUI.enabled = false;
                }
                EditorGUILayout.PropertyField(targetProp, targetProp.isExpanded);
                GUI.enabled = restoreEnable;
            }
        }
Пример #2
0
        /// <summary>
        /// Draw a SerializedProperty as a ReorderableList if it was found during
        /// initialization, otherwise use EditorGUILayout.PropertyField
        /// </summary>
        /// <param name="property"></param>
        protected void DrawPropertySortableArray(SerializedProperty property)
        {
            // Try to get the sortable list this property belongs to
            SortableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.propertyPath.StartsWith(data.Parent));
            }

            Editor scriptableEditor;
            bool   isScriptableEditor = editableIndex.TryGetValue(property.propertyPath, out scriptableEditor);

            // Has ReorderableList
            if (listData != null)
            {
                // Try to show the list
                if (listData.DoLayoutProperty(property) == false)
                {
                    EditorGUILayout.PropertyField(property, false);
                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);
                        IterateDrawProperty(targetProp);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            // Else try to draw ScriptableObject editor
            else if (isScriptableEditor)
            {
                bool hasHeader = property.HasAttribute <HeaderAttribute>();
                bool hasSpace  = property.HasAttribute <SpaceAttribute>();

                float foldoutSpace = hasHeader ? 24 : 7;
                if (hasHeader && hasSpace)
                {
                    foldoutSpace = 31;
                }

                hasSpace |= hasHeader;

                // No data in property, draw property field with create button
                if (scriptableEditor == null)
                {
                    bool doCreate;
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.PropertyField(property, uiExpandWidth);
                        using (new EditorGUILayout.VerticalScope(uiWidth50))
                        {
                            if (hasSpace)
                            {
                                GUILayout.Space(10);
                            }
                            doCreate = GUILayout.Button(labelBtnCreate, EditorStyles.miniButton);
                        }
                    }

                    if (doCreate)
                    {
                        Type propType     = property.GetTypeReflection();
                        var  createdAsset = CreateAssetWithSavePrompt(propType, "Assets");
                        if (createdAsset != null)
                        {
                            property.objectReferenceValue = createdAsset;
                            property.isExpanded           = true;
                        }
                    }
                }
                // Has data in property, draw foldout and editor
                else
                {
                    EditorGUILayout.PropertyField(property);

                    Rect rectFoldout = GUILayoutUtility.GetLastRect();
                    rectFoldout.width = 20;
                    if (hasSpace)
                    {
                        rectFoldout.yMin += foldoutSpace;
                    }

                    property.isExpanded = EditorGUI.Foldout(rectFoldout, property.isExpanded, GUIContent.none);

                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        using (new EditorGUILayout.VerticalScope(styleEditBox))
                        {
                            var restoreIndent = EditorGUI.indentLevel;
                            EditorGUI.indentLevel = 1;
                            scriptableEditor.serializedObject.Update();
                            scriptableEditor.OnInspectorGUI();
                            scriptableEditor.serializedObject.ApplyModifiedProperties();
                            EditorGUI.indentLevel = restoreIndent;
                        }
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);

                bool isStartProp = targetProp.propertyPath.StartsWith("m_");
                using (new EditorGUI.DisabledScope(isStartProp))
                {
                    EditorGUILayout.PropertyField(targetProp, targetProp.isExpanded);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Draw a SerializedProperty as a ReorderableList if it was found during
        /// initialization, otherwise use EditorGUILayout.PropertyField
        /// </summary>
        /// <param name="property"></param>
        protected void DrawPropertySortableArray(SerializedProperty property)
        {
            // Try to get the sortable list this property belongs to
            SortableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.propertyPath.StartsWith(data.Parent));
            }

            Editor scriptableEditor;
            bool   isScriptableEditor = editableIndex.TryGetValue(property.propertyPath, out scriptableEditor);

            if (listData != null)
            {
                // Try to show the list
                if (listData.DoLayoutProperty(property) == false)
                {
                    EditorGUILayout.PropertyField(property, false);
                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);
                        IterateDrawProperty(targetProp);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else if (isScriptableEditor)
            {
                if (scriptableEditor == null)
                {
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.PropertyField(property, uiExpandWidth);
                        if (GUILayout.Button(labelBtnCreate, EditorStyles.miniButton, uiWidth50))
                        {
                            Type propType     = property.GetTypeReflection();
                            var  createdAsset = CreateAssetWithSavePrompt(propType, "Assets");
                            if (createdAsset != null)
                            {
                                property.objectReferenceValue = createdAsset;
                            }
                        }
                    }
                }
                else
                {
                    EditorGUILayout.PropertyField(property);
                    Rect rectFoldout = GUILayoutUtility.GetLastRect();
                    rectFoldout.width   = 20;
                    property.isExpanded = EditorGUI.Foldout(rectFoldout, property.isExpanded, GUIContent.none);

                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        scriptableEditor.OnInspectorGUI();
                        GUILayout.Box(GUIContent.none, uiHeight2, uiExpandWidth);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);

                bool isStartProp = targetProp.propertyPath.StartsWith("m_");
                using (new EditorGUI.DisabledScope(isStartProp))
                {
                    EditorGUILayout.PropertyField(targetProp, targetProp.isExpanded);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Draw a SerializedProperty as a ReorderableList if it was found during
        /// initialization, otherwise use EditorGUILayout.PropertyField
        /// </summary>
        /// <param name="property"></param>
        protected void DrawPropertySortableArray(SerializedProperty property)
        {
            // Try to get the sortable list this property belongs to
            SortableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.propertyPath.StartsWith(data.Parent));
            }

            Editor scriptableEditor;
            bool   isScriptableEditor = editableIndex.TryGetValue(property.propertyPath, out scriptableEditor);

            // Has ReorderableList
            if (listData != null)
            {
                // Try to show the list
                if (listData.DoLayoutProperty(property) == false)
                {
                    EditorGUILayout.PropertyField(property, false);
                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);
                        IterateDrawProperty(targetProp);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            // Else try to draw ScriptableObject editor
            else if (isScriptableEditor)
            {
                bool hasHeader = property.HasAttribute <HeaderAttribute>();
                bool hasSpace  = property.HasAttribute <SpaceAttribute>();

                float foldoutSpace = hasHeader ? 24 : 7;
                if (hasHeader && hasSpace)
                {
                    foldoutSpace = 31;
                }

                hasSpace |= hasHeader;

                // No data in property, draw property field with create button
                if (scriptableEditor == null)
                {
                    bool        doCreate;
                    List <Type> subclasses = new List <Type>();
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.PropertyField(property, uiExpandWidth);
                        using (new EditorGUILayout.VerticalScope(uiWidth50))
                        {
                            using (new EditorGUILayout.HorizontalScope())
                            {
                                Type propType = property.GetTypeReflection();
                                subclasses = Assembly.GetAssembly(propType).GetTypes().Where(x => x.IsSubclassOf(propType)).ToList();

                                if (subclasses.Count > 0)
                                {
                                    string keyname = this.GetType().Name + target.name;
                                    if (!nv.editor.EditorData.Instance.HasData <int>(keyname, property.propertyPath))
                                    {
                                        nv.editor.EditorData.Instance.SetData <int>(0, keyname, property.propertyPath);
                                    }

                                    int selection = nv.editor.EditorData.Instance.GetData <int>(keyname, property.propertyPath);
                                    selection = EditorGUILayout.Popup(selection, subclasses.Select(x => x.Name).ToArray());
                                    nv.editor.EditorData.Instance.SetData <int>(selection, keyname, property.propertyPath);
                                }

                                if (hasSpace)
                                {
                                    GUILayout.Space(10);
                                }
                                doCreate = GUILayout.Button(labelBtnCreate, EditorStyles.miniButton);
                            }
                        }
                    }

                    if (doCreate)
                    {
                        string keyname  = this.GetType().Name + target.name;
                        Type   propType = property.GetTypeReflection();
                        if (subclasses.Count > 0)
                        {
                            int selection = nv.editor.EditorData.Instance.GetData <int>(keyname, property.propertyPath);
                            propType = subclasses[selection];
                        }

                        string pwd = nv.editor.EditorData.Instance.GetData <string>(keyname, property.propertyPath);
                        if (string.IsNullOrEmpty(pwd))
                        {
                            pwd = Application.dataPath;
                        }

                        var createdAsset = CreateAssetWithSavePrompt(propType, ref pwd);
                        if (createdAsset != null)
                        {
                            nv.editor.EditorData.Instance.SetData <string>(pwd, keyname, property.propertyPath);
                            property.objectReferenceValue = createdAsset;
                            property.isExpanded           = true;
                        }
                    }
                }
                // Has data in property, draw foldout and editor
                else
                {
                    EditorGUILayout.PropertyField(property);

                    Rect rectFoldout = GUILayoutUtility.GetLastRect();
                    rectFoldout.width = 20;
                    if (hasSpace)
                    {
                        rectFoldout.yMin += foldoutSpace;
                    }

                    property.isExpanded = EditorGUI.Foldout(rectFoldout, property.isExpanded, GUIContent.none);

                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        using (new EditorGUILayout.VerticalScope(styleEditBox))
                        {
                            var restoreIndent = EditorGUI.indentLevel;
                            EditorGUI.indentLevel = 1;
                            scriptableEditor.serializedObject.Update();
                            scriptableEditor.OnInspectorGUI();
                            scriptableEditor.serializedObject.ApplyModifiedProperties();
                            EditorGUI.indentLevel = restoreIndent;
                        }
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                SerializedProperty targetProp = serializedObject.FindProperty(property.propertyPath);

                bool isStartProp = targetProp.propertyPath.StartsWith("m_");
#if UNITY_5_5_OR_NEWER
                using (new EditorGUI.DisabledScope(isStartProp))
                {
                    EditorGUILayout.PropertyField(targetProp, targetProp.isExpanded);
                }
#else
                EditorGUI.BeginDisabledGroup(isStartProp);
                EditorGUILayout.PropertyField(targetProp, targetProp.isExpanded);
                EditorGUI.EndDisabledGroup();
#endif
            }
        }