Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Serializable dictionary class for Unity

License

NotificationsYou must be signed in to change notification settings

azixMcAze/Unity-SerializableDictionary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A serializable dictionary class for Unity.

Unity cannot serialize standard dictionaries. This means that they won't show or be edited in the inspectorand they won't be instantiated at startup. A classic workaround is to store the keys and values in separate arraysand construct the dictionary at startup.

This project provides a generic dictionary class and its custom property drawer that solves this problem.

General screenshot

Features

  • It inherits fromDictionary<TKey, TValue>

  • It implements aCopyFrom(IDictionary<TKey, TValue>) method to help assign values from regular dictionaries

  • You can use any serializable type by unity as key or value.

  • It can be edited in the inspector without having to implement custom editors or property drawers.

  • The inspector will handle invalid dictionary keys such as duplicated ornull keys and warn the user that data loss can occur if the keys are not fixed.

    Conflicting keys screenshot

    Null key screenshot

Limitations

  • A non-generic derived class has to be created for each<TKey, TValue> combination you want to use. ACustomPropertyDrawer has to be declared for each of these classes.
  • Multiple editing of scripts usingSerializableDictionaries in the inspector is not supported. The inspector will show the dictionaries but data loss is likely to occur.
  • The conflicting key detection does not work when usingLayerMask as key. TheLayerMask value is changed after theCustomPropertyDrawer execution.
  • Dictionaries of lists or arrays must use the 3 argumentsSerializableDictionary<TKey, TValue, TValueStorage> dictionary class with the extraSerializableDictionary.Storage<TValue> class to hold the values. See the "Dictionary of lists or arrays" section for details.

Usage

Simple dictionary example

To create a serializable dictionary of type<string, string>:

  • Create aSerializableDictionary subclass
    [Serializable]publicclassStringStringDictionary:SerializableDictionary<string,string>{}
  • UseStringStringDictionary in your scripts as a normalIDictionary<string, string> type

Dictionary of lists example

To create a serializable dictionary of type<string, List<Color>>:

  • Create aSerializableDictionary.Storage subclass to hold the list

    [Serializable]publicclassColorListStorage:SerializableDictionary.Storage<List<Color>>{}
  • Create aSerializableDictionary subclass using the previous subclass

    [Serializable]publicclassStringColorListDictionary:SerializableDictionary<string,List<Color>,ColorListStorage>{}
  • UseStringColorListDictionary in your scripts as a normalIDictionary<string, List<Color>> type

Details

Older versions of Unity (before 2020.1) are unable to directly serialize generic types. Therefore, you need to create a derived class for eachSerializedDictionary specialization you want.

[Serializable]publicclassStringStringDictionary:SerializableDictionary<string,string>{}[Serializable]publicclassMyScriptColorDictionary:SerializableDictionary<MyScript,Color>{}

You can use your own serializable classes.

[Serializable]publicclassMyClass{publicinti;publicstringstr;}[Serializable]publicclassStringMyClassDictionary:SerializableDictionary<string,MyClass>{}

Add the dictionaries to your scripts and access them directly of through a property.The dictionaries can be accessed through a property of typeIDictionary<TKey, TValue> for better encapsulation.

publicStringStringDictionarym_myDictionary1;[SerializeField]MyScriptColorDictionary m_myDictionary2;publicIDictionary<MyScript,Color>MyDictionary2{get{returnm_myDictionary2;}set{m_myDictionary2.CopyFrom(value);}}publicStringMyClassDictionarym_myDictionary3;

TheCopyFrom(value) method clears them_myDictionary2 dictionary and adds to it each of content of thevalue dictionary, effectively copyingvalue intom_myDictionary2.

SerializableDictionary has a copy constructor fromIDictionary<TKey, TValue>. As constructors from parent classes cannot be used directly, you have to add a copy constructor to your derived classes calling the base constructor in order to use it.

[Serializable]publicclassStringColorDictionary:SerializableDictionary<string,Color>{publicStringColorDictionary(IDictionary<string,Color>dict):base(dict){}}

Dictionary of lists or arrays

Because unity cannot serialize a array of lists or an array of arrays, using aSerializableDictionary<TKey, TValue[]> or aSerializableDictionary<TKey, List<TValue>> in a script will not work properly. The dictionary will not show up in the inspector and the values will not be saved.

It is necessary to create an intermediate class that will contain the list or array. This class can then be contained in an array and be serialized by Unity.

Create a class that inherits fromSerializableDictionary.Storage<List<TValue>. This storage class will only contain aList<TValue> data field.

[Serializable]publicclassColorListStorage:SerializableDictionary.Storage<List<Color>>{}

If you use this storage class directly with SerializableDictionary, you will have to access the list or array through the.data field of theStorage class because your dictionary will inherit fromDictionary<TKey, Storage<TValue>> instead ofDictionary<TKey, List<TValue>>. This is far from ideal.

// non optimal example for a dictionary of color list[Serializable]publicclassColorListStorage:SerializableDictionary.Storage<List<Color>>{}[Serializable]publicclassStringColorListDictionary:SerializableDictionary<string,ColorListStorage>{}publicStringColorListDictionarym_colorStringListDict;// you would have to access the color list through the .data field of ColorListStorageList<Color>colorList=m_colorStringListDict[key].data;

To access the lists directly, use the special 3 argumentsSerializableDictionary<TKey, TValue, TValueStorage> class whereTValueStorage is the class previously created.

[Serializable]publicclassColorListStorage:SerializableDictionary.Storage<List<Color>>{}[Serializable]publicclassStringColorListDictionary:SerializableDictionary<string,List<Color>,ColorListStorage>{}publicStringColorListDictionarym_colorStringListDict;// you can now access directly the color listList<Color>colorList=m_colorStringListDict[key];

About

Serializable dictionary class for Unity

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp