private VisualElement CreateDefaultInspector(SerializedObject serializedObject)
        {
            if (serializedObject == null)
            {
                return(null);
            }

            SerializedProperty property = serializedObject.GetIterator();

            if (property.NextVisible(true)) // Expand first child.
            {
                do
                {
                    var field = new PropertyField(property);
                    field.name = "PropertyField:" + property.propertyPath;

                    if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
                    {
                        field.SetEnabled(false);
                    }

                    hierarchy.Add(field);
                }while (property.NextVisible(false));
            }

            if (serializedObject.targetObject == null)
            {
                AddMissingScriptLabel(serializedObject);
            }

            AddToClassList(uIEDefaultVariantUssClassName);
            AddToClassList(uIEInspectorVariantUssClassName);

            return(this);
        }
        private void UpdateArrayFoldout(
            ChangeEvent <int> changeEvent,
            PropertyField targetPropertyField,
            PropertyField parentPropertyField)
        {
            var propertyIntValue = targetPropertyField.m_SerializedProperty.intValue;

            if (targetPropertyField == null || targetPropertyField.m_SerializedProperty == null || parentPropertyField == null && targetPropertyField.m_SerializedProperty.intValue == changeEvent.newValue)
            {
                return;
            }

            var parentSerializedObject = parentPropertyField?.m_SerializedProperty?.serializedObject;

            if (propertyIntValue != changeEvent.newValue)
            {
                var serialiedObject = targetPropertyField.m_SerializedProperty.serializedObject;
                serialiedObject.UpdateIfRequiredOrScript();
                targetPropertyField.m_SerializedProperty.intValue = changeEvent.newValue;
                serialiedObject.ApplyModifiedProperties();
            }

            if (parentPropertyField != null)
            {
                parentPropertyField.RefreshChildrenProperties(parentPropertyField.m_SerializedProperty.Copy(), true);
                return;
            }
        }
Example #3
0
        /// <summary>
        /// Adds default inspector property fields under a container VisualElement
        /// </summary>
        /// <param name="container">The parent VisualElement</param>
        /// <param name="serializedObject">The SerializedObject to inspect</param>
        /// <param name="editor">The editor currently used</param>
        public static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, Editor editor)
        {
            if (serializedObject == null)
            {
                return;
            }

            bool isPartOfPrefabInstance = editor != null && GenericInspector.IsAnyMonoBehaviourTargetPartOfPrefabInstance(editor);

            SerializedProperty property = serializedObject.GetIterator();

            if (property.NextVisible(true)) // Expand first child.
            {
                do
                {
                    var field = new PropertyField(property);
                    field.name = "PropertyField:" + property.propertyPath;

                    if (property.propertyPath == "m_Script")
                    {
                        if ((serializedObject.targetObject != null) || isPartOfPrefabInstance)
                        {
                            field.SetEnabled(false);
                        }
                    }

                    container.Add(field);
                }while (property.NextVisible(false));
            }

            if (serializedObject.targetObject == null)
            {
                AddMissingScriptLabel(container, serializedObject, isPartOfPrefabInstance);
            }
        }
Example #4
0
        private VisualElement CreateFoldout(SerializedProperty property)
        {
            property = property.Copy();
            var foldout = new Foldout()
            {
                text = property.localizedDisplayName
            };

            foldout.value       = property.isExpanded;
            foldout.bindingPath = property.propertyPath;
            foldout.name        = "unity-foldout-" + property.propertyPath;

            var endProperty = property.GetEndProperty();

            property.NextVisible(true); // Expand the first child.
            do
            {
                if (SerializedProperty.EqualContents(property, endProperty))
                {
                    break;
                }

                var field = new PropertyField(property);
                field.m_ParentPropertyField = this;
                field.name = "unity-property-field-" + property.propertyPath;
                if (field == null)
                {
                    continue;
                }

                foldout.Add(field);
            }while (property.NextVisible(false)); // Never expand children.

            return(foldout);
        }
Example #5
0
        void RefreshChildrenProperties(SerializedProperty property, bool bindNewFields)
        {
            if (m_ChildrenContainer == null)
            {
                return;
            }

            var endProperty = property.GetEndProperty();
            int propCount   = 0;

            if (m_ChildrenProperties == null)
            {
                m_ChildrenProperties = new List <PropertyField>();
            }

            property.NextVisible(true); // Expand the first child.
            do
            {
                if (SerializedProperty.EqualContents(property, endProperty))
                {
                    break;
                }

                PropertyField field    = null;
                var           propPath = property.propertyPath;
                if (propCount < m_ChildrenProperties.Count)
                {
                    field = m_ChildrenProperties[propCount];
                    if (field.bindingPath != propPath)
                    {
                        field.bindingPath = property.propertyPath;
                        field.Bind(property.serializedObject);
                    }
                }
                else
                {
                    field = new PropertyField(property);
                    field.m_ParentPropertyField = this;
                    m_ChildrenProperties.Add(field);
                    field.bindingPath = property.propertyPath;

                    if (bindNewFields)
                    {
                        field.Bind(property.serializedObject);
                    }
                }
                field.name = "unity-property-field-" + property.propertyPath;

                // Not yet knowing what type of field we are dealing with, we defer the showMixedValue value setting
                // to be automatically done via the next Reset call
                m_ChildrenContainer.Add(field);
                propCount++;
            }while (property.NextVisible(false)); // Never expand children.

            TrimChildrenContainerSize(propCount);
        }
        private void UpdateArrayFoldout(
            ChangeEvent <int> changeEvent,
            PropertyField targetPropertyField,
            PropertyField parentPropertyField)
        {
            if (targetPropertyField == null || targetPropertyField.m_SerializedProperty == null)
            {
                return;
            }

            // We need to unbind *first* before we change the array size property value.
            // If we don't, the binding system could try to sync properties that no longer
            // exist - if the array shrunk.
            var parentSerializedObject = parentPropertyField?.m_SerializedProperty?.serializedObject;

            if (parentSerializedObject != null)
            {
                parentPropertyField.Unbind();
            }

            // We're forcefully updating the SerializedProperty value here, even
            // though we have a binding on it, because the very next step is to
            // Rebind() it. The Rebind() will regenerate the field (this field) and
            // bind it to another copy of this property. We need the value to be correct
            // on that copy of this property.
            var serialiedObject = targetPropertyField.m_SerializedProperty.serializedObject;

            serialiedObject.UpdateIfRequiredOrScript();
            targetPropertyField.m_SerializedProperty.intValue = changeEvent.newValue;
            serialiedObject.ApplyModifiedProperties();

            // We rebind the parent property field (which should be the foldout expanded field)
            // so that it regenerates (and rebinds) all array property fields (the new
            // number of them).
            if (parentSerializedObject != null)
            {
                parentPropertyField.Bind(parentSerializedObject);
            }

            // Very important that we stop immediate propagation here. If we don't,
            // the next handler will be FieldValueChanged() in the IBinding which
            // will be operating on a stale [this target] field
            // (we just killed it in our Unbind()/Bind() above).
            // In turn, because we share the IBinding, this event handling will
            // essentially call Unbind() one more time on the array size field
            // [this.target] and the array size field will no longer work.
            // See: case 1141787
            changeEvent.StopImmediatePropagation();
        }
        private VisualElement CreateFoldout(SerializedProperty property)
        {
            property = property.Copy();
            var foldout = new Foldout();

            foldout.text        = property.localizedDisplayName;
            foldout.value       = property.isExpanded;
            foldout.bindingPath = property.propertyPath;
            foldout.name        = "unity-foldout-" + property.propertyPath;

            // Get Foldout label.
            var foldoutToggle = foldout.Q <Toggle>(className: Foldout.toggleUssClassName);
            var foldoutLabel  = foldoutToggle.Q <Label>(className: Toggle.textUssClassName);

            foldoutLabel.bindingPath = property.propertyPath;
            foldoutLabel.SetProperty(foldoutTitleBoundLabelProperty, true);

            var endProperty = property.GetEndProperty();

            property.NextVisible(true); // Expand the first child.
            do
            {
                if (SerializedProperty.EqualContents(property, endProperty))
                {
                    break;
                }

                var field = new PropertyField(property);
                field.m_ParentPropertyField = this;
                field.name = "unity-property-field-" + property.propertyPath;
                if (field == null)
                {
                    continue;
                }

                foldout.Add(field);
            }while (property.NextVisible(false)); // Never expand children.

            return(foldout);
        }
Example #8
0
        private void UpdateArrayFoldout(
            ChangeEvent <int> changeEvent,
            PropertyField targetPropertyField,
            PropertyField parentPropertyField)
        {
            if (targetPropertyField == null || targetPropertyField.m_SerializedProperty == null)
            {
                return;
            }

            // We need to unbind *first* before we change the array size property value.
            // If we don't, the binding system could try to sync properties that no longer
            // exist - if the array shrunk.
            var parentSerializedObject = parentPropertyField?.m_SerializedProperty?.serializedObject;

            if (parentSerializedObject != null)
            {
                parentPropertyField.Unbind();
            }

            // We're forcefully updating the SerializedProperty value here, even
            // though we have a binding on it, because the very next step is to
            // Rebind() it. The Rebind() will regenerate the field (this field) and
            // bind it to another copy of this property. We need the value to be correct
            // on that copy of this property.
            var serialiedObject = targetPropertyField.m_SerializedProperty.serializedObject;

            serialiedObject.UpdateIfRequiredOrScript();
            targetPropertyField.m_SerializedProperty.intValue = changeEvent.newValue;
            serialiedObject.ApplyModifiedProperties();

            // We rebind the parent property field (which should be the foldout expanded field)
            // so that it regenerates (and rebinds) all array property fields (the new
            // number of them).
            if (parentSerializedObject != null)
            {
                parentPropertyField.Bind(parentSerializedObject);
            }
        }
Example #9
0
        public UnityEventItem()
        {
            AddToClassList(kListViewItemClassName);

            var leftColumn = new VisualElement();

            leftColumn.AddToClassList(kLeftColumnClassName);
            Add(leftColumn);

            var rightColumn = new VisualElement();

            rightColumn.AddToClassList(kRightColumnClassName);
            Add(rightColumn);

            callStateDropdown       = new PropertyField();
            callStateDropdown.label = "";
            callStateDropdown.name  = kCallStateDropdownName;
            leftColumn.Add(callStateDropdown);

            listenerTarget       = new PropertyField();
            listenerTarget.label = "";
            listenerTarget.name  = kListenerTargetName;
            leftColumn.Add(listenerTarget);

            functionDropdown      = new DropdownField();
            functionDropdown.name = kFunctionDropdownName;
            rightColumn.Add(functionDropdown);

            parameterProperty       = new PropertyField();
            parameterProperty.label = "";
            parameterProperty.name  = kParameterPropertyName;
            rightColumn.Add(parameterProperty);

            objectParameter      = new ObjectField();
            objectParameter.name = kObjectParameterName;
            objectParameter.allowSceneObjects = true;
            rightColumn.Add(objectParameter);
        }
Example #10
0
        public MinMaxGradientField(MinMaxGradientPropertyDrawer.PropertyData propertyData, string label) : base(label, null)
        {
            if (propertyData != null)
            {
                m_ColorMin = new PropertyField(propertyData.colorMin, "");
                m_ColorMin.AddToClassList(colorFieldUssClass);
                m_ColorMax = new PropertyField(propertyData.colorMax, "");
                m_ColorMax.AddToClassList(colorFieldUssClass);
                m_GradientMin  = new PropertyField(propertyData.gradientMin, "");
                m_GradientMax  = new PropertyField(propertyData.gradientMax, "");
                m_ModeDropdown = new DropdownField
                {
                    choices = stringModes.ToList()
                };
                m_ModeDropdown.createMenuCallback = () =>
                {
                    var osMenu = new GenericOSMenu();

                    for (int i = 0; i < stringModes.Length; i++)
                    {
                        var option          = stringModes[i];
                        var isValueSelected = propertyData.mode.intValue == i;

                        osMenu.AddItem(option, isValueSelected, () =>
                        {
                            m_ModeDropdown.value = option;
                        });
                    }

                    return(osMenu);
                };
                m_ModeDropdown.formatSelectedValueCallback = (value) =>
                {
                    // Don't show label for this dropdown
                    return("");
                };

                m_ModeDropdown.index = propertyData.mode.intValue;
                m_ModeDropdown.AddToClassList(dropdownFieldUssClass);
                m_MixedValueTypeLabel = new Label("\u2014");
                m_MixedValueTypeLabel.AddToClassList(multipleValuesLabelUssClass);

                var dropdownInput = m_ModeDropdown.Q <VisualElement>(null, "unity-popup-field__input");
                dropdownInput.AddToClassList(dropdownInputUssClass);

                m_GradientsContainer = new VisualElement();
                m_GradientsContainer.AddToClassList(gradientContainerUssClass);
                m_GradientsContainer.Add(m_GradientMin);
                m_GradientsContainer.Add(m_GradientMax);

                visualInput.AddToClassList(visualInputUssClass);
                visualInput.Add(m_ColorMin);
                visualInput.Add(m_ColorMax);
                visualInput.Add(m_GradientsContainer);
                visualInput.Add(m_MixedValueTypeLabel);
                visualInput.Add(m_ModeDropdown);

                m_ModeDropdown.RegisterCallback <ChangeEvent <string> >(e =>
                {
                    var index = Array.IndexOf(stringModes, e.newValue);
                    var mode  = (MinMaxGradientState)index;

                    propertyData.mode.intValue = index;
                    propertyData.mode.serializedObject.ApplyModifiedProperties();
                    UpdateFieldsDisplay(propertyData.mode);
                });

                UpdateFieldsDisplay(propertyData.mode);
            }
        }