Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. 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
  8. 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:
    412
    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:
    126
    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:
    412
    Thanks for the tips. I will try that out and see what happens :)
     
  4. BinaryCats

    BinaryCats

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