public void FindTheSamePropertyDrawerOnSecondCall()
        {
            SerializedPropertyX property = new SerializedPropertyX("name", typeof(SomeType), new SomeType());
            PropertyDrawerX     drawer1  = Reflector.GetCustomPropertyDrawerFor(property);
            PropertyDrawerX     drawer2  = Reflector.GetCustomPropertyDrawerFor(property);

            Assert.AreEqual(drawer1, drawer2);
        }
        public void FindCustomPropertyDrawerForSubclass()
        {
            SerializedPropertyX property = new SerializedPropertyX("name", typeof(SomeType), new SomeType());
            PropertyDrawerX     drawerX  = Reflector.GetCustomPropertyDrawerFor(property);

            Assert.IsNotNull(drawerX);
            Assert.IsInstanceOf <SomeDrawerX>(drawerX);
        }
    public override float GetPropertyHeight(SerializedPropertyX property, GUIContent label)
    {
        float slh = EditorGUIUtility.singleLineHeight;

        if (property.isExpanded)
        {
            PropertyDrawerX     minDrawerX = Reflector.GetCustomPropertyDrawerFor(property["min"]);
            PropertyDrawerX     maxDrawerX = Reflector.GetCustomPropertyDrawerFor(property["max"]);
            SerializedPropertyX min        = property["min"];
            SerializedPropertyX max        = property["max"];
            return(2f * slh + (minDrawerX.GetPropertyHeight(min, min.label) + maxDrawerX.GetPropertyHeight(max, max.label)));
        }
        else
        {
            return(slh);
        }
    }
Exemple #4
0
    public static PropertyDrawerX GetCustomPropertyDrawerFor(SerializedPropertyX property)
    {
        if (drawerCache == null)
        {
            drawerCache = new Dictionary <Type, UnityEditor.PropertyDrawer>();
        }

        PropertyDrawerX drawerX = drawerInstanceCache.Get(property);

        if (drawerX == null)
        {
            var drawerType = GetExtendedDrawerTypeFor(property.Type);
            if (drawerType == null)
            {
                return(null);
            }
            drawerX = Activator.CreateInstance(drawerType) as PropertyDrawerX;
            drawerInstanceCache[property] = drawerX;
        }
        return(drawerX);
    }
    //todo account for decorators eventually
    public static float GetHeight(SerializedPropertyX property, GUIContent label, bool includeChildren)
    {
        PropertyDrawerX drawerX = Reflector.GetCustomPropertyDrawerFor(property);

        if (drawerX != null)
        {
            return(drawerX.GetPropertyHeight(property, label));
        }
        else if (!includeChildren || property.type.IsPrimitive || property.type.IsEnum || property.type == typeof(string) || Array.IndexOf(BuildInTypes, property.type) != -1)
        {
            return(GetSinglePropertyHeight(property.type, label));
        }
        else if (property.type.IsArray)
        {
            if (property.isExpanded)
            {
                float height = 32f;
                for (int i = 0; i < property.ChildCount; i++)
                {
                    SerializedPropertyX child = property.GetChildAt(i);
                    height += GetHeight(child, child.label, child.isExpanded);
                }
                return(height);
            }
            return(16f);
        }
        else
        {
            float height = 16f;
            for (int i = 0; i < property.ChildCount; i++)
            {
                SerializedPropertyX child = property.GetChildAt(i);
                height += GetHeight(child, child.label, child.isExpanded);
            }
            return(height);
        }
    }
    public static void PropertyField(SerializedPropertyX property, GUIContent label, bool includeChildren, params GUILayoutOption[] options)
    {
        Type type = property.type;

        if (!property.IsDrawable)
        {
            return;
        }
        PropertyDrawerX drawerX = Reflector.GetCustomPropertyDrawerFor(property);

        if (drawerX != null)
        {
            drawerX.OnGUI(property, label);
            return;
        }
        if (type.IsSubclassOf(typeof(UnityEngine.Object)))
        {
            property.Value = EditorGUILayout.ObjectField(label, (UnityEngine.Object)property.Value, type, true, options);
        }
        else if (type.IsArray)
        {
            if (property.Value == null)
            {
                property.Value = Array.CreateInstance(type.GetElementType(), 1);
            }
            property.isExpanded = EditorGUILayout.Foldout(property.isExpanded, label.text);
            if (property.isExpanded)
            {
                EditorGUI.indentLevel++;
                property.ArraySize = EditorGUILayout.IntField(new GUIContent("Size"), property.ArraySize);
                for (int i = 0; i < property.ArraySize; i++)
                {
                    SerializedPropertyX child = property.GetChildAt(i);
                    PropertyField(child, child.label, child.isExpanded, options);
                }
                EditorGUI.indentLevel--;
            }
        }
        else if (type.IsEnum)
        {
            property.Value = EditorGUILayout.EnumPopup(label, (Enum)property.Value, options);
        }
        else if (type == typeof(Color))
        {
            property.Value = EditorGUILayout.ColorField(label, (Color)property.Value);
        }
        else if (type == typeof(Bounds))
        {
            Bounds b = (Bounds)property.Value;
            property.Value = EditorGUILayout.BoundsField(label, b, options);
        }
        else if (type == typeof(AnimationCurve))
        {
            if (property.Value == null)
            {
                property.Value = new AnimationCurve();
            }
            property.Value = EditorGUILayout.CurveField(label, (AnimationCurve)property.Value, options);
        }
        else if (type == typeof(double))
        {
            property.Value = EditorGUILayout.DoubleField(label, (double)property.Value);
        }
        else if (type == typeof(float))
        {
            property.Value = EditorGUILayout.FloatField(label, (float)property.Value);
        }
        else if (type == typeof(int))
        {
            property.Value = EditorGUILayout.IntField(label, (int)property.Value, options);
        }
        else if (type == typeof(long))
        {
            property.Value = EditorGUILayout.LongField(label, (long)property.Value, options);
        }
        else if (type == typeof(Rect))
        {
            property.Value = EditorGUILayout.RectField(label, (Rect)property.Value, options);
        }
        else if (type == typeof(bool))
        {
            property.Value = EditorGUILayout.Toggle(label, (bool)property.Value, options);
        }
        else if (type == typeof(Vector2))
        {
            property.Value = EditorGUILayout.Vector2Field(label, (Vector2)property.Value, options);
        }
        else if (type == typeof(Vector3))
        {
            property.Value = EditorGUILayout.Vector3Field(label, (Vector3)property.Value, options);
        }
        else if (type == typeof(Vector4))
        {
            property.Value = EditorGUILayout.Vector4Field(label.text, (Vector4)property.Value, options);
        }
        else if (type == typeof(string))
        {
            property.Value = EditorGUILayout.TextField(label, (string)property.Value, options);
        }
        else
        {
            property.isExpanded = EditorGUILayout.Foldout(property.isExpanded, label);
            if (property.isExpanded)
            {
                EditorGUI.indentLevel++;
                for (int i = 0; i < property.ChildCount; i++)
                {
                    SerializedPropertyX child = property.GetChildAt(i);
                    if (child.IsDrawable)
                    {
                        PropertyField(child, child.label, child.isExpanded, options);
                    }
                }
                EditorGUI.indentLevel--;
            }
        }
    }