Exemplo n.º 1
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            serializedObject.UpdateIfRequiredOrScript();

            EditorGUILayoutx.MinField(properties.Iterations, 0, Content.Iterations);
            EditorGUILayout.PropertyField(properties.DeformerElement, Content.Deformer);

            serializedObject.ApplyModifiedProperties();

            var deformerProperty = properties.DeformerElement.FindPropertyRelative("component");
            var deformer         = deformerProperty.objectReferenceValue;


            if (!properties.DeformerElement.hasMultipleDifferentValues && deformer != null)
            {
                CreateCachedEditor(deformer, null, ref deformerEditor);

                SceneView.onSceneGUIDelegate -= SceneGUI;
                SceneView.onSceneGUIDelegate += SceneGUI;

                using (new EditorGUILayout.VerticalScope(DeformEditorResources.GetStyle("Box")))
                    deformerEditor.OnInspectorGUI();
            }

            EditorApplication.QueuePlayerLoopUpdate();
        }
        /// <summary>
        /// Make sure your implementation of IComponentElement has a PropertyDrawer and
        /// serialized fields for for the component reference and active bool called "component" and "active".
        /// </summary>
        public ReorderableComponentElementList(SerializedObject serializedObject, SerializedProperty elements)
        {
            list = new ReorderableList(serializedObject, elements);
            list.elementHeight = EditorGUIUtility.singleLineHeight;

            list.onAddCallback = (list) =>
            {
                var property = list.serializedProperty;

                property.arraySize++;

                // Even though in the DeformerElement class, active defaults to true, serialized bools default to false.
                var lastElement = property.GetArrayElementAtIndex(property.arraySize - 1);
                lastElement.FindPropertyRelative("active").boolValue = true;
            };

            list.drawHeaderCallback  = (r) => GUI.Label(r, new GUIContent($"{typeof (T).Name}s"));
            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                try
                {
                    var elementProperty = list.serializedProperty.GetArrayElementAtIndex(index);

                    EditorGUI.PropertyField(rect, elementProperty);

                    // get the current element's component property
                    var componentProperty = elementProperty.FindPropertyRelative("component");

                    if (componentProperty == null)
                    {
                        elementProperty.serializedObject.SetIsDifferentCacheDirty();
                        elementProperty.serializedObject.Update();

                        componentProperty = elementProperty.FindPropertyRelative("component");
                    }

                    // and the property's object reference
                    var component = (Component)componentProperty.objectReferenceValue;

                    // if the current element is selected
                    if (!componentProperty.hasMultipleDifferentValues && index == list.index && component != null)
                    {
                        // create it's editor and draw it
                        Editor.CreateCachedEditor(component, null, ref selectedComponentInspectorEditor);
#if UNITY_2019_1_OR_NEWER
                        SceneView.duringSceneGui -= SceneGUI;
                        SceneView.duringSceneGui += SceneGUI;
#else
                        SceneView.onSceneGUIDelegate -= SceneGUI;
                        SceneView.onSceneGUIDelegate += SceneGUI;
#endif

                        var foldoutName = $"{ObjectNames.NicifyVariableName (componentProperty.objectReferenceValue.GetType ().Name)} Properties";
                        using (var foldout = new EditorGUILayoutx.FoldoutContainerScope(list.serializedProperty, foldoutName, DeformEditorResources.GetStyle("Box"), EditorStyles.foldout))
                        {
                            if (foldout.isOpen)
                            {
                                selectedComponentInspectorEditor.OnInspectorGUI();
                            }
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    list.serializedProperty.serializedObject.SetIsDifferentCacheDirty();
                    list.serializedProperty.serializedObject.Update();
                }
            };
        }