Search Unity

Showcase Sub class property drawer

Discussion in 'Scripting' started by lordconstant, Jun 3, 2022.

  1. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Hello,

    I made a custom property drawer that lets you change a polymorphic class in editor & thought I'd share as I couldnt find it anywhere else.

    To use it the base class & all classes you want to set it to must set as serializable.
    Then when declaring the variable add the attributes [SerializeReference, Subclass], SerializeReference is needed as otherwise Unity wont serialize your class properly!

    This was tested on Unity 2021.3.1f, it may work on older versions but that is untested.

    It can be used on a single variable:
    Code (CSharp):
    1. [SerializeReference, Subclass]
    2. public TargetComparer Comparer;
    upload_2022-6-3_14-0-13.png

    Also works with arrays & lists:
    Code (CSharp):
    1. [SerializeReference, Subclass]
    2. public InteractFilter[] Filter;
    3.  
    4. [SerializeReference, Subclass(IsList=true)]
    5. public List<InteractFilter> Filter;
    upload_2022-6-3_14-2-2.png

    To use this your class layout should look something like this:

    Code (CSharp):
    1. [System.Serializable]
    2. public class BaseClass
    3. {
    4. };
    5.  
    6. [System.Serializable]
    7. public class ClassA : BaseClass
    8. {
    9.      public int TestInt;
    10. };
    11.  
    12. [System.Serializable]
    13. public class ClassB : BaseClass
    14. {
    15.     public float TestFloat
    16. };
    It can also be used with inherited templated classes but the type needs to be defined already:

    Code (CSharp):
    1. [System.Serializable]
    2. public class BaseClass
    3. {
    4. };
    5.  
    6. [System.Serializable]
    7. public class BaseClass<T> : BaseClass
    8. {
    9.      public T MyVar;
    10. };
    11.  
    12. [System.Serializable]
    13. public class ClassA : BaseClass<int>
    14. {
    15. };
    16.  
    17. [System.Serializable]
    18. public class ClassB : BaseClass<float>
    19. {
    20. };
    upload_2022-6-3_14-25-51.png

    It also works with deeper classes:

    Code (CSharp):
    1. [System.Serializable]
    2. public class BaseClass
    3. {
    4. };
    5.  
    6. [System.Serializable]
    7. public class ClassA : BaseClass
    8. {
    9.      public int TestInt;
    10. };
    11.  
    12. [System.Serializable]
    13. public class ClassB : ClassA
    14. {
    15.     public float TestFloat
    16. };
    upload_2022-6-3_14-28-18.png

    Download link for unity package, its just 3 files (PropertyAttribute, PropertyDrawer, SubclassSelector)
    https://github.com/lordconstant/SubclassPropertyDrawer

    Any questions feel free to ask.

    Cheers,
    Chris
     
  2. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    136
    i literally posted a thread asking for something like this 10 minutes after you did lol

    doesnt work in 2020 bc the managedReferenceValue doesnt have a getter, but i upgraded to 2021 LTS and it works just fine!

    thanks for this and thanks to @spiney199 for suggesting it to me!
     
    lordconstant likes this.
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    That makes sense, the property drawer is fully dependent on the new managed reference stuff.
    For older projects you can just use the SubclassSelector class as that handles finding the subclasses & drawing the dropdown, then you can create a custom editor for your class. Its just move convenient as a property drawer :)
     
  4. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    136
    there's some incompatibility with Editor attribute drawers, such as [TextArea]. ive posted an issue on your github :)
     
    lordconstant likes this.