Ejemplo n.º 1
0
        public static void Run()
        {
            Console.WriteLine(nameof(TestAttributeExpressions));

            TypeAttribute attr1 = typeof(Holder1).GetCustomAttribute <TypeAttribute>();

            if (attr1.SomeType.ToString() != "ReflectionTest+TestAttributeExpressions+FirstNeverUsedType*[,]")
            {
                throw new Exception();
            }

            TypeAttribute attr2 = typeof(Holder2).GetCustomAttribute <TypeAttribute>();

            if (attr2.SomeType.ToString() != "ReflectionTest+TestAttributeExpressions+Gen`1[ReflectionTest+TestAttributeExpressions+SecondNeverUsedType]")
            {
                throw new Exception();
            }

            EnumArrayAttribute attr3 = typeof(Holder3).GetCustomAttribute <EnumArrayAttribute>();

            if (attr3.EnumArray[0] != 0)
            {
                throw new Exception();
            }
        }
Ejemplo n.º 2
0
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            EnumArrayAttribute enumArrayAttribute = attribute as EnumArrayAttribute;

            string[] names = Enum.GetNames(enumArrayAttribute.EnumType);
            return(names.Length * (EditorGUIUtility.singleLineHeight + 8));
        }
Ejemplo n.º 3
0
        protected string GetEnumName(GUIContent label)
        {
            EnumArrayAttribute attr = (EnumArrayAttribute)attribute;

            string newLabel = string.Empty;

            if (attr.EnumType.IsEnum)
            {
                int    idx = label.text.Split(' ').Last().ParseInt();
                object e   = System.Enum.ToObject(attr.EnumType, idx);
                newLabel = System.Enum.GetName(attr.EnumType, e);
            }

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

            return(newLabel);
        }
Ejemplo n.º 4
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumArrayAttribute enumArrayAttribute = attribute as EnumArrayAttribute;

        EditorGUI.BeginProperty(position, label, property);
        int end = property.propertyPath.LastIndexOf(']');

        if (end == -1)
        {
            EditorGUI.HelpBox(position, string.Format("{0} is not an array element but has [EnumArray].", property.propertyPath), MessageType.Error);
        }
        else
        {
            int    start = property.propertyPath.LastIndexOf('[') + 1;
            int    index = int.Parse(property.propertyPath.Substring(start, end - start));
            string name  = Enum.GetName(enumArrayAttribute.Type, index);
            name = name != null? name : "<UNDEF>";
            EditorGUI.PropertyField(position, property, new GUIContent(name), true);
        }
        EditorGUI.EndProperty();
    }
Ejemplo n.º 5
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumArrayAttribute enumArrayAttribute = attribute as EnumArrayAttribute;

            string[] names = Enum.GetNames(enumArrayAttribute.EnumType);

            SerializedProperty valuesProperty = property.FindPropertyRelative("Values");

            while (valuesProperty.arraySize != names.Length)
            {
                valuesProperty.InsertArrayElementAtIndex(valuesProperty.arraySize);
                SerializedProperty prop = valuesProperty.GetArrayElementAtIndex(valuesProperty.arraySize - 1);
                prop.floatValue = enumArrayAttribute.Exception == (valuesProperty.arraySize - 1) ? enumArrayAttribute.ExceptionValue : enumArrayAttribute.DefaultValue;
                prop.serializedObject.ApplyModifiedProperties();
            }

            //position.y = 55;
            position.height = EditorGUIUtility.singleLineHeight;
            EditorGUI.LabelField(position, enumArrayAttribute.EnumLabel, EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();
            for (int i = 0; i < valuesProperty.arraySize; i++)
            {
                string name = names[i];
                position.x     = 35;
                position.y    += position.height;
                position.width = Screen.width / 2f;
                EditorGUI.PrefixLabel(position, new GUIContent(name));

                position.x += position.width;
                SerializedProperty floatAtIndex = valuesProperty.GetArrayElementAtIndex(i);
                floatAtIndex.floatValue = EditorGUI.FloatField(position, floatAtIndex.floatValue);
            }

            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }
        }