Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom Editor for type not inheriting from MonoBehaviour?

Discussion in 'Scripting' started by VipHaLongPro, May 16, 2015.

  1. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    I really don't know how to figure this out and already done some search about this. I can make a simple working example of Custom Editor for type (class) inheriting from MonoBehaviour. It's so simple that I just need to show a label field like this:

    Code (CSharp):
    1. public class Test : MonoBehaviour {}
    2.  
    3. [CustomEditor(typeof(Test))]
    4. public class TestEditor : Editor {
    5.       public override void OnInspectorGUI(){
    6.            EditorGUILayout.LabelField("I'm shown OK");
    7.       }
    8. }
    However I would like to create a custom editor for a pure class (implicitly) inheriting from System.Object (in C#) like this:

    Code (CSharp):
    1. [System.Serializable]
    2. public class Test {}
    But somehow the exact code for custom editor above does not work at all. There is nothing shown. Could you explain to me what's wrong here. I believe technically we should be able to create custom editor for all types of classes and structs.

    Thank you all for your help.
     
    Last edited: May 18, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Don't create custom editor, use property drawer:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomPropertyDrawer( typeof( Test ) )]
    6. public class Ed : PropertyDrawer {
    7.  
    8.     public override float GetPropertyHeight ( SerializedProperty property, GUIContent label ) {
    9.         return base.GetPropertyHeight( property, label );
    10.     }
    11.  
    12.     public override void OnGUI ( Rect position, SerializedProperty property, GUIContent label ) {
    13.         GUI.Label( position, "This is a Test" );
    14.     }
    15.  
    16. }
    17.  
     
    VipHaLongPro likes this.
  3. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    Thank you, it looks more promising. However it seems to be more difficult to use (because I don't seem be able to use EditorGUILayout with it). Also I've tried playing with it a little. For simple example (like the one you gave) it works. But my property has a complex type (Test is an example). In Inspector, it has an expanding arrow (like in TreeView) so that expanding it will show all its sub properties. I don't know why all the sub properties are drawn on the other properties (of Test) below. While it should not be drawn when the before property is collapsed. I've seen a simple example in Unity manual, it does have some similar collapsible properties but the code in there has nothing special. Could you tell me what is probably wrong here?

    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2. {
    3.    int i = 0;
    4.    foreach (SerializedProperty p in property) {
    5.        EditorGUI.PropertyField(new Rect(position.xMin, position.yMin + 30 * (i++),
    6.                                                position.width, position.height), p);
    7.    }
    8. }
    All the sub properties have just basic data types.

    Thank you for your help!
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Thats why i included the getPropertyHeight function in the example
    change what it returns to control the size of the area that's made available (dependant on the toggle state)
    And yes, No layout
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can use layout if you create an empty custom inspector for the parent MonoBehaviour. Its a bit of a hack, but it works.
     
  6. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    I don't intend to use layout now because I've already managed to finish the custom editor based on PropertyDrawer. But it would be nice if you could give me some link to that bit of hack. It would be useful in some cases I think. Thanks!
     
  7. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    Thank you, I had finished my custom editor before coming back here to view your reply.