From e846c64d6f927879cb8a095e62d773a8d7b3c9f4 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 15 Oct 2020 07:24:10 +0800 Subject: *ability system --- .../Plugins/AdvancedInspector/Core/UDictionary.cs | 230 +++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 Assets/Plugins/AdvancedInspector/Core/UDictionary.cs (limited to 'Assets/Plugins/AdvancedInspector/Core/UDictionary.cs') diff --git a/Assets/Plugins/AdvancedInspector/Core/UDictionary.cs b/Assets/Plugins/AdvancedInspector/Core/UDictionary.cs new file mode 100644 index 00000000..5322154f --- /dev/null +++ b/Assets/Plugins/AdvancedInspector/Core/UDictionary.cs @@ -0,0 +1,230 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; +using System.Text; + +using UnityEngine; + +namespace AdvancedInspector +{ + [Serializable] + [ComVisible(false)] + [DebuggerDisplay("Count = {Count}")] + public class UDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ISerializationCallbackReceiver + { + [SerializeField] + private List keys = new List(); + + [SerializeField] + private List values = new List(); + + [NonSerialized] + private Dictionary dictionary = new Dictionary(); + + #region Implementation of ISerializationCallbackReceiver + public void OnAfterDeserialize() + { + dictionary.Clear(); + for (int i = 0; i < keys.Count; i++) + if (keys[i] != null && (!(keys[i] is UnityEngine.Object) || ((UnityEngine.Object)(object)keys[i]))) + dictionary.Add(keys[i], values[i]); + } + + public void OnBeforeSerialize() + { + keys.Clear(); + values.Clear(); + foreach (KeyValuePair pair in dictionary) + { + if (pair.Key == null || (pair.Key is UnityEngine.Object && !((UnityEngine.Object)(object)pair.Key))) + continue; + + keys.Add(pair.Key); + values.Add(pair.Value); + } + } + #endregion + + #region Implementation of ISerializable + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + dictionary.GetObjectData(info, context); + } + #endregion + + #region Implementation of IDeserializationCallback + public void OnDeserialization(object sender) + { + dictionary.OnDeserialization(sender); + } + #endregion + + #region Implementation IDictionary + public bool IsFixedSize + { + get { return false; } + } + + public ICollection Keys + { + get { return dictionary.Keys; } + } + + ICollection IDictionary.Keys + { + get { return dictionary.Keys; } + } + + public ICollection Values + { + get { return dictionary.Values; } + } + + ICollection IDictionary.Values + { + get { return dictionary.Values; } + } + + public TValue this[TKey key] + { + get { return dictionary[key]; } + set { dictionary[key] = value; } + } + + object IDictionary.this[object key] + { + get + { + if (!(key is TKey)) + return null; + + return dictionary[(TKey)key]; + } + set + { + if (!(key is TKey)) + return; + + if (!(value is TValue) && value != null) + return; + + dictionary[(TKey)key] = (TValue)value; + } + } + + public void Add(TKey key, TValue value) + { + dictionary.Add(key, value); + } + + void IDictionary.Add(object key, object value) + { + if (!(key is TKey)) + return; + + if (!(value is TValue) && value != null) + return; + + dictionary.Add((TKey)key, (TValue)value); + } + + public bool ContainsKey(TKey key) + { + return dictionary.ContainsKey(key); + } + + bool IDictionary.Contains(object key) + { + if (!(key is TKey)) + return false; + + return dictionary.ContainsKey((TKey)key); + } + + public bool Remove(TKey key) + { + return dictionary.Remove(key); + } + + void IDictionary.Remove(object key) + { + if (!(key is TKey)) + return; + + dictionary.Remove((TKey)key); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return dictionary.TryGetValue(key, out value); + } + + IDictionaryEnumerator IDictionary.GetEnumerator() + { + return ((IDictionary)dictionary).GetEnumerator(); + } + #endregion + + #region Implementation ICollection + public int Count + { + get { return dictionary.Count; } + } + + public bool IsReadOnly + { + get { return false; } + } + + public bool IsSynchronized + { + get { return false; } + } + + public object SyncRoot + { + get { return null; } + } + + public void Add(KeyValuePair item) + { + dictionary.Add(item.Key, item.Value); + } + + public void Clear() + { + dictionary.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return dictionary.ContainsKey(item.Key) && dictionary[item.Key].Equals(item.Value); + } + + void ICollection.CopyTo(Array array, int index) { } + + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { } + + public bool Remove(KeyValuePair item) + { + return dictionary.Remove(item.Key); + } + #endregion + + #region Implementation of IEnumerable + public IEnumerator> GetEnumerator() + { + return dictionary.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return dictionary.GetEnumerator(); + } + #endregion + } +} \ No newline at end of file -- cgit v1.1-26-g67d0