Search Unity

Is it possible to re-use custom propertyDrawer inside customEditor ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Kiupe, Apr 20, 2018.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello guys,

    Simple question, is there a way inside a Custom Inspector to call/use a specific PropertyDrawer to manage the display of a property ? I ask this, because sometimes I have to re-write code that really looks the same when I want a custom inspector for a class that has a property for which I have already wrote a PropertyDrawer.

    Thanks
     
  2. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Yes you can!

    You have to get the field as a serialized property, then draw it using editorguilayout.propertyField (property)

    It's a bit of a faff to do this iirc (I don't have code with me ) if the field belongs to the custom editor. Do you have any sample code to go off, so I can give you a more defined solution?
     
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Thanks for the tips. I will try that out and see what happens :)
     
  4. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hi,

    I was able to find this snippet of code which may help you.
    Code (CSharp):
    1.  
    2.     public class MyScriptEditor : Editor
    3.     {
    4.         public override void OnInspectorGUI()
    5.         {
    6.  
    7.  
    8.   var it = serializedObject.GetIterator();
    9.             EditorGUI.BeginChangeCheck();
    10.  
    11.             bool Enter = true;
    12.             while (it.NextVisible(Enter))
    13.             {
    14.  
    15.                 if (  it.name == "Robot")
    16.                 {
    17.                     EditorGUILayout.ObjectField(it);
    18.                 }
    19.                 else
    20.                     Enter = EditorGUILayout.PropertyField(it);
    21.  
    22.             }
    23.  
    24.             if (EditorGUI.EndChangeCheck())
    25.             {
    26.                 so.ApplyModifiedProperties();
    27.  
    28.             }
    29. }
    This pretty much draws the inspector as default, except for my field labelled Robot
     
  5. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    I have just made a quick test and it works. Thanks !