Search Unity

custom inspector: how do you show all the properties of a component?

Discussion in 'Immediate Mode GUI (IMGUI)' started by laurentlavigne, Jan 6, 2018.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    I want to display all the variables of a class like this

    Code (CSharp):
    1. public class Thing:ScriptableObject
    2. {
    3.    public float gold;
    4.    public string name;
    5.    public Thing parentThing;
    6. }
    But I don't want to code that for each class so I tried iterating through property and it doesn't do that.
    What's the trick?
    Code (CSharp):
    1.     public override void OnInspectorGUI()
    2.     {
    3.         var item = target as Item;
    4.         serializedObject.Update();
    5.  
    6.         Color cachedGuiColor = GUI.color;
    7.  
    8.         // Draw attributes with a delete button below each one:
    9.         int indexToDelete = -1;
    10.  
    11.         for (int i = 0; i < item.attributes.Count; i++)
    12.         {
    13.             var property = (item.attributes [i] as SerializedObject).GetIterator();// serializedObject.GetIterator ();
    14.             var next = property.NextVisible(true);
    15.             if (next)
    16.             do {
    17.                 EditorGUILayout.PropertyField (property);
    18.             } while (property.NextVisible (true));
    19.         }
    20.  
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    Do you only want to display the fields, or do you just want to embed the whole Editor for your target object in the Inspector? If the latter, use Editor.CreateEditor() and then just call OnInspectorGUI on the result.
     
    orrinjones likes this.
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    I only want all the fields.
    But I tried CreateEditor and it doesn't display anything, maybe I'm using it wrong:

    Code (CSharp):
    1.     public override void OnInspectorGUI()
    2.     {
    3.         var item = target as Item;
    4.         serializedObject.Update();
    5.  
    6.         for (int i = 0; i < item.attributes.Count; i++)
    7.         {
    8.             GUILayout.BeginHorizontal(EditorStyles.helpBox);
    9.             if (item.attributes [i] != null)
    10.             {
    11.                 Editor.CreateEditor (item.attributes [i]);
    12.             }
    13.             EditorGUILayout.EndHorizontal();
    14.         }
    attribute is this base class
    Code (CSharp):
    1. public abstract class ItemAttribute : ScriptableObject
    2. {
    3. }
    4.  
    even if I add a public field in the ItemAttribute the CreateEditor shows nothing
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    now I get it
    Editor.CreateEditor (item.attributes ).OnInspectorGUI();
    so nice.

    Unity_2018-01-07_03-22-47.png
    how do I get rid of the script and label at the top? (costattribute, armorattribute etc...)
     
    Last edited: Jan 7, 2018
    E_Nickelodeon likes this.
  6. Dest8

    Dest8

    Joined:
    Dec 30, 2015
    Posts:
    1
    To get rid of the "Script" field at the top you can do something like this:

    Code (CSharp):
    1.         while (property.NextVisible(enterChildren))
    2.         {
    3.             if ("m_Script" == property.propertyPath)
    4.             {
    5.                 continue;
    6.             }
    7.          ...
    8.         }