Skip to content

Instantly share code, notes, and snippets.

@LuviKunG
Last active August 3, 2023 18:13
Show Gist options
  • Save LuviKunG/f2d90fb8a30fca5a4d39febb82c7a291 to your computer and use it in GitHub Desktop.
Save LuviKunG/f2d90fb8a30fca5a4d39febb82c7a291 to your computer and use it in GitHub Desktop.
Serializable Dictionary for Unity (by LuviKunG)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Serializable Dictionary for Unity (by LuviKunG)
// https://gist.github.com/LuviKunG
namespace LuviKunG.Serializables
{
[Serializable]
public sealed class SerializedDictionary<T1, T2> : IEnumerable<SerializedDictionaryElement<T1, T2>>
{
[SerializeField]
private List<SerializedDictionaryElement<T1, T2>> m_list;
public SerializedDictionary()
{
m_list = new List<SerializedDictionaryElement<T1, T2>>();
}
public SerializedDictionary(IEnumerable<SerializedDictionaryElement<T1, T2>> list)
{
m_list = new List<SerializedDictionaryElement<T1, T2>>(list);
}
public SerializedDictionaryElement<T1, T2> this[int index]
{
get
{
return m_list[index];
}
set
{
m_list[index] = value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)m_list).GetEnumerator();
}
public IEnumerator<SerializedDictionaryElement<T1, T2>> GetEnumerator()
{
return ((IEnumerable<SerializedDictionaryElement<T1, T2>>)m_list).GetEnumerator();
}
public int Count => m_list.Count;
public int IndexOfKey(T1 key)
{
for (int i = 0; i < m_list.Count; _ = ++i)
if (m_list[i].Key.Equals(key))
return i;
return -1;
}
public int IndexOfValue(T2 value)
{
for (int i = 0; i < m_list.Count; _ = ++i)
if (m_list[i].Value.Equals(value))
return i;
return -1;
}
public void Add(T1 key, T2 value)
{
Add(new SerializedDictionaryElement<T1, T2>(key, value));
}
public void Add(SerializedDictionaryElement<T1, T2> element)
{
int index = IndexOfKey(element.Key);
if (index < 0)
m_list.Add(element);
else
m_list[index] = element;
}
public void RemoveAt(int index)
{
m_list.RemoveAt(index);
}
public bool RemoveKey(T1 key)
{
int index = IndexOfKey(key);
if (index < 0)
return false;
else
{
m_list.RemoveAt(index);
return true;
}
}
public bool RemoveValue(T2 value)
{
int index = IndexOfValue(value);
if (index < 0)
return false;
else
{
m_list.RemoveAt(index);
return true;
}
}
public bool ContainsKey(T1 key)
{
return IndexOfKey(key) >= 0;
}
public void Clear()
{
m_list.Clear();
}
public void TrimExcess()
{
m_list.TrimExcess();
}
public void Sort()
{
m_list.Sort();
}
public void Sort(Comparison<SerializedDictionaryElement<T1, T2>> comparison)
{
m_list.Sort(comparison);
}
public void SortKey(Comparison<T1> comparison)
{
m_list.Sort((lhs, rhs) => comparison(lhs.Key, rhs.Key));
}
public void SortValue(Comparison<T2> comparison)
{
m_list.Sort((lhs, rhs) => comparison(lhs.Value, rhs.Value));
}
}
}
using System;
// Serializable Dictionary for Unity (by LuviKunG)
// https://gist.github.com/LuviKunG
namespace LuviKunG.Serializables
{
[Serializable]
public struct SerializedDictionaryElement<T1, T2>
{
public T1 Key;
public T2 Value;
public SerializedDictionaryElement(T1 key, T2 value)
{
Key = key;
Value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment