mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2025-09-18 10:04:36 +08:00
add recovery reference
This commit is contained in:
parent
25251f044f
commit
19d968d8c0
61
src/Utils/Reference.cs
Normal file
61
src/Utils/Reference.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace DCFApixels.DragonECS
|
||||||
|
{
|
||||||
|
[System.Serializable]
|
||||||
|
public struct Reference<T>
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
: ISerializationCallbackReceiver
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
[SerializeReference]
|
||||||
|
private T _value;
|
||||||
|
[SerializeField]
|
||||||
|
private string _json;
|
||||||
|
public T Value
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get { return _value; }
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
set { _value = value; }
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
public T Value;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
private static System.Collections.Generic.Dictionary<string, System.Type> _metaIDTypePairs;
|
||||||
|
private static void InitRecoverCache()
|
||||||
|
{
|
||||||
|
if (_metaIDTypePairs == null) { return; }
|
||||||
|
_metaIDTypePairs = new System.Collections.Generic.Dictionary<string, System.Type>();
|
||||||
|
}
|
||||||
|
private static T TryRecoverReference(string metaID)
|
||||||
|
{
|
||||||
|
InitRecoverCache();
|
||||||
|
if (_metaIDTypePairs.TryGetValue(metaID, out System.Type type))
|
||||||
|
{
|
||||||
|
return (T)System.Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
||||||
|
{
|
||||||
|
if (_value == null)
|
||||||
|
{
|
||||||
|
int indexof = _json.IndexOf(',');
|
||||||
|
_value = TryRecoverReference(_json.Substring(0, indexof));
|
||||||
|
if (_value == null) { return; }
|
||||||
|
JsonUtility.FromJsonOverwrite(_json.Substring(indexof + 1), _value);
|
||||||
|
_json = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
||||||
|
{
|
||||||
|
_json = $"{_value.GetMeta().MetaID},{JsonUtility.ToJson(_value)}";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
11
src/Utils/Reference.cs.meta
Normal file
11
src/Utils/Reference.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
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