Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Nested Class EditorScript

Discussion in 'Scripting' started by mexicano, Jun 4, 2019.

  1. mexicano

    mexicano

    Joined:
    Nov 12, 2013
    Posts:
    23
    Hi Guys ,
    I am having some trouble with unity...
    I need to create an Editor script to Hide and unhide elements.
    I know I can use DrawPropertiesExcluding for chosing what to Hide
    Code (CSharp):
    1. string[] _dontIncludeMe = new string[] { "float1" };
    2.  
    3.     public override void OnInspectorGUI()
    4.     {
    5.         serializedObject.Update();
    6.  
    7.         DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
    8.  
    9.         serializedObject.ApplyModifiedProperties();
    10. }
    and use RepaintInspector to refresh it

    Code (CSharp):
    1. public static void RepaintInspector(System.Type t)
    2.     {
    3.         Editor[] ed = (Editor[])Resources.FindObjectsOfTypeAll<Editor>();
    4.         for (int i = 0; i < ed.Length; i++)
    5.         {
    6.             if (ed[i].GetType() == t)
    7.             {
    8.                 ed[i].Repaint();
    9.                 return;
    10.             }
    11.         }
    12.     }
    But here is the real problem
    I need to edit a nested class called "EventData"
    here is the code
    Code (CSharp):
    1. public class EventRelay : MonoBehaviour
    2. {
    3.     [Serializable]
    4.     public class FloatFloatEvent : UnityEvent<EventData> { }
    5.     [Serializable]
    6.     public struct RelayEvent
    7.     {
    8.         public EventData Data;
    9.     }
    10.  
    11.  
    12.         [System.Serializable]
    13.         public class EventData
    14.     {
    15.             public FloatFloatEvent Event;
    16.  
    17.             [TagSelector]
    18.             [Header("Tags")]
    19.             public string[] tags;
    20.             [Header("Action Target")]
    21.             public GameObject target;
    22.             [Header("CallbackEvent")]
    23.             public FloatFloatEvent[] CalbackEvents;
    24.             [Header("Lerp")]
    25.             public float time;
    26.  
    27.     }
    28.      
    29.  
    30.     [SerializeField]
    31.     public RelayEvent[] m_Events;
    32. }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The class being nested (as far as I know) doesn't really make it special in the eyes of the inspector. If I've understood you correctly, this is like trying to hide only the x property of a Vector3 member. In order to pull that off, you're either going to have to make a PropertyDrawer to customize the way EventData is drawn everywhere, in every inspector, or (still a PropertyDrawer) specifically modifying the way it's drawn in response to an attribute you attach to it here, or you'll have to actually write the custom editor's Draw functionality yourself instead of relying on DrawPropertiesExcluding. The latter probably ends up meaning have to update the editor script every time you make a change to its MonoBehaviour class, so I'd recommend the PropertyDrawer approach instead.
     
    Last edited: Jun 4, 2019
    mexicano likes this.
  3. mexicano

    mexicano

    Joined:
    Nov 12, 2013
    Posts:
    23
    @lysander1313 I have no practice doing PropertyDrawers.
    is there a generic way of Draw functionality? because I have very different elements like UnityEvents inside.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Wrong person, lol.

    The short answer is that PropertyField allows you to sort of "pass control" for drawing a given property to the next step in the line, meaning that you don't need to manually draw every single serialized element of a class in the inspector, but can let the normal drawer for things like UnityEvents handle those, while you handle just the ones you want to draw in a different way.

    That may not really be enough of an answer, but this is a really big subject and I'm not really wanting to sit here and describe editor scripting line-by-line when there are numerous tutorials out there that do so already, so instead, you may want to check out this tutorial on catlikecoding for a good quick start on PropertyDrawers. This tutorial series covers Custom Inspectors, but you can also watch this live training presentation for something like an overview on the whole array of Custom Inspectors, PropertyDrawers, and AttributeDrawers (just PropertyDrawers that are made for fields with a given attribute, rather than a given type).
     
    Last edited: Jun 11, 2019
    mexicano likes this.
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Worth noting that you could also purchase Odin Inspector in order to use the [ShowIf()], [HideIf()], [EnableIf()], and [DisableIf()] attributes, pointing to some boolean field/property or bool-returning function to determine when the attribute should apply.

    It seems a bit rude to just say "Oh, you need help? Just purchase this asset on the UAS and you're set!" when you can just as easily make the property drawer yourself to do the same thing, but Odin has hundreds of useful attributes, and its own serialization system which is pretty awesome too, so if you don't want to go through the effort of learning editor scripting for things like this, it's definitely an option.
     
    mexicano likes this.
  6. mexicano

    mexicano

    Joined:
    Nov 12, 2013
    Posts:
    23
    Not at all, Thank you very much for the reply I will consider that If I fail to learn my self. I will defnetly go true all the tutorials you linked. Your answers were very helpfull