Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- [Serializable]
- public class SerializableDictionary<K,V> : ScriptableObject where K : IEquatable<K>
- {
- private Dictionary<K, V> m_internalDictionary;
- public Dictionary<K, V> Dictionary
- {
- get { return m_internalDictionary; }
- }
- [SerializeField]
- private List<K> m_keys = new List<K>();
- public List<K> Keys
- {
- get{ return m_keys; }
- private set{ m_keys = value; }
- }
- [SerializeField]
- private List<V> m_values = new List<V>();
- public List<V> Values
- {
- get { return m_values; }
- private set { m_values = value; }
- }
- public void OnEnable()
- {
- if (m_internalDictionary == null)
- {
- m_internalDictionary = new Dictionary<K, V>();
- }
- if (m_keys == null)
- {
- m_keys = new List<K>();
- }
- if (m_values == null)
- {
- m_values = new List<V>();
- }
- for (int i = 0; i < m_keys.Count; i++)
- {
- m_internalDictionary.Add(m_keys[i], m_values[i]);
- }
- }
- public void Add(K key, V value)
- {
- if (!ContainsKey(key))
- {
- m_keys.Add(key);
- m_values.Add(value);
- m_internalDictionary.Add(key, value);
- }
- else
- {
- SetValue(key, value);
- }
- }
- public void SetValue(K key, V value)
- {
- int index = GetKeyIndex(key);
- m_values[index] = value;
- m_internalDictionary[key] = value;
- }
- public void Remove(K key)
- {
- m_values.Remove(GetValueFromKey(key));
- m_keys.Remove(key);
- m_internalDictionary.Remove(key);
- }
- public bool ContainsKey(K key)
- {
- return m_internalDictionary.ContainsKey(key);
- }
- public bool ContainsValue(V data)
- {
- return m_internalDictionary.ContainsValue(data);
- }
- private int GetKeyIndex(K key)
- {
- int index = 0;
- for (int i = 0; i < m_keys.Count; i++)
- {
- if (m_keys[i].Equals(key))
- {
- index = i;
- }
- }
- return index;
- }
- private V GetValueFromKey(K key)
- {
- int index = 0;
- for (int i = 0; i < m_keys.Count; i++)
- {
- if (m_keys[i].Equals(key))
- {
- index = i;
- }
- }
- return m_values[index];
- }
- public V this[K key]
- {
- get
- {
- return m_internalDictionary[key];
- }
- set
- {
- SetValue(key, value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement