This commit is contained in:
Mikhail 2024-09-21 14:39:09 +08:00
parent c8b56cadae
commit a6bb3799d3
70 changed files with 711 additions and 145 deletions

View File

@ -5,7 +5,7 @@ using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Editors
{
[CustomEditor(typeof(EcsEntityConnect))]
[CustomEditor(typeof(EcsEntityConnect), true)]
[CanEditMultipleObjects]
internal class EcsEntityConnectEditor : ExtendedEditor<EcsEntityConnect>
{

View File

@ -8,7 +8,10 @@
public const string DEBUG_LAYER = EcsConsts.NAME_SPACE + "Unity." + nameof(DEBUG_LAYER);
public const string LOCAL_CACHE_AND_USER_SETTINGS_FOLDER = "/Library/" + EcsConsts.AUTHOR + "/com.dcfa_pixels.dragonecs-unity";
public const string UNITY_PACKAGE_NAME = "com.dcfa_pixels.dragonecs-unity";
public const string LOCAL_CACHE_FOLDER = "/Library/" + EcsConsts.AUTHOR + "/" + UNITY_PACKAGE_NAME;
public const string USER_SETTINGS_FOLDER = "/UserSettings/" + EcsConsts.AUTHOR + "/" + UNITY_PACKAGE_NAME;
//EcsConsts.AUTHOR + "/" + EcsConsts.FRAMEWORK_NAME + "/" + nameof(DragonDocsPrefs) + ".prefs"
}

View File

@ -18,12 +18,15 @@ namespace DCFApixels.DragonECS.Unity.Editors
wnd.Show();
}
private List<DefineSymbolsInfo> _defineSymbols = null;
private DefineSymbolsInfo[] _defineSymbols = null;
private void InitDefines()
{
string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.Standalone);
_defineSymbols = new List<DefineSymbolsInfo>(typeof(EcsDefines).GetFields(BindingFlags.Static | BindingFlags.Public).Select(o => new DefineSymbolsInfo(o.Name, false)));
for (int i = 0; i < _defineSymbols.Count; i++)
_defineSymbols =
typeof(EcsDefines).GetFields(BindingFlags.Static | BindingFlags.Public).Select(o => new DefineSymbolsInfo(o.Name, false))
.Concat(typeof(EcsUnityDefines).GetFields(BindingFlags.Static | BindingFlags.Public).Select(o => new DefineSymbolsInfo(o.Name, false)))
.ToArray();
for (int i = 0; i < _defineSymbols.Length; i++)
{
var symbol = _defineSymbols[i];
if (symbolsString.Contains(symbol.name))
@ -34,7 +37,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
}
private void OnGUI()
{
var prefs = SettingsPrefs.instance;
var prefs = UserSettingsPrefs.instance;
float labelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 0f;
@ -47,31 +50,31 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUILayout.Label("", EditorStyles.toolbar, GUILayout.ExpandWidth(true), GUILayout.Height(22f));
Rect rect = GUILayoutUtility.GetLastRect();
rect.xMin += 9f;
GUI.Label(rect, "Settings", EditorStyles.whiteLargeLabel);
GUI.Label(rect, "User Settings", EditorStyles.whiteLargeLabel);
//using (prefs.DisableAutoSave())
{
GUILayout.BeginHorizontal();
prefs.IsShowHidden = EditorGUILayout.Toggle(prefs.IsShowHidden, GUILayout.Width(checkBoxWidth));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(SettingsPrefs.IsShowHidden)), GUILayout.ExpandWidth(false));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(UserSettingsPrefs.IsShowHidden)), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
prefs.IsShowInterfaces = EditorGUILayout.Toggle(prefs.IsShowInterfaces, GUILayout.Width(checkBoxWidth));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(SettingsPrefs.IsShowInterfaces)), GUILayout.ExpandWidth(false));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(UserSettingsPrefs.IsShowInterfaces)), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
prefs.IsShowRuntimeComponents = EditorGUILayout.Toggle(prefs.IsShowRuntimeComponents, GUILayout.Width(checkBoxWidth));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(SettingsPrefs.IsShowRuntimeComponents)), GUILayout.ExpandWidth(false));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(UserSettingsPrefs.IsShowRuntimeComponents)), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
prefs.IsUseCustomNames = EditorGUILayout.Toggle(prefs.IsUseCustomNames, GUILayout.Width(checkBoxWidth));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(SettingsPrefs.IsUseCustomNames)), GUILayout.ExpandWidth(false));
GUILayout.Label(UnityEditorUtility.TransformFieldName(nameof(UserSettingsPrefs.IsUseCustomNames)), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
prefs.ComponentColorMode = (ComponentColorMode)EditorGUILayout.EnumPopup(UnityEditorUtility.TransformFieldName(nameof(SettingsPrefs.ComponentColorMode)), prefs.ComponentColorMode);
prefs.ComponentColorMode = (ComponentColorMode)EditorGUILayout.EnumPopup(UnityEditorUtility.TransformFieldName(nameof(UserSettingsPrefs.ComponentColorMode)), prefs.ComponentColorMode);
}
GUILayout.EndVertical();
@ -89,7 +92,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
InitDefines();
}
EditorGUI.BeginChangeCheck();
for (int i = 0; i < _defineSymbols.Count; i++)
for (int i = 0; i < _defineSymbols.Length; i++)
{
GUILayout.BeginHorizontal();
var symbol = _defineSymbols[i];
@ -108,7 +111,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (GUILayout.Button("Apply"))
{
string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
for (int i = 0; i < _defineSymbols.Count; i++)
for (int i = 0; i < _defineSymbols.Length; i++)
{
var symbol = _defineSymbols[i];
symbolsString = symbolsString.Replace(symbol.name, "");

View File

@ -10,8 +10,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
Auto = 1,
Rainbow = 2,
}
[FilePath(EcsUnityConsts.LOCAL_CACHE_AND_USER_SETTINGS_FOLDER + "/" + nameof(SettingsPrefs) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
internal class SettingsPrefs : ScriptableSingleton<SettingsPrefs>
[FilePath(EcsUnityConsts.USER_SETTINGS_FOLDER + "/" + nameof(UserSettingsPrefs) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
internal class UserSettingsPrefs : ScriptableSingleton<UserSettingsPrefs>
{
[SerializeField]
private bool _isUseCustomNames = true;

View File

@ -6,7 +6,7 @@ using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Docs.Editors
{
[FilePath(EcsUnityConsts.LOCAL_CACHE_AND_USER_SETTINGS_FOLDER + "/" + nameof(DragonDocsPrefs) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
[FilePath(EcsUnityConsts.LOCAL_CACHE_FOLDER + "/" + nameof(DragonDocsPrefs) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
internal class DragonDocsPrefs : ScriptableSingleton<DragonDocsPrefs>
{
[SerializeField] private DragonDocs m_docs;

View File

@ -36,13 +36,13 @@ namespace DCFApixels.DragonECS.Unity.Docs.Editors
private DragonDocsPrefs Prefs { get { return DragonDocsPrefs.instance; } }
private static bool IsShowHidden
{
get { return SettingsPrefs.instance.IsShowHidden; }
set { SettingsPrefs.instance.IsShowHidden = value; }
get { return UserSettingsPrefs.instance.IsShowHidden; }
set { UserSettingsPrefs.instance.IsShowHidden = value; }
}
private static bool IsUseCustomNames
{
get { return SettingsPrefs.instance.IsUseCustomNames; }
set { SettingsPrefs.instance.IsUseCustomNames = value; }
get { return UserSettingsPrefs.instance.IsUseCustomNames; }
set { UserSettingsPrefs.instance.IsUseCustomNames = value; }
}

View File

@ -1,71 +0,0 @@
using DCFApixels.DragonECS.Unity;
using DCFApixels.DragonECS.Unity.Internal;
using System;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public abstract class MonoEntityTemplateBase : MonoBehaviour, ITemplate
{
public abstract void Apply(short worldID, int entityID);
}
[DisallowMultipleComponent]
[AddComponentMenu(EcsConsts.FRAMEWORK_NAME + "/" + nameof(MonoEntityTemplate), 30)]
[MetaColor(MetaColor.Cyan)]
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsUnityConsts.ENTITY_BUILDING_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, nameof(MonoBehaviour) + " implementation of an entity template. Templates are a set of components that are applied to entities.")]
public class MonoEntityTemplate : MonoEntityTemplateBase, ITemplateInternal
{
[SerializeReference]
private IComponentTemplate[] _components;
#region Properties
string ITemplateInternal.ComponentsPropertyName
{
get { return nameof(_components); }
}
#endregion
#region Methods
public override void Apply(short worldID, int entityID)
{
foreach (var item in _components)
{
item.Apply(worldID, entityID);
}
}
public void Clear()
{
_components = Array.Empty<IComponentTemplate>();
}
#endregion
#region UnityEvents
private void OnValidate()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnValidate(gameObject);
}
}
private void OnDrawGizmos()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnGizmos(transform, IComponentTemplate.GizmosMode.Always);
}
}
private void OnDrawGizmosSelected()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnGizmos(transform, IComponentTemplate.GizmosMode.Selected);
}
}
#endregion
}
}

View File

@ -267,18 +267,18 @@ namespace DCFApixels.DragonECS.Unity.Editors
#region Properties
private static ComponentColorMode AutoColorMode
{
get { return SettingsPrefs.instance.ComponentColorMode; }
set { SettingsPrefs.instance.ComponentColorMode = value; }
get { return UserSettingsPrefs.instance.ComponentColorMode; }
set { UserSettingsPrefs.instance.ComponentColorMode = value; }
}
private static bool IsShowHidden
{
get { return SettingsPrefs.instance.IsShowHidden; }
set { SettingsPrefs.instance.IsShowHidden = value; }
get { return UserSettingsPrefs.instance.IsShowHidden; }
set { UserSettingsPrefs.instance.IsShowHidden = value; }
}
private static bool IsShowRuntimeComponents
{
get { return SettingsPrefs.instance.IsShowRuntimeComponents; }
set { SettingsPrefs.instance.IsShowRuntimeComponents = value; }
get { return UserSettingsPrefs.instance.IsShowRuntimeComponents; }
set { UserSettingsPrefs.instance.IsShowRuntimeComponents = value; }
}
private static float OneLineHeight
{

View File

@ -35,13 +35,13 @@ namespace DCFApixels.DragonECS.Unity.Editors
}
protected bool IsShowInterfaces
{
get { return SettingsPrefs.instance.IsShowInterfaces; }
set { SettingsPrefs.instance.IsShowInterfaces = value; }
get { return UserSettingsPrefs.instance.IsShowInterfaces; }
set { UserSettingsPrefs.instance.IsShowInterfaces = value; }
}
protected bool IsShowHidden
{
get { return SettingsPrefs.instance.IsShowHidden; }
set { SettingsPrefs.instance.IsShowHidden = value; }
get { return UserSettingsPrefs.instance.IsShowHidden; }
set { UserSettingsPrefs.instance.IsShowHidden = value; }
}
protected bool IsMultipleTargets => targets.Length > 1;
@ -144,13 +144,13 @@ namespace DCFApixels.DragonECS.Unity.Editors
}
protected bool IsShowInterfaces
{
get { return SettingsPrefs.instance.IsShowInterfaces; }
set { SettingsPrefs.instance.IsShowInterfaces = value; }
get { return UserSettingsPrefs.instance.IsShowInterfaces; }
set { UserSettingsPrefs.instance.IsShowInterfaces = value; }
}
protected bool IsShowHidden
{
get { return SettingsPrefs.instance.IsShowHidden; }
set { SettingsPrefs.instance.IsShowHidden = value; }
get { return UserSettingsPrefs.instance.IsShowHidden; }
set { UserSettingsPrefs.instance.IsShowHidden = value; }
}
protected virtual bool IsStaticInit { get { return _isStaticInit; } }
protected virtual bool IsInit { get { return _isInit; } }

View File

@ -50,7 +50,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
Type type = item.Obj;
if (_arrayProperty != null)
if (_arrayProperty != null && type != null)
{
int index = _arrayProperty.arraySize;
_arrayProperty.arraySize += 1;
@ -121,7 +121,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
Type componentType = item.Obj.GetType();
IComponentTemplate cmptmp = item.Obj;
if (_arrayProperty != null)
if (_arrayProperty != null && cmptmp != null)
{
int index = _arrayProperty.arraySize;
if (_isCheckUnique)

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

Before

Width:  |  Height:  |  Size: 379 B

After

Width:  |  Height:  |  Size: 379 B

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 299 B

After

Width:  |  Height:  |  Size: 299 B

View File

Before

Width:  |  Height:  |  Size: 646 B

After

Width:  |  Height:  |  Size: 646 B

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 647 B

After

Width:  |  Height:  |  Size: 647 B

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6a95e563d250e80489b71ecdd0392cf0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,309 @@
#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

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 839f12827a9b59d4ea2cb945a98f3e6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
src/Templates.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 305c6d80bbe938c40b8789ac24b0b812
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace DCFApixels.DragonECS
@ -146,5 +147,106 @@ 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
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ec26aa9da3a093f418f434297d3ec377
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,8 +15,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
private static ComponentColorMode AutoColorMode
{
get { return SettingsPrefs.instance.ComponentColorMode; }
set { SettingsPrefs.instance.ComponentColorMode = value; }
get { return UserSettingsPrefs.instance.ComponentColorMode; }
set { UserSettingsPrefs.instance.ComponentColorMode = value; }
}
#region Init

View File

@ -0,0 +1,179 @@
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]
[AddComponentMenu(EcsConsts.FRAMEWORK_NAME + "/" + nameof(MonoEntityTemplate), 30)]
[MetaColor(MetaColor.Cyan)]
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsUnityConsts.ENTITY_BUILDING_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, nameof(MonoBehaviour) + " implementation of an entity template. Templates are a set of components that are applied to entities.")]
public class MonoEntityTemplate : MonoEntityTemplateBase, ITemplateInternal
{
[SerializeReference]
private IComponentTemplate[] _components;
#region Properties
string ITemplateInternal.ComponentsPropertyName
{
get { return nameof(_components); }
}
#endregion
#region Methods
public override void Apply(short worldID, int entityID)
{
foreach (var item in _components)
{
item.Apply(worldID, entityID);
}
}
public void Clear()
{
_components = Array.Empty<IComponentTemplate>();
}
#endregion
#region UnityEvents
private void OnValidate()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnValidate(gameObject);
}
}
private void OnDrawGizmos()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnGizmos(transform, IComponentTemplate.GizmosMode.Always);
}
}
private void OnDrawGizmosSelected()
{
if (_components == null) { return; }
foreach (var item in _components)
{
item?.OnGizmos(transform, IComponentTemplate.GizmosMode.Selected);
}
}
#endregion
}
}

View File

@ -1,13 +1,20 @@
using DCFApixels.DragonECS.Unity;
using DCFApixels.DragonECS.Unity.Internal;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public abstract class ScriptableEntityTemplateBase : ScriptableObject, 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; }
}
[MetaColor(MetaColor.Cyan)]

View File

@ -13,9 +13,12 @@ namespace DCFApixels.DragonECS
{
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
[InitializeOnLoad]
internal static class ReferenceUtility
internal static class RecoveryReferenceUtility
{
static ReferenceUtility()
internal static bool _recompileAfterInitializationScope = false;
private static Dictionary<string, Type> _metaIDTypePairs;
static RecoveryReferenceUtility()
{
_recompileAfterInitializationScope = true;
EditorApplication.update += BeforeCompilation;
@ -27,7 +30,33 @@ namespace DCFApixels.DragonECS
EditorApplication.update -= BeforeCompilation;
}
internal static bool _recompileAfterInitializationScope = false;
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
@ -64,42 +93,12 @@ namespace DCFApixels.DragonECS
public static implicit operator Reference<T>(T a) { return new Reference<T>() { Value = a }; }
#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY
private static Dictionary<string, Type> _metaIDTypePairs;
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);
}
}
}
}
private static T TryRecoverReference(string metaID)
{
InitRecoverCache();
if (_metaIDTypePairs.TryGetValue(metaID, out Type type))
{
return (T)Activator.CreateInstance(type);
}
return default;
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (_value == null && ReferenceUtility._recompileAfterInitializationScope && string.IsNullOrEmpty(_json) == false)
if (_value == null && RecoveryReferenceUtility._recompileAfterInitializationScope && string.IsNullOrEmpty(_json) == false)
{
int indexof = _json.IndexOf(',');
_value = TryRecoverReference(_json.Substring(0, indexof));
_value = RecoveryReferenceUtility.TryRecoverReference<T>(_json.Substring(0, indexof));
if (_value == null) { return; }
JsonUtility.FromJsonOverwrite(_json.Substring(indexof + 1), _value);
}