Skip to content

Repository files navigation

SerializableDictionary

GitHub Actions Releases openupm

README | 中文文档

Serializable Dictionary, HashSet and KeyValuePair-style types with reorderable Unity Inspector controls.

This project was developed from azixMcAze's Unity-SerializableDictionary. See Third-Party Notices.

Serializable Dictionary Inspector

Serializable HashSet Inspector

Requirements

  • Unity 2022.3 or newer.
  • Key and value types must follow Unity's serialization rules.

The package is tested with Unity 2022.3 LTS and Unity 6.

Installation

OpenUPM

Run this command in the Unity project directory:

openupm add com.strodio.serializable-dictionary

See the OpenUPM package page for scoped-registry installation.

Git URL

In Package Manager, select Add package from git URL and enter:

https://github.com/StromKuo/SerializableDictionary.git

For reproducible builds, append a release tag such as #v0.2.0.

Usage

using System;
using SKUnityToolkit.SerializableDictionary;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    [SerializeField]
    SerializableDictionary<string, int> itemCounts = new SerializableDictionary<string, int>();

    [SerializeField]
    SerializableHashSet<string> unlockedItems = new SerializableHashSet<string>();

    [SerializeField]
    SerializableKeyValuePair<string, Color> categoryColor =
        new SerializableKeyValuePair<string, Color>("Default", Color.white);
}

The types derive from the standard collection implementations, so normal dictionary and set APIs are available at runtime.

Nested collections

Unity does not directly serialize nested containers. Wrap the inner collection in a serializable class:

using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class ColorList
{
    public List<Color> values = new List<Color>();
}

[SerializeField]
SerializableDictionary<string, ColorList> colorsByCategory =
    new SerializableDictionary<string, ColorList>();

Conflict behavior and limitations

  • The first entry for a duplicate dictionary key is retained; later duplicates are discarded.
  • Null and destroyed UnityEngine.Object keys are discarded.
  • DeserializationConflictCount reports how many dictionary or HashSet entries were discarded by the latest deserialization.
  • Custom equality comparers affect only the current in-memory instance and are not serialized by Unity.
  • Serialization order follows collection enumeration order and must not be used as application logic.

See the full documentation, changelog, and the Basic Usage sample available from Package Manager.

License

MIT

About

A Unity package of serializable Dictionary, HashSet and KeyValuePair.

Resources

Stars

17 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages