Exemplo n.º 1
0
            public ObjectFieldDisplay(ObjectField objectField)
            {
                AddToClassList(ussClassName);
                m_ObjectIcon = new Image {
                    scaleMode = ScaleMode.ScaleAndCrop, pickingMode = PickingMode.Ignore
                };
                m_ObjectIcon.AddToClassList(iconUssClassName);
                m_ObjectLabel = new Label {
                    pickingMode = PickingMode.Ignore
                };
                m_ObjectLabel.AddToClassList(labelUssClassName);
                m_ObjectField = objectField;

                Update();

                Add(m_ObjectIcon);
                Add(m_ObjectLabel);
            }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private VisualElement CreateFieldFromProperty(SerializedProperty property)
        {
            var propertyType = property.propertyType;

            if (EditorGUI.HasVisibleChildFields(property))
            {
                return(CreateFoldout(property));
            }

            switch (propertyType)
            {
            case SerializedPropertyType.Integer:
                return(ConfigureField <IntegerField, int>(new IntegerField(), property));

            case SerializedPropertyType.Boolean:
                return(ConfigureField <Toggle, bool>(new Toggle(), property));

            case SerializedPropertyType.Float:
                return(ConfigureField <FloatField, float>(new FloatField(), property));

            case SerializedPropertyType.String:
                return(ConfigureField <TextField, string>(new TextField(), property));

            case SerializedPropertyType.Color:
                return(ConfigureField <ColorField, Color>(new ColorField(), property));

            case SerializedPropertyType.ObjectReference:
            {
                var  field        = new ObjectField();
                Type requiredType = null;

                // Checking if the target ExtendsANativeType() avoids a native error when
                // getting the type about: "type is not a supported pptr value"
                var target = property.serializedObject.targetObject;
                if (NativeClassExtensionUtilities.ExtendsANativeType(target))
                {
                    ScriptAttributeUtility.GetFieldInfoFromProperty(property, out requiredType);
                }

                if (requiredType == null)
                {
                    requiredType = typeof(UnityEngine.Object);
                }

                field.objectType = requiredType;
                return(ConfigureField <ObjectField, UnityEngine.Object>(field, property));
            }

            case SerializedPropertyType.LayerMask:
                return(ConfigureField <LayerMaskField, int>(new LayerMaskField(), property));

            case SerializedPropertyType.Enum:
            {
                var field = new PopupField <string>(property.enumDisplayNames.ToList(), property.enumValueIndex);
                field.index = property.enumValueIndex;
                return(ConfigureField <PopupField <string>, string>(field, property));
            }

            case SerializedPropertyType.Vector2:
                return(ConfigureField <Vector2Field, Vector2>(new Vector2Field(), property));

            case SerializedPropertyType.Vector3:
                return(ConfigureField <Vector3Field, Vector3>(new Vector3Field(), property));

            case SerializedPropertyType.Vector4:
                return(ConfigureField <Vector4Field, Vector4>(new Vector4Field(), property));

            case SerializedPropertyType.Rect:
                return(ConfigureField <RectField, Rect>(new RectField(), property));

            case SerializedPropertyType.ArraySize:
            {
                var field = new IntegerField();
                field.SetValueWithoutNotify(property.intValue); // This avoids the OnValueChanged/Rebind feedback loop.
                field.isDelayed = true;                         // To match IMGUI. Also, focus is lost anyway due to the rebind.
                field.RegisterValueChangedCallback((e) => { UpdateArrayFoldout(e, this, m_ParentPropertyField); });
                return(ConfigureField <IntegerField, int>(field, property));
            }

            case SerializedPropertyType.Character:
            {
                var field = new TextField();
                field.maxLength = 1;
                return(ConfigureField <TextField, string>(field, property));
            }

            case SerializedPropertyType.AnimationCurve:
                return(ConfigureField <CurveField, AnimationCurve>(new CurveField(), property));

            case SerializedPropertyType.Bounds:
                return(ConfigureField <BoundsField, Bounds>(new BoundsField(), property));

            case SerializedPropertyType.Gradient:
                return(ConfigureField <GradientField, Gradient>(new GradientField(), property));

            case SerializedPropertyType.Quaternion:
                return(null);

            case SerializedPropertyType.ExposedReference:
                return(null);

            case SerializedPropertyType.FixedBufferSize:
                return(null);

            case SerializedPropertyType.Vector2Int:
                return(ConfigureField <Vector2IntField, Vector2Int>(new Vector2IntField(), property));

            case SerializedPropertyType.Vector3Int:
                return(ConfigureField <Vector3IntField, Vector3Int>(new Vector3IntField(), property));

            case SerializedPropertyType.RectInt:
                return(ConfigureField <RectIntField, RectInt>(new RectIntField(), property));

            case SerializedPropertyType.BoundsInt:
                return(ConfigureField <BoundsIntField, BoundsInt>(new BoundsIntField(), property));

            case SerializedPropertyType.Generic:
            default:
                return(null);
            }
        }
Exemplo n.º 4
0
 public ObjectFieldSelector(ObjectField objectField)
 {
     m_ObjectField = objectField;
 }
Exemplo n.º 5
0
        private VisualElement CreateOrUpdateFieldFromProperty(SerializedProperty property, object originalField = null)
        {
            var propertyType = property.propertyType;

            if (EditorGUI.HasVisibleChildFields(property, true))
            {
                return(CreateFoldout(property, originalField));
            }

            TrimChildrenContainerSize(0);
            m_ChildrenContainer = null;

            switch (propertyType)
            {
            case SerializedPropertyType.Integer:
                if (property.type == "long")
                {
                    return(ConfigureField <LongField, long>(originalField as LongField, property, () => new LongField()));
                }
                return(ConfigureField <IntegerField, int>(originalField as IntegerField, property, () => new IntegerField()));

            case SerializedPropertyType.Boolean:
                return(ConfigureField <Toggle, bool>(originalField as Toggle, property, () => new Toggle()));

            case SerializedPropertyType.Float:
                if (property.type == "double")
                {
                    return(ConfigureField <DoubleField, double>(originalField as DoubleField, property, () => new DoubleField()));
                }
                return(ConfigureField <FloatField, float>(originalField as FloatField, property, () => new FloatField()));

            case SerializedPropertyType.String:
                return(ConfigureField <TextField, string>(originalField as TextField, property, () => new TextField()));

            case SerializedPropertyType.Color:
                return(ConfigureField <ColorField, Color>(originalField as ColorField, property, () => new ColorField()));

            case SerializedPropertyType.ObjectReference:
            {
                ObjectField field = originalField as ObjectField;
                if (field == null)
                {
                    field = new ObjectField();
                }

                Type requiredType = null;

                // Checking if the target ExtendsANativeType() avoids a native error when
                // getting the type about: "type is not a supported pptr value"
                var target = property.serializedObject.targetObject;
                if (NativeClassExtensionUtilities.ExtendsANativeType(target))
                {
                    ScriptAttributeUtility.GetFieldInfoFromProperty(property, out requiredType);
                }

                if (requiredType == null)
                {
                    requiredType = typeof(UnityEngine.Object);
                }

                field.objectType = requiredType;
                return(ConfigureField <ObjectField, UnityEngine.Object>(field, property, () => new ObjectField()));
            }

            case SerializedPropertyType.LayerMask:
                return(ConfigureField <LayerMaskField, int>(originalField as LayerMaskField, property, () => new LayerMaskField()));

            case SerializedPropertyType.Enum:
            {
                ScriptAttributeUtility.GetFieldInfoFromProperty(property, out var enumType);

                if (enumType != null && enumType.IsDefined(typeof(FlagsAttribute), false))
                {
                    // We should use property.longValue instead of intValue once long enum types are supported.
                    var enumData = EnumDataUtility.GetCachedEnumData(enumType);
                    if (originalField != null && originalField is EnumFlagsField enumFlagsField)
                    {
                        enumFlagsField.choices = enumData.displayNames.ToList();
                        enumFlagsField.value   = (Enum)Enum.ToObject(enumType, property.intValue);
                    }
                    return(ConfigureField <EnumFlagsField, Enum>(originalField as EnumFlagsField, property,
                                                                 () => new EnumFlagsField
                        {
                            choices = enumData.displayNames.ToList(),
                            value = (Enum)Enum.ToObject(enumType, property.intValue)
                        }));
                }
                else
                {
                    // We need to use property.enumDisplayNames[property.enumValueIndex] as the source of truth for
                    // the popup index, because enumData.displayNames and property.enumDisplayNames might not be
                    // in the same order.
                    var enumData             = enumType != null ? (EnumData?)EnumDataUtility.GetCachedEnumData(enumType) : null;
                    var propertyDisplayNames = EditorGUI.EnumNamesCache.GetEnumDisplayNames(property);
                    var popupEntries         = (enumData?.displayNames ?? propertyDisplayNames).ToList();
                    int propertyFieldIndex   = (property.enumValueIndex < 0 || property.enumValueIndex >= propertyDisplayNames.Length
                            ? PopupField <string> .kPopupFieldDefaultIndex : (enumData != null
                                ? Array.IndexOf(enumData.Value.displayNames, propertyDisplayNames[property.enumValueIndex])
                                : property.enumValueIndex));
                    if (originalField != null && originalField is PopupField <string> popupField)
                    {
                        popupField.choices = popupEntries;
                        popupField.index   = propertyFieldIndex;
                    }
                    return(ConfigureField <PopupField <string>, string>(originalField as PopupField <string>, property,
                                                                        () => new PopupField <string>(popupEntries, property.enumValueIndex)
                        {
                            index = propertyFieldIndex
                        }));
                }
            }

            case SerializedPropertyType.Vector2:
                return(ConfigureField <Vector2Field, Vector2>(originalField as Vector2Field, property, () => new Vector2Field()));

            case SerializedPropertyType.Vector3:
                return(ConfigureField <Vector3Field, Vector3>(originalField as Vector3Field, property, () => new Vector3Field()));

            case SerializedPropertyType.Vector4:
                return(ConfigureField <Vector4Field, Vector4>(originalField as Vector4Field, property, () => new Vector4Field()));

            case SerializedPropertyType.Rect:
                return(ConfigureField <RectField, Rect>(originalField as RectField, property, () => new RectField()));

            case SerializedPropertyType.ArraySize:
            {
                IntegerField field = originalField as IntegerField;
                if (field == null)
                {
                    field = new IntegerField();
                }
                field.SetValueWithoutNotify(property.intValue); // This avoids the OnValueChanged/Rebind feedback loop.
                field.isDelayed = true;                         // To match IMGUI. Also, focus is lost anyway due to the rebind.
                field.RegisterValueChangedCallback((e) => { UpdateArrayFoldout(e, this, m_ParentPropertyField); });
                return(ConfigureField <IntegerField, int>(field, property, () => new IntegerField()));
            }

            case SerializedPropertyType.Character:
            {
                TextField field = originalField as TextField;
                if (field != null)
                {
                    field.maxLength = 1;
                }
                return(ConfigureField <TextField, string>(field, property, () => new TextField {
                        maxLength = 1
                    }));
            }

            case SerializedPropertyType.AnimationCurve:
                return(ConfigureField <CurveField, AnimationCurve>(originalField as CurveField, property, () => new CurveField()));

            case SerializedPropertyType.Bounds:
                return(ConfigureField <BoundsField, Bounds>(originalField as BoundsField, property, () => new BoundsField()));

            case SerializedPropertyType.Gradient:
                return(ConfigureField <GradientField, Gradient>(originalField as GradientField, property, () => new GradientField()));

            case SerializedPropertyType.Quaternion:
                return(null);

            case SerializedPropertyType.ExposedReference:
                return(null);

            case SerializedPropertyType.FixedBufferSize:
                return(null);

            case SerializedPropertyType.Vector2Int:
                return(ConfigureField <Vector2IntField, Vector2Int>(originalField as Vector2IntField, property, () => new Vector2IntField()));

            case SerializedPropertyType.Vector3Int:
                return(ConfigureField <Vector3IntField, Vector3Int>(originalField as Vector3IntField, property, () => new Vector3IntField()));

            case SerializedPropertyType.RectInt:
                return(ConfigureField <RectIntField, RectInt>(originalField as RectIntField, property, () => new RectIntField()));

            case SerializedPropertyType.BoundsInt:
                return(ConfigureField <BoundsIntField, BoundsInt>(originalField as BoundsIntField, property, () => new BoundsIntField()));


            case SerializedPropertyType.Generic:
                return(property.isArray
                        ? ConfigureListView(new ListView(), property)
                        : null);

            default:
                return(null);
            }
        }