Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork71
Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.
License
mackysoft/Unity-SerializeReferenceExtensions
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Inspired bythis post.
TheSerializeReference
attribute, added in Unity 2019.3, makes it possible to serialize references to interfaces and abstract classes.
TheSubclassSelector
attribute allows you to easily set subclasses of those abstract classes in the Editor that are serialized bySerializeReference
attribute.
- Easily set subclass by popup.
- [New] Type finding by fuzzy finder.
- [New] Override the type name and path by the
AddTypeMenu
attribute. - [New] Available
CustomPropertyDrawer
for subclasses. - [New] Restore values of previous object from JSON when subclass is changed. (required Unity 2021.3 or later)
- [New] Copy & Paste the subclass properties. (required Unity 2021.3 or later)
- [New] Clear & reset the subclass properties. (required Unity 2021.3 or later)
- [New] Covariance / Contravariance support. (required Unity 2023.2 or later)
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.
Releases:https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases
Or, you can add this package by opening PackageManager and entering
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions
from theAdd package from git URL
option.
If you are specifying a version, enter#{VERSION}
at the end, as shown below.
https://github.com/mackysoft/Unity-SerializeReferenceExtensions.git?path=Assets/MackySoft/MackySoft.SerializeReferenceExtensions#1.1.9
Or, you can install this package from theOpen UPM registry.
More detailshere.
openupm add com.mackysoft.serializereference-extensions
usingSystem;usingUnityEngine;publicclassExample:MonoBehaviour{// The type that implements ICommand will be displayed in the popup.[SerializeReference,SubclassSelector]ICommandm_Command;// Collection support[SerializeReference,SubclassSelector]ICommand[]m_Commands=Array.Empty<ICommand>();voidStart(){m_Command?.Execute();foreach(ICommandcommandinm_Commands){command?.Execute();}}// Nested type support[Serializable]publicclassNestedCommand:ICommand{publicvoidExecute(){Debug.Log("Execute NestedCommand");}}}publicinterfaceICommand{voidExecute();}[Serializable]publicclassDebugCommand:ICommand{[SerializeField]stringm_Message;publicvoidExecute(){Debug.Log(m_Message);}}[Serializable]publicclassInstantiateCommand:ICommand{[SerializeField]GameObjectm_Prefab;publicvoidExecute(){UnityEngine.Object.Instantiate(m_Prefab,Vector3.zero,Quaternion.identity);}}// Menu override support[AddTypeMenu("Example/Add Type Menu Command")][Serializable]publicclassAddTypeMenuCommand:ICommand{publicvoidExecute(){Debug.Log("Execute AddTypeMenuCommand");}}[Serializable]publicstructStructCommand:ICommand{publicvoidExecute(){Debug.Log("Execute StructCommand");}}
TheSubclassSelector
attribute supports types that meet the following conditions.
- Public or nested private
- Not abstract
- Not generic
- Not unity object
- Serializable attribute is applied.
It is a limitation ofSerializeReference
of Unity.
When serializing aSerializeReference
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.
- https://forum.unity.com/threads/serializereference-data-loss-when-class-name-is-changed.736874/
- https://issuetracker.unity3d.com/issues/serializereference-serialized-reference-data-lost-when-the-class-name-is-refactored
I welcome feature requests and bug reports inissues andpull 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
About
Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.