DragonECS-Unity/src/Internal/Editor/SOWrappers/WrapperBase.cs

93 lines
2.7 KiB
C#
Raw Normal View History

2024-03-03 22:46:26 +08:00
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
2024-03-04 07:38:38 +08:00
using System.Runtime.CompilerServices;
2024-03-03 22:46:26 +08:00
using UnityEditor;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Editors
{
[Serializable]
internal abstract class WrapperBase : ScriptableObject
{
public abstract object Data { get; }
2024-03-04 03:00:45 +08:00
public abstract bool IsExpanded { get; set; }
2024-03-03 22:46:26 +08:00
public abstract SerializedObject SO { get; }
public abstract SerializedProperty Property { get; }
public abstract void Release();
}
[Serializable]
internal abstract class WrapperBase<TSelf> : WrapperBase
where TSelf : WrapperBase<TSelf>
{
private SerializedObject _so;
private SerializedProperty _property;
2024-03-04 03:00:45 +08:00
private bool _isDestroyed = false;
2024-03-03 22:46:26 +08:00
private bool _isReleased = false;
2024-03-04 03:00:45 +08:00
2024-03-03 22:46:26 +08:00
private static Stack<TSelf> _wrappers = new Stack<TSelf>();
2024-03-04 03:00:45 +08:00
public override bool IsExpanded
{
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-04 03:00:45 +08:00
get { return Property.isExpanded; }
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-04 03:00:45 +08:00
set { Property.isExpanded = value; }
}
2024-03-03 22:46:26 +08:00
public override SerializedObject SO
{
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:26 +08:00
get { return _so; }
}
public override SerializedProperty Property
{
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:26 +08:00
get { return _property; }
}
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:26 +08:00
public static TSelf Take()
{
TSelf result;
if (_wrappers.Count <= 0)
{
result = CreateInstance<TSelf>();
result._so = new SerializedObject(result);
result._property = result._so.FindProperty("data");
}
else
{
result = _wrappers.Pop();
2024-03-04 03:00:45 +08:00
if (result._isDestroyed)
{
result = Take();
}
2024-03-03 22:46:26 +08:00
}
2024-03-04 03:00:45 +08:00
result._isReleased = false;
2024-03-03 22:46:26 +08:00
return result;
}
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:26 +08:00
public static void Release(TSelf wrapper)
{
if (wrapper._isReleased)
{
2024-03-04 03:00:45 +08:00
throw new Exception();
2024-03-03 22:46:26 +08:00
}
wrapper._isReleased = true;
_wrappers.Push(wrapper);
}
2024-03-04 03:00:45 +08:00
private void OnDestroy()
{
_isDestroyed = true;
}
2024-03-04 07:38:38 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:26 +08:00
public override void Release()
{
Release((TSelf)this);
}
}
}
#endif