com.alicizax.unity.framework/Runtime/ABase/Base/Service/Unity/MonoServiceBehaviour.cs
陈思海 6397cc03b2 框架大更新
1.进步优化UI系统 加载问题 性能问题 Canvas重绘问题 边界处理问题
2.优化对象池和游戏对象池的性能 游戏对象池根据窗口 策略定期清理
3.优化整个AppService 和ServiceWorld结构 固定三大类 具体参考代码
2026-03-31 17:25:20 +08:00

109 lines
2.8 KiB
C#

using UnityEngine;
namespace AlicizaX
{
public abstract class MonoServiceBehaviour : MonoBehaviour, IMonoService
{
public ServiceContext Context { get; private set; }
public bool IsInitialized { get; private set; }
protected ServiceWorld World => Context.World;
protected ServiceScope Scope => Context.Scope;
void IService.Initialize(ServiceContext context)
{
if (IsInitialized)
throw new System.InvalidOperationException($"{GetType().FullName} is already initialized.");
Context = context;
IsInitialized = true;
OnInitialize();
}
void IService.Destroy()
{
if (!IsInitialized) return;
OnDestroyService();
IsInitialized = false;
Context = default;
}
protected virtual void OnInitialize() { }
protected virtual void OnDestroyService() { }
}
public abstract class MonoServiceBehaviour<TScope> : MonoServiceBehaviour
where TScope : IScope
{
[SerializeField] private bool _dontDestroyOnLoad = false;
private void Awake()
{
OnAwake();
}
private void Start()
{
var scope = ResolveOrCreateScope();
if (scope.HasContract(GetType()))
{
Destroy(gameObject);
return;
}
if (_dontDestroyOnLoad)
DontDestroyOnLoad(gameObject);
scope.Register(this);
}
private void OnDestroy()
{
if (!IsInitialized) return;
if (!AppServices.HasWorld) return;
if (!TryResolveScope(out var scope)) return;
scope.Unregister(this);
}
private static ServiceScope ResolveOrCreateScope()
{
if (typeof(TScope) == typeof(AppScope))
return AppServices.App;
if (typeof(TScope) == typeof(SceneScope))
return AppServices.EnsureScene();
if (typeof(TScope) == typeof(GameplayScope))
return AppServices.EnsureGameplay();
throw new System.InvalidOperationException($"Unsupported service scope: {typeof(TScope).FullName}.");
}
private static bool TryResolveScope(out ServiceScope scope)
{
if (typeof(TScope) == typeof(AppScope))
{
scope = AppServices.App;
return true;
}
if (typeof(TScope) == typeof(SceneScope))
return AppServices.TryGetScene(out scope);
if (typeof(TScope) == typeof(GameplayScope))
return AppServices.TryGetGameplay(out scope);
scope = null;
return false;
}
protected virtual void OnAwake() { }
}
}