Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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:
    442
    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:
    133
    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:
    442
    Thanks for the tips. I will try that out and see what happens :)
     
  4. BinaryCats

    BinaryCats

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