Search Unity

Locate CustomPropertyDrawer from SerializedObject ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by canis, Mar 22, 2017.

  1. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    trying to build the Node Editor based on the others.
    I wonder if there is a way to find the PropertyDrawer/PropertyEditor from the SerializedObject.

    case:
    Code (CSharp):
    1. public TestObject : ScriptableObject { ... }
    2.  
    3. [CustomPropertyDrawer(typeof(TestObject)]
    4. public TestObjectDrawer : PropertyDrawer { ..... }
    //----------------------------
    during drawing my Node editor canvas, we use **GUILayout.Window()** for display those TestObject
    since those TestObject can be any scriptableObject, I would like to use the Unity CustomPropertyXXX to display the result.
    anyone have idea ? (sorry for English)
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Welcome to the Dark Side. You'll need to use Reflection to get the property handler then use Reflection again to get the drawer
    Something like this may help:

    Code (CSharp):
    1.  
    2. public class PropertyHandler {
    3.  
    4.     private static MethodInfo getHandler;
    5.     private static object[] getHandlerParams;
    6.  
    7.     private object handler;
    8.     private Type type;
    9.  
    10.     private PropertyInfo propertyDrawerInfo;
    11.     private MethodInfo guiHandler;
    12.     private object[] guiParams;
    13.  
    14.     public PropertyDrawer propertyDrawer {
    15.  
    16.         get { return propertyDrawerInfo.GetValue(handler, null) as PropertyDrawer; }
    17.     }
    18.  
    19.     static PropertyHandler() {
    20.  
    21.         getHandler = Type.GetType("UnityEditor.ScriptAttributeUtility, UnityEditor").GetMethod("GetHandler", BindingFlags.NonPublic | BindingFlags.Static);
    22.         getHandlerParams = new object[1];
    23.     }
    24.  
    25.     private PropertyHandler(object handler) {
    26.  
    27.         this.handler = handler;
    28.  
    29.         type = handler.GetType();
    30.         propertyDrawerInfo = type.GetProperty("propertyDrawer", BindingFlags.NonPublic | BindingFlags.Instance);
    31.         guiHandler = type.GetMethod("OnGUI", BindingFlags.Public | BindingFlags.Instance);
    32.         guiParams = new object[4];
    33.     }
    34.  
    35.     public bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren) {
    36.  
    37.         guiParams[0] = position;
    38.         guiParams[1] = property;
    39.         guiParams[2] = label;
    40.         guiParams[3] = includeChildren;
    41.  
    42.         return (bool)guiHandler.Invoke(handler, guiParams);
    43.     }
    44.  
    45.     public static PropertyHandler GetHandler(SerializedProperty property) {
    46.  
    47.         getHandlerParams[0] = property;
    48.  
    49.         return new PropertyHandler(getHandler.Invoke(null, getHandlerParams));
    50.     }
    51. }
    52.  
    Check out the "PropertyHandler" class in the UnityEditor namespace if you have DotPeek, lot's of other useful methods and properties in there. Shame all this stuff is internal, would be very handy.
     
    ggzerosum, dev_cwj and canis like this.