Table of Contents

ReferenceDropdown Attribute

ReferenceDropdown was created to allow selectable interface references inside of Unity Objects. They work together with SerializeReference to serialize your interface so you can use it in the inspector.

public interface ITest {}

public class StringTest : ITest { public string s; }

public class IntTest : ITest { public int i; }

public class MyMonoBehaviour : MonoBehaviour
{
    [SerializeReference, ReferenceDropdown] public ITest test;
}

Restrictions

In order for an implementation to be selectable, it must adhere to the following rules:

  • The implementation must have an empty constructor.
  • The implementation must not be abstract.
  • The implementation must not derive from UnityEngine.Object.

Nullable

You can specify your reference to be nullable through the following:

public class MyMonoBehaviour : MonoBehaviour
{
    [SerializeReference, ReferenceDropdown(nullable = true)] public ITest test;
}

Types

Whilst SerializeReference can serialize interfaces, it cannot serialize generics. In order to circumvent this issue, you can specify types that the reference needs to be assignable from.

public interface ITest<T> {}

public class Text : ITest<string> {}

public class Password : ITest<string> {}

public class MyMonoBehaviour : MonoBehaviour
{
    [SerializeReference, ReferenceDropdown] public ITest<string> invalid; // invalid

    [SerializeReference, ReferenceDropdown(typeof(ITest<string>))] public object valid; // valid
}