Beispiel #1
0
        public void Init(Enum defaultValue, bool includeObsoleteValues)
        {
            if (defaultValue == null)
            {
                throw new ArgumentNullException(nameof(defaultValue));
            }

            m_EnumType = defaultValue.GetType();
            m_EnumData = EnumDataUtility.GetCachedEnumData(m_EnumType, !includeObsoleteValues);

            SetValueWithoutNotify(defaultValue);
        }
Beispiel #2
0
        public MultiselectDataSource(Enum enumValue)
        {
            m_EnumFlag = enumValue;
            var enumType = enumFlags.GetType();

            var enumData = EnumDataUtility.GetCachedEnumData(enumType);

            if (!enumData.serializable)
            {
                // this is the same message used in SerializedPropertyEnumHelper.cpp
                throw new NotSupportedException(string.Format("Unsupported enum base type for {0}", enumType.Name));
            }

            m_Mask         = EnumDataUtility.EnumFlagsToInt(enumData, enumFlags);
            m_DisplayNames = enumData.displayNames;
            m_FlagValues   = enumData.flagValues;

            MaskFieldGUI.GetMenuOptions(m_Mask, m_DisplayNames, m_FlagValues, out var buttonText, out var buttonTextMixed, out m_OptionNames, out m_OptionMaskValues, out m_SelectedOptions);
        }
        public void Init(Enum defaultValue, bool includeObsoleteValues = false)
        {
            if (defaultValue == null)
            {
                throw new ArgumentNullException(nameof(defaultValue));
            }

            m_EnumType = defaultValue.GetType();
            m_EnumData = EnumDataUtility.GetCachedEnumData(m_EnumType, !includeObsoleteValues);

            if (!m_EnumData.flags)
            {
                Debug.LogWarning("EnumMaskField is not bound to enum type with the [Flags] attribute");
            }

            choices      = new List <string>(m_EnumData.displayNames);
            choicesMasks = new List <int>(m_EnumData.flagValues);

            SetValueWithoutNotify(defaultValue);
        }
        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);
            }
        }