Fixing Prefab Save Issues for Dictionary Fields in Unity Custom Editors
Автор: vlogommentary
Загружено: 2026-01-03
Просмотров: 0
Learn why Unity prefab fields using Dictionary types fail to save, and how to fix this by replacing dictionaries with serializable classes in custom editors.
---
This video is based on the question https://stackoverflow.com/q/79386992/ asked by the user 'CHP' ( https://stackoverflow.com/u/923757/ ) and on the answer https://stackoverflow.com/a/79387171/ provided by the user 'Charles Henington' ( https://stackoverflow.com/u/18423247/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Prefab changes not saved for Custom Editor fields that store data for a complex object
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
The Problem: Dictionaries Not Saving in Prefabs
When creating custom editors in Unity to edit complex data structures, you might find that some fields save correctly in prefabs, but others reset unexpectedly after closing and reopening the project.
Specifically, using Dictionary string, float fields inside a serializable class does not persist changes made in the custom inspector, even though basic types like int and string save fine.
This is a common issue due to Unity’s serialization system not supporting Dictionary or even KeyValuePair types.
Why This Happens
Unity's built-in serialization does not support generic dictionaries.
Any changes made to dictionary fields appear immediate in the inspector because they are modified at runtime, but these changes are not serialized when saving the prefab.
Upon reloading, the dictionary fields revert to their initial state.
Using a List<KeyValuePair<string, float>> instead doesn’t help as KeyValuePair also isn’t serializable by Unity.
The Solution: Use Serializable Custom Classes Instead of Dictionaries
To work around Unity serialization limits, replace the dictionary with a serializable class or record that holds key-value pairs.
Steps:
Define Serializable Classes for Key-Value Pairs:
[[See Video to Reveal this Text or Code Snippet]]
Update Your Data Class to use Arrays of These Classes:
[[See Video to Reveal this Text or Code Snippet]]
Modify Your Custom Editor to Edit These Arrays:
Iterate over Multipliers and Addends arrays instead of dictionaries.
Provide UI elements to edit Key and Value fields.
Benefits of This Approach
Fully compatible with Unity's serialization system.
Prefab changes to these fields persist after saving and reopening.
Clear structure that works with Unity's inspector.
Final Notes
Avoid using types not supported by Unity serialization (e.g., Dictionary, KeyValuePair).
Arrays or lists of simple serializable classes are the recommended pattern.
This approach ensures your custom editor fields save reliably in prefabs.
By replacing dictionary fields with serializable classes or records representing key-value pairs, you enable full prefab serialization compatibility and avoid frustrating data loss issues.
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: