在检查器中提供弹出菜单,用于指定 [SerializeReference] 属性序列化的字段类型。
在检查器中提供弹出菜单,用于指定 [SerializeReference] 属性序列化的字段类型。
This library provides an Inspector dropdown (SubclassSelector) for fields serialized by Unity's [SerializeReference].
[SerializeReference] fields via dropdown.T[], List, etc.).[AddTypeMenu].CustomPropertyDrawer for selected subtypes.Generic support improvements on Unity 2023.2+
in/out) for generic interfaces (covariance / contravariance)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
.unitypackageDownload 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.SerializeReferenceExtensionsTo pin a version, append #{VERSION}:
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions#1.7.0Or, you can install this package from the Open UPM registry.
More details here.
openupm add com.mackysoft.serializereference-extensions…Unity 2023.2+ supports generic type instances for SerializeReference fields more reliably.
SerializeReferenceExtensions also enhances type discovery for generic base types.
Examples:
[SerializeReference, SubclassSelector] ICommand cmd;ICommand (including variance rules if applicable)[SerializeReference, SubclassSelector] BaseCommand cmd;BaseCommandNote: 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:
[Serializable]?abstract?UnityEngine.Object?[HideInTypeMenu] applied?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
暂无开放 Issues,或尚未同步最近议题。