{"id":13661160,"url":"https://github.com/azixMcAze/Unity-SerializableDictionary","last_synced_at":"2025-04-24T23:31:54.339Z","repository":{"id":39423092,"uuid":"91009753","full_name":"azixMcAze/Unity-SerializableDictionary","owner":"azixMcAze","description":"Serializable dictionary class for Unity","archived":false,"fork":false,"pushed_at":"2021-04-07T11:57:18.000Z","size":408,"stargazers_count":753,"open_issues_count":20,"forks_count":94,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-04T15:07:35.506Z","etag":null,"topics":["property-drawers","unity","unity-editor"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azixMcAze.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-11T18:10:10.000Z","updated_at":"2025-03-25T07:34:10.000Z","dependencies_parsed_at":"2022-07-10T01:46:40.705Z","dependency_job_id":null,"html_url":"https://github.com/azixMcAze/Unity-SerializableDictionary","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azixMcAze%2FUnity-SerializableDictionary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azixMcAze%2FUnity-SerializableDictionary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azixMcAze%2FUnity-SerializableDictionary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azixMcAze%2FUnity-SerializableDictionary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azixMcAze","download_url":"https://codeload.github.com/azixMcAze/Unity-SerializableDictionary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250727704,"owners_count":21477353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["property-drawers","unity","unity-editor"],"created_at":"2024-08-02T05:01:30.473Z","updated_at":"2025-04-24T23:31:49.321Z","avatar_url":"https://github.com/azixMcAze.png","language":"C#","funding_links":[],"categories":["C\\#","C#"],"sub_categories":[],"readme":"# SerializableDictionary\nA serializable dictionary class for Unity.\n\nUnity cannot serialize standard dictionaries. This means that they won't show or be edited in the inspector\nand they won't be instantiated at startup. A classic workaround is to store the keys and values in separate arrays\nand construct the dictionary at startup.\n\nThis project provides a generic dictionary class and its custom property drawer that solves this problem.\n\n![General screenshot](docs/SerializableDictionary_screenshot1.png)\n\n## Features\n\n- It inherits from `Dictionary\u003cTKey, TValue\u003e`\n- It implements a `CopyFrom(IDictionary\u003cTKey, TValue\u003e)` method to help assign values from regular dictionaries\n- You can use any serializable type by unity as key or value. \n- It can be edited in the inspector without having to implement custom editors or property drawers.\n- 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.\n    \n    ![Conflicting keys screenshot](docs/SerializableDictionary_screenshot2.png)\n\n    ![Null key screenshot](docs/SerializableDictionary_screenshot3.png)\n\n\n## Limitations\n- A non-generic derived class has to be created for each `\u003cTKey, TValue\u003e` combination you want to use. A `CustomPropertyDrawer` has to be declared for each of these classes.\n- 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.\n- The conflicting key detection does not work when using `LayerMask` as key. The `LayerMask` value is changed after the `CustomPropertyDrawer` execution.\n- Dictionaries of lists or arrays must use the 3 arguments `SerializableDictionary\u003cTKey, TValue, TValueStorage\u003e` dictionary class with the extra `SerializableDictionary.Storage\u003cTValue\u003e` class to hold the values. See the \"Dictionary of lists or arrays\" section for details.\n\n\n## Usage\n\n### Simple dictionary example \n\nTo create a serializable dictionary of type `\u003cstring, string\u003e`:\n-  Create a `SerializableDictionary` subclass\n    ```csharp\n    [Serializable]\n    public class StringStringDictionary : SerializableDictionary\u003cstring, string\u003e {}\n    ```\n- Use `StringStringDictionary` in your scripts as a normal `IDictionary\u003cstring, string\u003e` type\n\n\n### Dictionary of lists example \n\nTo create a serializable dictionary of type `\u003cstring, List\u003cColor\u003e\u003e`:\n- Create a `SerializableDictionary.Storage` subclass to hold the list\n    ```csharp\n    [Serializable]\n    public class ColorListStorage : SerializableDictionary.Storage\u003cList\u003cColor\u003e\u003e {}\n    ```\n\n-  Create a `SerializableDictionary` subclass using the previous subclass\n    ```csharp\n    [Serializable]\n    public class StringColorListDictionary : SerializableDictionary\u003cstring, List\u003cColor\u003e, ColorListStorage\u003e {}\n    ```\n- Use `StringColorListDictionary` in your scripts as a normal `IDictionary\u003cstring, List\u003cColor\u003e\u003e` type\n\n\n## Details\n\nOlder versions of Unity (before 2020.1) are unable to directly serialize generic types. Therefore, you need to create a derived class for each `SerializedDictionary` specialization you want.\n```csharp\n[Serializable]\npublic class StringStringDictionary : SerializableDictionary\u003cstring, string\u003e {}\n\n[Serializable]\npublic class MyScriptColorDictionary : SerializableDictionary\u003cMyScript, Color\u003e {}\n```\n\nYou can use your own serializable classes.\n```csharp\n[Serializable]\npublic class MyClass\n{\n    public int i;\n    public string str;\n}\n\n[Serializable]\npublic class StringMyClassDictionary : SerializableDictionary\u003cstring, MyClass\u003e {}\n```\n\n\nAdd the dictionaries to your scripts and access them directly of through a property.\nThe dictionaries can be accessed through a property of type `IDictionary\u003cTKey, TValue\u003e` for better encapsulation.\n\n```csharp\npublic StringStringDictionary m_myDictionary1;\n\n[SerializeField]\nMyScriptColorDictionary m_myDictionary2;\npublic IDictionary\u003cMyScript, Color\u003e MyDictionary2\n{\n    get { return m_myDictionary2; }\n    set { m_myDictionary2.CopyFrom (value); }\n}\n\npublic StringMyClassDictionary m_myDictionary3;\n```\n\nThe `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`.\n\n`SerializableDictionary` has a copy constructor from `IDictionary\u003cTKey, TValue\u003e`. 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.\n\n```csharp\n[Serializable]\npublic class StringColorDictionary : SerializableDictionary\u003cstring, Color\u003e\n{\n    public StringColorDictionary(IDictionary\u003cstring, Color\u003e dict) : base(dict) {}\n}\n```\n\n### Dictionary of lists or arrays\n\nBecause unity cannot serialize a array of lists or an array of arrays, using a `SerializableDictionary\u003cTKey, TValue[]\u003e` or a `SerializableDictionary\u003cTKey, List\u003cTValue\u003e\u003e` in a script will not work properly. The dictionary will not show up in the inspector and the values will not be saved.\n\nIt 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.\n\nCreate a class that inherits from `SerializableDictionary.Storage\u003cList\u003cTValue\u003e`. This storage class will only contain a `List\u003cTValue\u003e data` field.\n\n```csharp\n[Serializable]\npublic class ColorListStorage : SerializableDictionary.Storage\u003cList\u003cColor\u003e\u003e {}\n```\n\nIf 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\u003cTKey, Storage\u003cTValue\u003e\u003e` instead of `Dictionary\u003cTKey, List\u003cTValue\u003e\u003e`. This is far from ideal.\n\n```csharp\n// non optimal example for a dictionary of color list\n[Serializable]\npublic class ColorListStorage : SerializableDictionary.Storage\u003cList\u003cColor\u003e\u003e {}\n[Serializable]\npublic class StringColorListDictionary : SerializableDictionary\u003cstring, ColorListStorage\u003e {}\n\npublic StringColorListDictionary m_colorStringListDict;\n\n// you would have to access the color list through the .data field of ColorListStorage\nList\u003cColor\u003e colorList = m_colorStringListDict[key].data;\n```\n\nTo access the lists directly, use the special 3 arguments `SerializableDictionary\u003cTKey, TValue, TValueStorage\u003e` class where `TValueStorage` is the class previously created.\n\n\n```csharp\n[Serializable]\npublic class ColorListStorage : SerializableDictionary.Storage\u003cList\u003cColor\u003e\u003e {}\n[Serializable]\npublic class StringColorListDictionary : SerializableDictionary\u003cstring, List\u003cColor\u003e, ColorListStorage\u003e {}\n\npublic StringColorListDictionary m_colorStringListDict;\n\n// you can now access directly the color list\nList\u003cColor\u003e colorList = m_colorStringListDict[key];\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FazixMcAze%2FUnity-SerializableDictionary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FazixMcAze%2FUnity-SerializableDictionary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FazixMcAze%2FUnity-SerializableDictionary/lists"}