| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This library provides an Inspector dropdown (SubclassSelector) for fields serialized by Unity's [SerializeReference].
Generic support improvements on Unity 2023.2+
See below for the reason for the limitation of versions less than Unity 2021.3. https://blog.unity.com/engine-platform/serializereference-improvements-in-unity-2021-lts
Download any version from releases. https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases
Open Package Manager → “Add package from git URL…”, then input:
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions
To pin a version, append #{VERSION}:
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions#1.7.0
Or, you can install this package from the Open UPM registry.
More details here.
openupm add com.mackysoft.serializereference-extensions
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");
}
}Unity 2023.2+ supports generic type instances for SerializeReference fields more reliably. SerializeReferenceExtensions also enhances type discovery for generic base types.
Examples:
Note: Candidate types shown in the menu still follow “Type > eligibility rules” below.
The dropdown list is built by:
| Rule | Eligible | Notes |
|---|---|---|
| Top-level public type | ✅ | Internal / non-public top-level types are not listed |
| Nested private type | ✅ | Supported (useful for encapsulated implementations) |
| Nested public type | ✅ | Listed like other candidates |
| abstract | ❌ | Cannot be instantiated |
| generic type (open or constructed) | ❌ | See “Generic support” (base type generics are supported, but generic candidate types are excluded) |
| derives from UnityEngine.Object | ❌ | Unity SerializeReference limitation |
| [Serializable] is applied | ✅ | Unity SerializeReference requires serializable types |
| [HideInTypeMenu] is applied | ❌ | Explicitly hidden from menu |
| Field base type | Support | How compatibility is checked |
|---|---|---|
| non-generic interface / abstract class | ✅ | baseType.IsAssignableFrom(candidateType) |
| generic base type (Unity 2023.2+) | ✅ | candidate must implement/derive from the same generic definition and match type arguments (supports variance for in / out) |
| generic base type (Unity < 2023.2) | ⚠️ | Unity engine limitations may prevent correct serialization of generic instances |
Check the eligibility rules:
No. Unity does not allow UnityEngine.Object types in SerializeReference.
Unity documentation states that value types are not supported for SerializeReference, but in practice boxed structs may work in some scenarios. If you rely on structs, test on your target Unity versions and prefer classes when possible.
It is a limitation of SerializeReference of Unity.
When serializing a SerializeReference reference, the type name, namespace, and assembly name are used, so if any of these are changed, the reference cannot be resolved during deserialization.
To solve this problem, UnityEngine.Scripting.APIUpdating.MovedFromAttribute can be used.
Also, this thread will be helpful.
I welcome feature requests and bug reports in issues and pull requests.
If you feel that my works are worthwhile, I would greatly appreciate it if you could sponsor me.
GitHub Sponsors: https://github.com/sponsors/mackysoft
| Back | FazBrowse Home | New Git URL |