Search Unity

Question Custom inspector for nested struct inside class with visibility switch

Discussion in 'Scripting' started by Hend_d, Feb 20, 2023.

  1. Hend_d

    Hend_d

    Joined:
    Feb 28, 2017
    Posts:
    5
    Hi there,
    I was searching last week for a possiblity to get a nice and clean looking inspector tab for my custom Sprite class. Some tutorials used an modified Editor class others the Propertydrawer class, anyway non of those seem to be a simple and easy way to achive my wanted inspector window. Or seemed way to advanced...and probably the wrong solution? :(
    Let's get to the code, to show want I like to have in the inspector view.
    main class:
    Code (CSharp):
    1. public class CharacterBackground : MonoBehaviour
    2. {
    3.     public CharacterSprites sprites;
    4. }
    nested struct:
    Code (CSharp):
    1.  
    2. [Serializable]
    3. public struct CharacterSprites
    4. {
    5.     public CharacterActions Gym;
    6.     ...
    7. }
    nested struct:
    Code (CSharp):
    1. public struct CharacterActions
    2. {
    3.    
    4.     public BackgroundSprites Bath;
    5.     ...
    6. }
    nested struct:
    Code (CSharp):
    1.  
    2. [Serializable]
    3.     public struct BackgroundSprites
    4.     {
    5.         public Sprite Morning;
    6.         public Sprite Day;
    7.         public Sprite Evening;
    8.         public Sprite Night;
    9.     }
    10. }
    Basically I want to hide some of those "CharacterActions" depending on which room it is. You can not perform all action in every room and this would result in a messy tree view with a lots of free slots and not needed Actions.
    Is there a way to hide them directly in the inspector, toggle button or some kind of selection menu. Or is it better to make this hard coded within the cs-file.
    I would appreciate every idea or pointing in the right direction. :)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Well as you may have already learnt, property drawers are the way to go.
    UnityEditor.Editor
    's are for types inheriting from
    UnityEngine.Object
    , while property drawers are for everything else.

    Are they confusing and complex? Yeah a little bit, especially when doing them for the first time. Start simple with some tutorials to get an idea as to how the basics work. Your particular use case is a little advanced on its own.

    There are asset store tools like Odin Inspector that can make this sort of thing easier, but having an understanding as to how to make custom inspectors is still super useful.
     
    Kurt-Dekker likes this.
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Yes and no.
    Basically the UnityEditor.Editor is to draw a complete inspector, where PropertyDrawer is to draw a field (inside of an inspector). You can still use PropertyDrawers for types inheriting from UnityEngine.Object. (the other way around makes no sense, because if your object does not inherit from UnityEngine.Object it can never have its own inspector).