mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2025-11-14 00:05:56 +08:00
remove recovery reference utils
This commit is contained in:
parent
62d71d2a21
commit
a5277126e7
@ -30,7 +30,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
|
||||
{
|
||||
protected override void OnInit()
|
||||
{
|
||||
Type referenceBaseType = typeof(Reference<>);
|
||||
Type fieldType = fieldInfo.FieldType;
|
||||
if (fieldType.IsGenericType)
|
||||
{
|
||||
@ -38,25 +37,12 @@ namespace DCFApixels.DragonECS.Unity.Editors
|
||||
{
|
||||
fieldType = fieldType.GetGenericTypeDefinition();
|
||||
}
|
||||
|
||||
if (fieldType == referenceBaseType)
|
||||
{
|
||||
_isReferenceWrapper = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_isReferenceWrapper = false;
|
||||
}
|
||||
|
||||
private bool _isReferenceWrapper = false;
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
Init();
|
||||
if (_isReferenceWrapper)
|
||||
{
|
||||
property.Next(true);
|
||||
}
|
||||
if (property.managedReferenceValue != null)
|
||||
{
|
||||
return EditorGUI.GetPropertyHeight(property, label, true);
|
||||
@ -69,12 +55,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
|
||||
|
||||
protected override void DrawCustom(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (_isReferenceWrapper)
|
||||
{
|
||||
property.Next(true);
|
||||
label.text = property.displayName;
|
||||
}
|
||||
|
||||
if (IsArrayElement)
|
||||
{
|
||||
label = UnityEditorUtility.GetLabelTemp();
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a95e563d250e80489b71ecdd0392cf0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -1,309 +0,0 @@
|
||||
#if UNITY_EDITOR
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DCFApixels.DragonECS.Unity.Internal
|
||||
{
|
||||
[Serializable]
|
||||
internal class UnityEditorCacheBlock
|
||||
{
|
||||
[SerializeField]
|
||||
private int _id;
|
||||
|
||||
[SerializeField]
|
||||
private int _startIndex;
|
||||
|
||||
[SerializeField]
|
||||
private string[] _jsons = new string[256];
|
||||
[SerializeField]
|
||||
private byte _jsonsCount;
|
||||
|
||||
[SerializeField]
|
||||
private byte[] _recycledID = new byte[256];
|
||||
[SerializeField]
|
||||
private byte _recycledIDCount = 0;
|
||||
|
||||
[SerializeField]
|
||||
private int _fullLength = 0;
|
||||
|
||||
private UnityEditorCacheBlock() { }
|
||||
public UnityEditorCacheBlock(int id)
|
||||
{
|
||||
_id = id;
|
||||
}
|
||||
|
||||
public int StartIndex
|
||||
{
|
||||
get { return _startIndex; }
|
||||
}
|
||||
public int Count
|
||||
{
|
||||
get { return _jsonsCount; }
|
||||
}
|
||||
public int FullLength
|
||||
{
|
||||
get { return _fullLength; }
|
||||
}
|
||||
|
||||
public void Set(string json, ref int id)
|
||||
{
|
||||
const int MASK = ~(byte.MaxValue);
|
||||
byte slotID = (byte)id;
|
||||
if (slotID == 0)
|
||||
{
|
||||
if (_recycledIDCount > 0)
|
||||
{
|
||||
id = _recycledID[_recycledIDCount--];
|
||||
}
|
||||
else
|
||||
{
|
||||
id = _jsonsCount++;
|
||||
}
|
||||
id = (id & MASK) | slotID;
|
||||
}
|
||||
slotID--;
|
||||
|
||||
string oldJson = _jsons[slotID];
|
||||
if (oldJson != null)
|
||||
{
|
||||
_fullLength -= oldJson.Length;
|
||||
}
|
||||
_fullLength += json.Length;
|
||||
_jsons[slotID] = json;
|
||||
}
|
||||
public string Get(int id)
|
||||
{
|
||||
byte slotID = (byte)id;
|
||||
_recycledID[_recycledIDCount++] = slotID;
|
||||
return _jsons[slotID];
|
||||
}
|
||||
|
||||
public bool IsValide()
|
||||
{
|
||||
return
|
||||
_jsons != null &&
|
||||
_recycledID != null &&
|
||||
_jsons.Length == 256 &&
|
||||
_recycledID.Length == 256;
|
||||
}
|
||||
public void Reset()
|
||||
{
|
||||
_jsons = new string[256];
|
||||
_recycledID = new byte[256];
|
||||
_jsonsCount = 0;
|
||||
_recycledIDCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[FilePath(_CACHE_BLOCKS_FOLDER + ".prefs", FilePathAttribute.Location.ProjectFolder)]
|
||||
public class UnityEditorCache : ScriptableSingleton<UnityEditorCache>, ISerializationCallbackReceiver
|
||||
{
|
||||
private const string _CACHE_BLOCKS_FOLDER = EcsUnityConsts.LOCAL_CACHE_FOLDER + "/" + nameof(UnityEditorCache);
|
||||
private const int _THRESHOLD_LENGTH = 2048;
|
||||
|
||||
private static bool _isInit;
|
||||
private static string _projectPath;
|
||||
private static string _cachePath;
|
||||
|
||||
private static void Init()
|
||||
{
|
||||
if (_isInit) { return; }
|
||||
_projectPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'));
|
||||
_cachePath = _projectPath + _CACHE_BLOCKS_FOLDER;
|
||||
_isInit = true;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private string[] _blockNames = new string[512];
|
||||
[SerializeField]
|
||||
private int _blockNamesCount = 0;
|
||||
|
||||
[SerializeField]
|
||||
private int[] _recycledID = new int[512];
|
||||
[SerializeField]
|
||||
private int _recycledIDCount = 0;
|
||||
|
||||
public void Set<T>(T data, ref int id)
|
||||
{
|
||||
Init();
|
||||
//try
|
||||
{
|
||||
int blockID;
|
||||
if (id == -1)
|
||||
{
|
||||
if (_recycledIDCount > 0)
|
||||
{
|
||||
blockID = _recycledID[_recycledIDCount--];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_blockNamesCount >= _blockNames.Length)
|
||||
{
|
||||
Array.Resize(ref _blockNames, _blockNamesCount << 1);
|
||||
}
|
||||
blockID = _blockNamesCount++;
|
||||
}
|
||||
id = blockID << 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
blockID = (id >> 8) & 16_777_215;
|
||||
if (blockID >= _blockNames.Length)
|
||||
{
|
||||
Array.Resize(ref _blockNames, blockID << 1);
|
||||
}
|
||||
}
|
||||
|
||||
ref string blockName = ref _blockNames[blockID];
|
||||
string filePath;
|
||||
if (string.IsNullOrEmpty(blockName))
|
||||
{
|
||||
blockName = blockID.ToString();
|
||||
|
||||
filePath = Path.Combine(_cachePath, blockName);
|
||||
string directoryName = _cachePath;
|
||||
if (!Directory.Exists(directoryName))
|
||||
{
|
||||
Directory.CreateDirectory(directoryName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
filePath = Path.Combine(_cachePath, blockName);
|
||||
}
|
||||
|
||||
var json = JsonUtility.ToJson(data);
|
||||
UnityEditorCacheBlock block;
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
block = JsonUtility.FromJson<UnityEditorCacheBlock>(File.ReadAllText(filePath));
|
||||
}
|
||||
else
|
||||
{
|
||||
block = new UnityEditorCacheBlock(blockID);
|
||||
}
|
||||
block.Set(json, ref id);
|
||||
File.WriteAllText(filePath, JsonUtility.ToJson(block));
|
||||
|
||||
if (block.FullLength < _THRESHOLD_LENGTH)
|
||||
{
|
||||
if (_recycledIDCount >= _recycledID.Length)
|
||||
{
|
||||
Array.Resize(ref _recycledID, _recycledIDCount == 0 ? 64 : _recycledIDCount << 1);
|
||||
}
|
||||
_recycledID[_recycledIDCount++] = blockID;
|
||||
}
|
||||
Save(true);
|
||||
}
|
||||
//catch (Exception e)
|
||||
//{
|
||||
// Reset();
|
||||
// throw e;
|
||||
//}
|
||||
}
|
||||
public T Get<T>(ref int id)
|
||||
{
|
||||
Init();
|
||||
//try
|
||||
{
|
||||
int blockID = (id >> 8) & 16_777_215;
|
||||
id = -1;
|
||||
|
||||
ref string blockName = ref _blockNames[blockID];
|
||||
string filePath;
|
||||
if (string.IsNullOrEmpty(blockName))
|
||||
{
|
||||
blockName = blockID.ToString();
|
||||
|
||||
filePath = Path.Combine(_cachePath, blockName);
|
||||
string directoryName = _cachePath;
|
||||
if (!Directory.Exists(directoryName))
|
||||
{
|
||||
Directory.CreateDirectory(directoryName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
filePath = Path.Combine(_cachePath, blockName);
|
||||
}
|
||||
|
||||
UnityEditorCacheBlock block;
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
block = JsonUtility.FromJson<UnityEditorCacheBlock>(File.ReadAllText(filePath));
|
||||
}
|
||||
else
|
||||
{
|
||||
block = new UnityEditorCacheBlock(blockID);
|
||||
}
|
||||
|
||||
string json = block.Get(id);
|
||||
|
||||
|
||||
|
||||
if (block.FullLength < _THRESHOLD_LENGTH)
|
||||
{
|
||||
if (_recycledIDCount >= _recycledID.Length)
|
||||
{
|
||||
Array.Resize(ref _recycledID, _recycledIDCount == 0 ? 64 : _recycledIDCount << 1);
|
||||
}
|
||||
_recycledID[_recycledIDCount++] = blockID;
|
||||
}
|
||||
|
||||
|
||||
Save(true);
|
||||
return json == null ? default : JsonUtility.FromJson<T>(json);
|
||||
}
|
||||
//catch (Exception e)
|
||||
//{
|
||||
// Reset();
|
||||
// throw e;
|
||||
//}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Save(true);
|
||||
EcsDebug.PrintPass("Save Save Save");
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
_blockNames = new string[512];
|
||||
_blockNamesCount = 0;
|
||||
_recycledID = new int[512];
|
||||
_recycledIDCount = 0;
|
||||
Save(true);
|
||||
}
|
||||
|
||||
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
||||
{
|
||||
}
|
||||
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#region TODO
|
||||
//public int SetUnityObject(UnityObject obj)
|
||||
//{
|
||||
// if (_jsonDatasCount >= _jsonDatas.Length)
|
||||
// {
|
||||
// Array.Resize(ref _jsonDatas, _jsonDatas.Length << 1);
|
||||
// }
|
||||
//
|
||||
// _jsonDatas[_jsonDatasCount] = JsonUtility.ToJson(obj);
|
||||
// return _jsonDatasCount++;
|
||||
//}
|
||||
//public void GetOverwriteUnityObject(UnityObject obj, int id)
|
||||
//{
|
||||
// JsonUtility.FromJsonOverwrite(_jsonDatas[id], obj);
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 839f12827a9b59d4ea2cb945a98f3e6e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -2,7 +2,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
@ -147,106 +146,5 @@ namespace DCFApixels.DragonECS
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[SerializeField]
|
||||
//[HideInInspector]
|
||||
private int _saveID;
|
||||
|
||||
[Serializable]
|
||||
private struct SavedRecordList
|
||||
{
|
||||
public SavedRecord[] records;
|
||||
public SavedRecordList(SavedRecord[] records)
|
||||
{
|
||||
this.records = records;
|
||||
}
|
||||
}
|
||||
[Serializable]
|
||||
private struct SavedRecord
|
||||
{
|
||||
public int index;
|
||||
public string metaID;
|
||||
public string recordJson;
|
||||
public SavedRecord(int index, string metaID, string recordJson)
|
||||
{
|
||||
this.index = index;
|
||||
this.metaID = metaID;
|
||||
this.recordJson = recordJson;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Load();
|
||||
}
|
||||
|
||||
private void Load()
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
if (_saveID != -1)
|
||||
{
|
||||
bool isChanged = false;
|
||||
var savedRecords = UnityEditorCache.instance.Get<SavedRecordList>(ref _saveID).records;
|
||||
if (savedRecords != null)
|
||||
{
|
||||
for (int i = 0; i < savedRecords.Length; i++)
|
||||
{
|
||||
ref var savedRecord = ref savedRecords[i];
|
||||
if (savedRecord.index < _records.Length)
|
||||
{
|
||||
ref var record = ref _records[savedRecord.index];
|
||||
if (record.target == null && string.IsNullOrEmpty(savedRecord.metaID) == false)
|
||||
{
|
||||
record.target = RecoveryReferenceUtility.TryRecoverReference<object>(savedRecord.metaID);
|
||||
if (record.target == null) { return; }
|
||||
JsonUtility.FromJsonOverwrite(savedRecord.recordJson, record.target);
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isChanged)
|
||||
{
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
private void Save()
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
if (_saveID == -1 || EditorApplication.isPlaying == false)
|
||||
{
|
||||
SavedRecord[] savedRecords = new SavedRecord[_records.Length];
|
||||
SavedRecordList list = new SavedRecordList(savedRecords);
|
||||
for (int i = 0; i < _records.Length; i++)
|
||||
{
|
||||
ref var record = ref _records[i];
|
||||
string metaid = record.target.GetMeta().MetaID;
|
||||
if (string.IsNullOrEmpty(metaid) == false)
|
||||
{
|
||||
savedRecords[i] = new SavedRecord(i, metaid, JsonUtility.ToJson(record.target));
|
||||
}
|
||||
else
|
||||
{
|
||||
savedRecords[i] = default;
|
||||
}
|
||||
}
|
||||
UnityEditorCache.instance.Set(list, ref _saveID);
|
||||
//RecoveryReferencesCache.instance.Save();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
private void OnValidate()
|
||||
{
|
||||
Save();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@ -1,121 +1,13 @@
|
||||
using DCFApixels.DragonECS.Unity;
|
||||
using DCFApixels.DragonECS.Unity.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public abstract class MonoEntityTemplateBase : MonoBehaviour, ITemplate
|
||||
{
|
||||
[SerializeField]
|
||||
private int _saveID;
|
||||
public abstract void Apply(short worldID, int entityID);
|
||||
|
||||
private static IComponentTemplate _fake = null;
|
||||
protected virtual IList<IComponentTemplate> GetToRecover() { return null; }
|
||||
protected virtual ref IComponentTemplate GetToRecoverSingle() { return ref _fake; }
|
||||
|
||||
[Serializable]
|
||||
private struct SavedRecordList
|
||||
{
|
||||
public SavedRecord[] records;
|
||||
public SavedRecord singleRecord;
|
||||
public SavedRecordList(SavedRecord[] templates)
|
||||
{
|
||||
this.records = templates;
|
||||
singleRecord = default;
|
||||
}
|
||||
}
|
||||
[Serializable]
|
||||
private struct SavedRecord
|
||||
{
|
||||
public int index;
|
||||
public string metaID;
|
||||
public string recordJson;
|
||||
public SavedRecord(int index, string metaID, string recordJson)
|
||||
{
|
||||
this.index = index;
|
||||
this.metaID = metaID;
|
||||
this.recordJson = recordJson;
|
||||
}
|
||||
}
|
||||
protected void Save()
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
if (_saveID == -1 || EditorApplication.isPlaying == false)
|
||||
{
|
||||
var records = GetToRecover();
|
||||
var recordSingle = GetToRecoverSingle();
|
||||
|
||||
SavedRecord[] savedRecords = new SavedRecord[records.Count];
|
||||
SavedRecordList list = new SavedRecordList(savedRecords);
|
||||
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
var record = records[i];
|
||||
string metaid = record.GetMeta().MetaID;
|
||||
if (string.IsNullOrEmpty(metaid) == false)
|
||||
{
|
||||
savedRecords[i] = new SavedRecord(i, metaid, JsonUtility.ToJson(record));
|
||||
}
|
||||
else
|
||||
{
|
||||
savedRecords[i] = default;
|
||||
}
|
||||
}
|
||||
if (recordSingle != null)
|
||||
{
|
||||
string metaid = recordSingle.GetMeta().MetaID;
|
||||
list.singleRecord = new SavedRecord(-1, metaid, JsonUtility.ToJson(recordSingle));
|
||||
}
|
||||
UnityEditorCache.instance.Set(list, ref _saveID);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
protected void Load()
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
if (_saveID != -1)
|
||||
{
|
||||
bool isChanged = false;
|
||||
SavedRecordList list = UnityEditorCache.instance.Get<SavedRecordList>(ref _saveID);
|
||||
var savedRecords = list.records;
|
||||
if (savedRecords != null)
|
||||
{
|
||||
var records = GetToRecover();
|
||||
for (int i = 0; i < savedRecords.Length; i++)
|
||||
{
|
||||
ref var savedRecord = ref savedRecords[i];
|
||||
if (savedRecord.index < records.Count)
|
||||
{
|
||||
var record = records[savedRecord.index];
|
||||
if (record == null && string.IsNullOrEmpty(savedRecord.metaID) == false)
|
||||
{
|
||||
record = RecoveryReferenceUtility.TryRecoverReference<IComponentTemplate>(savedRecord.metaID);
|
||||
records[savedRecord.index] = record;
|
||||
if (record == null) { return; }
|
||||
JsonUtility.FromJsonOverwrite(savedRecord.recordJson, record);
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ref var recordSingle = ref GetToRecoverSingle();
|
||||
if (string.IsNullOrEmpty(list.singleRecord.metaID))
|
||||
{
|
||||
|
||||
}
|
||||
if (isChanged)
|
||||
{
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
|
||||
@ -1,116 +0,0 @@
|
||||
//#define DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
using DCFApixels.DragonECS.Unity.Internal;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
[InitializeOnLoad]
|
||||
internal static class RecoveryReferenceUtility
|
||||
{
|
||||
internal static bool _recompileAfterInitializationScope = false;
|
||||
private static Dictionary<string, Type> _metaIDTypePairs;
|
||||
|
||||
static RecoveryReferenceUtility()
|
||||
{
|
||||
_recompileAfterInitializationScope = true;
|
||||
EditorApplication.update += BeforeCompilation;
|
||||
|
||||
}
|
||||
private static void BeforeCompilation()
|
||||
{
|
||||
_recompileAfterInitializationScope = false;
|
||||
EditorApplication.update -= BeforeCompilation;
|
||||
}
|
||||
|
||||
|
||||
private static void InitRecoverCache()
|
||||
{
|
||||
if (_metaIDTypePairs != null) { return; }
|
||||
_metaIDTypePairs = new Dictionary<string, Type>();
|
||||
|
||||
List<Type> types = new List<Type>();
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (type.TryGetAttribute(out MetaIDAttribute atr))
|
||||
{
|
||||
_metaIDTypePairs.Add(atr.ID, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
internal static T TryRecoverReference<T>(string metaID)
|
||||
{
|
||||
InitRecoverCache();
|
||||
if (_metaIDTypePairs.TryGetValue(metaID, out Type type))
|
||||
{
|
||||
return (T)Activator.CreateInstance(type);
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Serializable]
|
||||
public struct Reference<T>
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
: ISerializationCallbackReceiver
|
||||
#endif
|
||||
{
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
[SerializeReference]
|
||||
private T _value;
|
||||
[SerializeField]
|
||||
private string _json;
|
||||
public T Value
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get { return _value; }
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set { _value = value; }
|
||||
}
|
||||
#else
|
||||
[SerializeField]
|
||||
public T Value;
|
||||
#endif
|
||||
public bool IsNull
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get { return Value == null; }
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator T(Reference<T> a) { return a.Value; }
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator Reference<T>(T a) { return new Reference<T>() { Value = a }; }
|
||||
|
||||
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
|
||||
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
||||
{
|
||||
if (_value == null && RecoveryReferenceUtility._recompileAfterInitializationScope && string.IsNullOrEmpty(_json) == false)
|
||||
{
|
||||
int indexof = _json.IndexOf(',');
|
||||
_value = RecoveryReferenceUtility.TryRecoverReference<T>(_json.Substring(0, indexof));
|
||||
if (_value == null) { return; }
|
||||
JsonUtility.FromJsonOverwrite(_json.Substring(indexof + 1), _value);
|
||||
}
|
||||
_json = null;
|
||||
}
|
||||
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
||||
{
|
||||
if (_value != null && EditorApplication.isPlaying == false)
|
||||
{
|
||||
_json = $"{_value.GetMeta().MetaID},{JsonUtility.ToJson(_value)}";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6399ac2dd5218444b98f4dcf3a359611
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in New Issue
Block a user