Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

Discussion in 'Extensions & OnGUI' started by Kiupe, Apr 20, 2018.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    383
    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:
    106
    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:
    383
    Thanks for the tips. I will try that out and see what happens :)
     
  4. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    106
    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:
    383
    I have just made a quick test and it works. Thanks !