All Projects → azixMcAze → Unity Serializabledictionary

azixMcAze / Unity Serializabledictionary

Licence: mit
Serializable dictionary class for Unity

Projects that are alternatives of or similar to Unity Serializabledictionary

Unity Script Collection
A maintained collection of useful & free unity scripts / library's / plugins and extensions
Stars: ✭ 3,640 (+1105.3%)
Mutual labels:  unity, unity-editor
Pb stl
STL import/export for Unity, supporting both ASCII and Binary.
Stars: ✭ 108 (-64.24%)
Mutual labels:  unity, unity-editor
Extosc
extOSC is a tool dedicated to simplify creation of applications in Unity with OSC protocol usage.
Stars: ✭ 69 (-77.15%)
Mutual labels:  unity, unity-editor
Scene View Bookmarks
Unity editor extension to bookmark scene views.
Stars: ✭ 40 (-86.75%)
Mutual labels:  unity, unity-editor
Gdk For Unity
SpatialOS GDK for Unity
Stars: ✭ 296 (-1.99%)
Mutual labels:  unity, unity-editor
Uniuguitoolbar
【Unity】uGUI のオブジェクトを作成できるツールバーのエディタ拡張
Stars: ✭ 44 (-85.43%)
Mutual labels:  unity, unity-editor
Webviewhook
Exposed Unity Editor WebView API
Stars: ✭ 107 (-64.57%)
Mutual labels:  unity, unity-editor
Autolod
Automatic LOD generation + scene optimization
Stars: ✭ 939 (+210.93%)
Mutual labels:  unity, unity-editor
Uiwidgets
UIWidget is a Unity Package which helps developers to create, debug and deploy efficient, cross-platform Apps.
Stars: ✭ 1,901 (+529.47%)
Mutual labels:  unity, unity-editor
Unity Bitmapfontimporter
An unity editor extension for bitmap font.
Stars: ✭ 139 (-53.97%)
Mutual labels:  unity, unity-editor
Unity Assetpipeline Presentation
Unity project for "A Technical Deep-Dive into Unity's Asset Pipeline" presented at Develop: 2018
Stars: ✭ 31 (-89.74%)
Mutual labels:  unity, unity-editor
Unity Aseprite Importer
An aseprite-file importer for unity written in C#, built upon the experimental AssetImporter API
Stars: ✭ 177 (-41.39%)
Mutual labels:  unity, unity-editor
Resharper Unity
Unity support for both ReSharper and Rider
Stars: ✭ 953 (+215.56%)
Mutual labels:  unity, unity-editor
Awesome Unity Open Source On Github
A categorized collection of awesome Unity open source on GitHub (800+)
Stars: ✭ 1,124 (+272.19%)
Mutual labels:  unity, unity-editor
Unitynativescripting
Unity Scripting in C++
Stars: ✭ 844 (+179.47%)
Mutual labels:  unity, unity-editor
Projectbuilder
A tool for easy automating and customizing build process for Unity.
Stars: ✭ 80 (-73.51%)
Mutual labels:  unity, unity-editor
Tracerysharp
C#/Unity port of Tracery (heavily WIP)
Stars: ✭ 19 (-93.71%)
Mutual labels:  unity, unity-editor
Node Level Editor
Unity3d Editor tools to create rooms quickly.
Stars: ✭ 26 (-91.39%)
Mutual labels:  unity, unity-editor
Ma textureatlasser
Texture atlas creator for Unity
Stars: ✭ 116 (-61.59%)
Mutual labels:  unity, unity-editor
Unitypausemenu
This is an open source Unity pause menu created for the game New Horizons, and it's completely free because of how a pause menu is a core component of a game, while the unity asset store was lacking in such an asset (until this was released on the asset store).
Stars: ✭ 160 (-47.02%)
Mutual labels:  unity, unity-editor

SerializableDictionary

A serializable dictionary class for Unity.

Unity cannot serialize standard dictionaries. This means that they won't show or be edited in the inspector and they won't be instantiated at startup. A classic workaround is to store the keys and values in separate arrays and 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 from Dictionary<TKey, TValue>

  • It implements a CopyFrom(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 or null 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. A CustomPropertyDrawer has to be declared for each of these classes.
  • Multiple editing of scripts using SerializableDictionaries 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 using LayerMask as key. The LayerMask value is changed after the CustomPropertyDrawer execution.
  • Dictionaries of lists or arrays must use the 3 arguments SerializableDictionary<TKey, TValue, TValueStorage> dictionary class with the extra SerializableDictionary.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 a SerializableDictionary subclass
    [Serializable]
    public class StringStringDictionary : SerializableDictionary<string, string> {}
    
  • Use StringStringDictionary in your scripts as a normal IDictionary<string, string> type

Dictionary of lists example

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

  • Create a SerializableDictionary.Storage subclass to hold the list

    [Serializable]
    public class ColorListStorage : SerializableDictionary.Storage<List<Color>> {}
    
  • Create a SerializableDictionary subclass using the previous subclass

    [Serializable]
    public class StringColorListDictionary : SerializableDictionary<string, List<Color>, ColorListStorage> {}
    
  • Use StringColorListDictionary in your scripts as a normal IDictionary<string, List<Color>> type

Details

As Unity is unable to directly serialize generic types, create a derived class for each SerializedDictionary specialization you want.

[Serializable]
public class StringStringDictionary : SerializableDictionary<string, string> {}

[Serializable]
public class MyScriptColorDictionary : SerializableDictionary<MyScript, Color> {}

You can use your own serializable classes.

[Serializable]
public class MyClass
{
    public int i;
    public string str;
}

[Serializable]
public class StringMyClassDictionary : 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 type IDictionary<TKey, TValue> for better encapsulation.

public StringStringDictionary m_myDictionary1;

[SerializeField]
MyScriptColorDictionary m_myDictionary2;
public IDictionary<MyScript, Color> MyDictionary2
{
    get { return m_myDictionary2; }
    set { m_myDictionary2.CopyFrom (value); }
}

public StringMyClassDictionary m_myDictionary3;

The CopyFrom(value) method clears the m_myDictionary2 dictionary and adds to it each of content of the value dictionary, effectively copying value into m_myDictionary2.

SerializableDictionary has a copy constructor from IDictionary<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]
public class StringColorDictionary : SerializableDictionary<string, Color>
{
    public StringColorDictionary(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 a SerializableDictionary<TKey, TValue[]> or a SerializableDictionary<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 from SerializableDictionary.Storage<List<TValue>. This storage class will only contain a List<TValue> data field.

[Serializable]
public class ColorListStorage : 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 the Storage class because your dictionary will inherit from Dictionary<TKey, Storage<TValue>> instead of Dictionary<TKey, List<TValue>>. This is far from ideal.

// non optimal example for a dictionary of color list
[Serializable]
public class ColorListStorage : SerializableDictionary.Storage<List<Color>> {}
[Serializable]
public class StringColorListDictionary : SerializableDictionary<string, ColorListStorage> {}

public StringColorListDictionary m_colorStringListDict;

// you would have to access the color list through the .data field of ColorListStorage
List<Color> colorList = m_colorStringListDict[key].data;

To access the lists directly, use the special 3 arguments SerializableDictionary<TKey, TValue, TValueStorage> class where TValueStorage is the class previously created.

[Serializable]
public class ColorListStorage : SerializableDictionary.Storage<List<Color>> {}
[Serializable]
public class StringColorListDictionary : SerializableDictionary<string, List<Color>, ColorListStorage> {}

public StringColorListDictionary m_colorStringListDict;

// you can now access directly the color list
List<Color> colorList = m_colorStringListDict[key];
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].