Have you made any useful utilities with Odin?
Login and submit your creations hereOne of Odin's best out-of-the-box features is its support for [SerializeReference]
, alongside supporting polymorphism through the Odin Serialiser.
However, the default object reference picker is (in my opinion) somewhat janky and awkward to use. And as the most common use case of [SerializeReference]
is to serialise a base class or interface, I made a [SubclassSelector]
attribute to expedite this common use case in a more user friendly manner. It comes partnered with a [SubclassPath]
attribute to customise the display-name and path when selecting types.
The [SubclassSelector]
attribute can simply be applied to a Non-UnityEngine.Object reference type member or collection:
public class SomeScriptableObject : ScriptableObject
{
[SerializeReference, SubclassSelector]
public SomeBaseClass BaseClassField;
[SerializeReference, SubclassSelector]
public List<SomeBaseClass> SomeBaseClassCollection;
}
[SubclassPath]
can be used on your class definitions to define their selection grouping and to give them designer friendly names:
public static class PlainClasses
{
public interface IBaseInterface { }
[System.Serializable]
[SubclassPath(SubClassName = "Base Class")]
public class ClassBase : IBaseInterface
{
public string TestString;
}
[System.Serializable]
[SubclassPath("Derived", "Class A")]
public class ClassA : ClassBase
{
public float ClassAFloat;
}
[System.Serializable]
[SubclassPath("Derived", "Class B")]
public class ClassB : ClassA
{
public GameObject ClassCGameObject;
}
[System.Serializable]
[SubclassPath("Derived", "Class C")]
public class ClassC : ClassBase
{
public ClassC(ClassBase classBase)
{
this.TestString = classBase.TestString;
this._classCBool = true;
}
public bool ClassCBool;
}
}
[SubclassSelector]
has been set up to work with any type of collection that Unity or Odin supports. The attribute also has a small number of properties that let you customise various aspects of the attributes behaviour.
[HideReferenceObjectPicker]
.[HideLabel]
. Only works on non-collection members.$type
named value. Note that abstract types are already filtered.