Esempio n. 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum targetEnum = (Enum)Enum.ToObject(fieldInfo.FieldType, property.intValue);

            string propName = flagSettings.enumName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = ObjectNames.NicifyVariableName(property.name);
            }

            EditorGUI.BeginChangeCheck();
            EditorGUI.BeginProperty(position, label, property);
#if UNITY_2017_3_OR_NEWER
            Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);
#else
            Enum enumNew = EditorGUI.EnumMaskField(position, propName, targetEnum);
#endif
            if (!property.hasMultipleDifferentValues || EditorGUI.EndChangeCheck())
            {
                property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
            }

            EditorGUI.EndProperty();
        }
Esempio n. 2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;

            string[] path         = property.propertyPath.Split('.');
            object   targetObject = property.serializedObject.targetObject;

            // Walk through field references to get actual targetObject
            // You must walk through them in case your Enum is a field in a class composited into a monobehavior
            for (int i = 0; i < path.Length - 1; ++i)
            {
                foreach (FieldInfo field in GetAllFields(targetObject.GetType()))
                {
                    if (field.Name == path[i])
                    {
                        targetObject = field.GetValue(targetObject);
                    }
                }
            }

            Enum targetEnum = (Enum)fieldInfo.GetValue(targetObject);

            string propName = flagSettings.EnumName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = ObjectNames.NicifyVariableName(property.name);
            }

            EditorGUI.BeginProperty(position, label, property);
            Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);

            property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
            EditorGUI.EndProperty();
        }
Esempio n. 3
0
        public void DrawShitButtons(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;

            int buttonsIntValue = 0;
            int enumLength      = property.enumNames.Length;

            bool[] buttonPressed = new bool[enumLength];
            float  buttonWidth   = (position.width - EditorGUIUtility.labelWidth) / enumLength;

            int propertyValue = property.intValue;

            label.text += " " + propertyValue;
            EditorGUI.LabelField(new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height), label);

            EditorGUI.BeginChangeCheck();

            if (flagSettings.isReadonly)
            {
                GUI.color = new Color(0.65f, 0.65f, 0.65f);
            }

            for (int i = 0; i < enumLength; i++)
            {
                // Check if the button is/was pressed
                if ((property.intValue & (1 << i)) == 1 << i)
                {
                    buttonPressed[i] = true;
                }


                Rect buttonPos = new Rect(position.x + EditorGUIUtility.labelWidth + buttonWidth * i, position.y, buttonWidth, position.height);

                buttonPressed[i] = GUI.Toggle(buttonPos, buttonPressed[i], property.enumNames[i], "Button");

                if (buttonPressed[i])
                {
                    buttonsIntValue += 1 << i;
                }
            }

            if (EditorGUI.EndChangeCheck() && !flagSettings.isReadonly)
            {
                property.intValue = buttonsIntValue;
            }

            GUI.color = Color.white;
        }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
        Enum targetEnum = GetBaseProperty <Enum>(property);

        string propName = flagSettings.enumName;

        if (string.IsNullOrEmpty(propName))
        {
            propName = property.name;
        }
        EditorGUI.BeginProperty(position, label, property);
        Enum enumNew = EditorGUI.EnumMaskField(position, label.text, targetEnum);

        property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
        EditorGUI.EndProperty();
    }
Esempio n. 5
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
        Enum targetEnum = (Enum)Enum.ToObject(fieldInfo.FieldType, property.intValue);

        GUIContent propName = new GUIContent(flagSettings.enumName);

        if (string.IsNullOrEmpty(flagSettings.enumName))
        {
            propName = label;
        }

        EditorGUI.BeginProperty(position, label, property);
        Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);

        property.intValue = (int)Convert.ChangeType(enumNew, fieldInfo.FieldType);
        EditorGUI.EndProperty();
    }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum targetEnum = (Enum)Enum.Parse(fieldInfo.FieldType, property.intValue.ToString());

            string propName = flagSettings.name;

            if (string.IsNullOrEmpty(propName))
            {
                propName = ObjectNames.NicifyVariableName(property.name);
            }

            EditorGUI.BeginProperty(position, label, property);
            Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);

            property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
            EditorGUI.EndProperty();
        }
Esempio n. 7
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
        Enum targetEnum = GetBaseProperty <Enum>(property);

        string propName = flagSettings.enumName;

        if (string.IsNullOrEmpty(propName))
        {
            propName = property.displayName;
        }

        // Generate the label for this
        GUIContent nameWithTooltip = new GUIContent(propName, property.tooltip);

        EditorGUI.BeginProperty(position, label, property);
        Enum enumNew = EditorGUI.EnumMaskField(position, nameWithTooltip, targetEnum);

        property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
        EditorGUI.EndProperty();
    }
Esempio n. 8
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;

            //label.text += " " + property.intValue;

            if (flagSettings.isReadonly)
            {
                GUI.color = new Color(0.65f, 0.65f, 0.65f);
            }

            if (flagSettings.buttonMode)
            {
                DrawButtons(position, property, label);
            }
            else
            {
                DrawMask(position, property, label);
            }


            GUI.color = Color.white;
        }
Esempio n. 9
0
    /// <summary>
    /// Overrides Unity display of Enums tagged with [EnumFlag].
    /// </summary>
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Grab the enum from the properties target.
        Enum targetEnum = (Enum)fieldInfo.GetValue(property.serializedObject.targetObject);
        EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;

        string propName = flagSettings.enumName;

        if (string.IsNullOrEmpty(propName))
        {
            propName = label.text;
        }

        // Start property rendering
        EditorGUI.BeginProperty(position, label, property);
        // Use Unity's Flags field to render the enum instead of the default mask.
        // This allows you to select multiple options.
        Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);

        // Convert back to an int value to represent the mask fields, as that is how its stored on the serialized property.
        property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
        // Finish property rendering
        EditorGUI.EndProperty();
    }
Esempio n. 10
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum   targetEnum = (System.Enum)System.Enum.ToObject(fieldInfo.FieldType, property.intValue);
            string propName   = flagSettings.enumName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = property.name;
            }

            EditorGUI.BeginProperty(position, label, property);
            Enum newEnum = EditorGUI.EnumMaskField(position, propName, targetEnum);

            int old  = (int)Convert.ChangeType(targetEnum, targetEnum.GetType());
            int newE = (int)Convert.ChangeType(newEnum, targetEnum.GetType());

            if (old != newE)
            {
                property.intValue = newE;
            }

            EditorGUI.EndProperty();
        }
Esempio n. 11
0
        public void DrawMask(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum targetEnum = GetBaseProperty <Enum>(property);

            string propName = flagSettings.displayName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = property.name;
            }

            EditorGUI.BeginProperty(position, label, property);

            Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);

            if (!flagSettings.isReadonly)
            {
                property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
            }


            EditorGUI.EndProperty();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum targetEnum = GetBaseProperty <Enum>(property);

            string propName = flagSettings.enumName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = Regex.Replace(property.name, "([^^])([A-Z])", "$1 $2");
            }
            EditorGUI.BeginChangeCheck();
            EditorGUI.BeginProperty(position, label, property);

            Enum enumNew = EditorGUI.EnumFlagsField(position, ObjectNames.NicifyVariableName(propName), targetEnum);

            EditorGUI.EndProperty();
            if (EditorGUI.EndChangeCheck())
            {
                property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
                property.serializedObject.ApplyModifiedProperties();
                property.serializedObject.UpdateIfRequiredOrScript();
            }
        }
Esempio n. 13
0
        public void DrawButtons(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;
            Enum targetEnum = GetBaseProperty <Enum>(property);

            string propName = flagSettings.displayName;

            if (string.IsNullOrEmpty(propName))
            {
                propName = label.text;
            }

            EditorGUI.LabelField(new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height), label);
            EditorGUI.BeginProperty(position, label, property);

            var values = Enum.GetValues(targetEnum.GetType());

            string[] names = Enum.GetNames(targetEnum.GetType());

            float buttonWidth = (position.width - EditorGUIUtility.labelWidth) / names.Length;

            int propValue   = property.intValue;
            int buttonValue = 0;

            int index = 0;

            foreach (int value in values)
            {
                //Calculate the position of the button
                Rect buttonPos = new Rect(position.x + EditorGUIUtility.labelWidth + buttonWidth * index, position.y, buttonWidth, position.height);

                //Calculate the correct styling
                GUIStyle style = EditorStyles.miniButtonMid;
                if (index == 0)
                {
                    style = EditorStyles.miniButtonLeft;
                }
                if (index == values.Length - 1)
                {
                    style = EditorStyles.miniButtonRight;
                }
                if (values.Length == 1)
                {
                    style = EditorStyles.miniButton;
                }

                //The value isn't 0, so we are not the first element
                if (value != 0)
                {
                    if (GUI.Toggle(buttonPos, ((propValue & value) != 0), property.enumNames[index], style))
                    {
                        buttonValue |= value;
                    }
                }
                else
                {
                    //The value is 0, so this is a nothing element.
                    if (GUI.Toggle(buttonPos, propValue == value, property.enumNames[index], style))
                    {
                        buttonValue = 0;
                        propValue   = 0;
                    }
                }

                index++;
            }

            if (!flagSettings.isReadonly)
            {
                property.intValue = buttonValue;
            }

            EditorGUI.EndProperty();
        }