https://github.com/mitay-walle/serializedinterfacereference
Unity3d script to Serialize reference to UnityEngine.Object inherited from interface, with constraints
https://github.com/mitay-walle/serializedinterfacereference
Last synced: 5 months ago
JSON representation
Unity3d script to Serialize reference to UnityEngine.Object inherited from interface, with constraints
- Host: GitHub
- URL: https://github.com/mitay-walle/serializedinterfacereference
- Owner: mitay-walle
- License: mit
- Created: 2023-01-28T01:54:36.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-31T00:40:13.000Z (over 2 years ago)
- Last Synced: 2025-05-08T03:13:23.132Z (5 months ago)
- Language: C#
- Size: 210 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SerializedInterfaceReference
Unity3d script to Serialize reference to UnityEngine.Object inherited from interface, with constraints[forum discussion about interface serialization](https://forum.unity.com/threads/serialized-interface-fields.1238785/)

Type switch used in gif by Odin Inspector
## Problems
1. Interface reference to UnityEngine.Object can't be serialized with built-in serialization
2. two-fields solution for 1. allow wrong-typed references
3. need some execution-context to cast serialized reference to interface
4. UnityEngine.Object and C#-plain classes implementing same interface can't be save to same field## Solutions
1. Use plain C#-class to serialize reference
2. ISerializationCallback.OnBeforeSerialize (for serialization) and property setters (for runtime assign) to prevent wrong-typed values
3. ISerializationCallback.OnAfterDeserialize to cast value before any other execution, and no need to duplicate execution
4. use SerializeReference with interface and create plain class-container, including field with reference and inherited from interface