Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How should I tackle this problem: Property Drawer, Custom Editor or Attributes?

Discussion in 'Editor & General Support' started by Rayeloy, Nov 24, 2023.

  1. Rayeloy


    Feb 12, 2017
    Hello Unity community, I've been using my own UI Animation system for long now, but I've never got to have an adequate inspector for it because I always get to a wall at some point when trying to:

    The system is organized like this:
    1- A Manager monobehaviour that controls the flow of the animations.
    2- non-monobehaviour animations clases using inherintance. The base class is UIAnimation and then an example child class would be UIAnimation_Scale.
    3- These UIAnimations can also have a base settings scriptable object, that when it is set, the rest of the parametters are ignored.
    4- The Base Settings scriptable object, is just a holder for a UIAnimation class.
    5- When declaring a public UIAnimation class, I need to be able to create/choose/add any of its child classes, not the parent class.

    This is the BaseSettings class, that uses [SerializeReference] and a custom attribute from the package CareBoo to achieve it:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CareBoo.Serially;
    6. namespace UIAnimations
    7. {
    8.     [CreateAssetMenu(fileName = "New UIAnimation Base Settings", menuName = "UIAnimations/Base Settings")]
    9.     public class UIAnimationBaseSettingData : ScriptableObject
    10.     {
    11.         [SerializeReference]
    12.         [ShowSerializeReference]
    13.         public UIAnimation baseUIAnimation;
    14.     }
    15. }
    However, I wonder if there is a better way to do this. This is also the way I show the UIAnimation inside the inspector when inside another monobehaviour. It looks like this:

    I would like to have a toggle/bool to show/hide some values to not make the inspector crowded with unnecessary info. such as the read only parametters, and some advanced options. (I tried with the "Show Advanced Options" bool as seen there, but is not functional).

    On the other hand, I've tried some [ShowIf()] attributes that I saw around, but they do not work well when showing UnityEvents, which take a lof of the space.
    Custom Editor doesn't seem like a solution because I would have to write a new one with new code over and over again for every new monobehaviour that contains a UIAnimation. Property Drawer would work... but with these 2 attributes I wrote (and need) it does not work. Attributes are the only thing somehow working as I intend it to, but not completely, as the ShowIf[()] attributes that I saw posted around by different people don't work with UnityEvents at all.

    I'm really lost and I don't know where I went wrong with this. Should I completely reestructure my code? How?
    Is there some magic solution I'm missing?

    I can't find any information whatsoever for any of these problems and ChatGPT is completely clueless too. Please, help me!
    Last edited: Nov 24, 2023
  2. CodeSmile


    Apr 10, 2014
    I know you mention "for long" but nevertheless, animation is one of the strong points of UIElements. You can do animations simply via CSS (USS).

    As to the Inspector, I can see two ways of improving this:
    1. Group fields that belong together into serializable subclasses, and only have a single field of this class in the SO. This makes the class appear as a foldout in Inspector, and when expanded shows the fields of the class.
    2. A custom editor window that works with a common object of yours (UIAnimationData). This gives you the option to leave only the most commonly edited fields in the Inspector, and to edit anything else you open a separate tool Window.

    More about 2)

    Users may dock the window and leave it open. This works well if the window code hooks into the Selection class' events to update what the window shows, and if no valid selection then it will just show a "not available" label.

    Also you can design that window rather quickly with UI Builder. This Asset Inspector I created a couple days ago is 100% handcrafted in UI Builder in negligible time compared to coded IMGUI craptastrophe:

    Yes, there's actually an image here but it's cut off because my monitor real estate runs out past 1440 verticel pixels. :D

    The foldouts here are Visual Elements, not what I described above with the nested serializable classes.
    Rayeloy likes this.
  3. Rayeloy


    Feb 12, 2017
    Thank you very much for your insight. I didn't know about UIElements animations, I will investigate more about it surely. On the other hand, I find the first solution really interesting, and feel a bit dumb I didn't think of it earlier.
    Solution 2 seems like the most profesional one, but even tho I've createe editor windows before, I've never created one for a specific class to edit a variable in a monobehaviour. i will read about it too.
    Thank you for interest in helping! I will write here when I ger to try these options :)
  4. spiney199


    Feb 11, 2021
    Worth noting that
    s are for custom inspectors for
    . PropertyDrawers are for serializable C# classes or other non-Unity objects types that you serialise into Unity objects. Attributes are just meta data that other systems look for and react to. It's not really an either-or situation, they are all used for different things.

    So if you want to customise the display of a scriptable object, use an Editor. If it is a serialised C# classes or other specific type, use a property drawer.

    Attributes are generally used with property drawers and decorator drawers to provide opt-in and configurable customised inspector drawing.

    For this kind of thing an Editor Window might be more worthwhile. Or potentially investing in Odin Inspector which gives you pretty powerful attribute-based means of customising inspectors.
    Rayeloy likes this.