forked from Shardstone/trail-into-darkness
added serialzed dictionary
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("AYellowpaper.SerializedCollections.Editor")]
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88ab3f65a23ff3a4697758e7a3ed3ac0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
public static class SerializedCollectionsUtility
|
||||
{
|
||||
public static bool IsValidKey(object obj)
|
||||
{
|
||||
// we catch this error if we are not on the main thread and simply return false as we assume the object is null
|
||||
try
|
||||
{
|
||||
return !(obj is Object unityObject && unityObject == null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1da1a15cc1e6754fb099902045e4572
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[System.Serializable]
|
||||
public partial class SerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
|
||||
{
|
||||
[SerializeField]
|
||||
internal List<SerializedKeyValuePair<TKey, TValue>> _serializedList = new List<SerializedKeyValuePair<TKey, TValue>>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
internal IKeyable LookupTable
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lookupTable == null)
|
||||
_lookupTable = new DictionaryLookupTable<TKey, TValue>(this);
|
||||
return _lookupTable;
|
||||
}
|
||||
}
|
||||
|
||||
private DictionaryLookupTable<TKey, TValue> _lookupTable;
|
||||
#endif
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
foreach (var kvp in _serializedList)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!ContainsKey(kvp.Key))
|
||||
Add(kvp.Key, kvp.Value);
|
||||
#else
|
||||
Add(kvp.Key, kvp.Value);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
LookupTable.RecalculateOccurences();
|
||||
#else
|
||||
_serializedList.Clear();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (UnityEditor.BuildPipeline.isBuildingPlayer)
|
||||
LookupTable.RemoveDuplicates();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfd467abf43f5944faa7fd0abcac2299
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[Conditional("UNITY_EDITOR")]
|
||||
public class SerializedDictionaryAttribute : Attribute
|
||||
{
|
||||
public readonly string KeyName;
|
||||
public readonly string ValueName;
|
||||
|
||||
public SerializedDictionaryAttribute(string keyName = null, string valueName = null)
|
||||
{
|
||||
KeyName = keyName;
|
||||
ValueName = valueName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3aa354f515fef24c865758f92a293ec
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct SerializedKeyValuePair<TKey, TValue>
|
||||
{
|
||||
public TKey Key;
|
||||
public TValue Value;
|
||||
|
||||
public SerializedKeyValuePair(TKey key, TValue value)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f808808de12544418e1f67eb1e8a78d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user