To enable a dropdown menu in the Unity Inspector for fields using [SerializeReference], apply the [SubclassSelector] attribute to the field. This allows you to select concrete types, search via a fuzzy finder, and supports collections (like T[] or List<T>) and nested types.
Basic Usage
using System;
using UnityEngine;
public class Example : MonoBehaviour {
// Enables the dropdown for this interface implementation
[SerializeReference, SubclassSelector]
ICommand m_Command;
// Supports collections
[SerializeReference, SubclassSelector]
ICommand[] m_Commands = Array.Empty<ICommand>();
}
public interface ICommand {
void Execute();
}
[Serializable]
public class DebugCommand : ICommand {
public void Execute() => Debug.Log("Debug");
}
using System;
using UnityEngine;
public class Example : MonoBehaviour {
// The type that implements ICommand will be displayed in the popup.
[SerializeReference, SubclassSelector]
ICommand m_Command;
// Collection support
[SerializeReference, SubclassSelector]
ICommand[] m_Commands = Array.Empty<ICommand>();
void Start () {
m_Command?.Execute();
foreach (ICommand command in m_Commands) {
command?.Execute();
}
}
// Nested type support
[Serializable]
public class NestedCommand : ICommand {
public void Execute () {
Debug.Log("Execute NestedCommand");
}
}
}
public interface ICommand {
void Execute ();
}
[Serializable]
public class DebugCommand : ICommand {
[SerializeField]
string m_Message;
public void Execute () {
Debug.Log(m_Message);
}
}
[Serializable]
public class InstantiateCommand : ICommand {
[SerializeField]
GameObject m_Prefab;
public void Execute () {
UnityEngine.Object.Instantiate(m_Prefab,Vector3.zero,Quaternion.identity);
}
}
// Menu override support
[AddTypeMenu("Example/Add Type Menu Command")]
[Serializable]
public class AddTypeMenuCommand : ICommand {
public void Execute () {
Debug.Log("Execute AddTypeMenuCommand");
}
}
[Serializable]
public struct StructCommand : ICommand {
public void Execute () {
Debug.Log("Execute StructCommand");
}
}
"}