Search Unity

PropertyAttribute and PropertyDrawer doesn't work?!

Discussion in 'Immediate Mode GUI (IMGUI)' started by Assassinbeast, May 12, 2015.

  1. Assassinbeast

    Assassinbeast

    Joined:
    Nov 4, 2013
    Posts:
    31
    Hello, i just followed this sample about the "Range" property in the manual http://docs.unity3d.com/Manual/editor-PropertyDrawers.html

    Im very sure i have made it right, but in the inspector, it says "No GUI implemented" when i try to use a propertyattribute on a float value.

    Below is my code and screenshots:

    MyRangeAttribute.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5.  
    6. public class MyRangeAttribute : PropertyAttribute
    7. {
    8.     public float min;
    9.     public float max;
    10.  
    11.     public MyRangeAttribute(float min, float max)
    12.     {
    13.         this.min = min;
    14.         this.max = max;
    15.     }
    16. }

    MyRangeDrawer.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomPropertyDrawer(typeof(MyRangeAttribute))]
    6. public class MyRangeDrawer : PropertyDrawer
    7. {
    8.     void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    9.     {
    10.         MyRangeAttribute range = attribute as MyRangeAttribute;
    11.         if (property.propertyType == SerializedPropertyType.Float)
    12.             EditorGUI.Slider(position, property, range.min, range.max, label);
    13.     }
    14. }

    MyTest.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyTest : MonoBehaviour
    5. {
    6.     [MyRangeAttribute(0,20)] public float Hello;
    7. }
    8.  
    Bug.png

    I am using Unity version 5.0.1f1
     
  2. Assassinbeast

    Assassinbeast

    Joined:
    Nov 4, 2013
    Posts:
    31
    Okay, i just figured out that you have to override OnGUI()
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Not only override - public override:
    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.     {
    3.         MyRangeAttribute range = attribute as MyRangeAttribute;
    4.         if (property.propertyType == SerializedPropertyType.Float)
    5.             EditorGUI.Slider(position, property, range.min, range.max, label);
    6.     }
    You are overriding the implementation from the base class.