com.alicizax.unity.framework/Runtime/ABase/Service/Unity/MonoServiceBehaviour.cs

105 lines
2.8 KiB
C#
Raw Normal View History

2026-03-26 10:49:41 +08:00
using UnityEngine;
namespace AlicizaX
{
2026-04-20 13:46:44 +08:00
public abstract class MonoServiceBehaviour : MonoBehaviour, IMonoService, IServiceLifecycle
2026-03-26 10:49:41 +08:00
{
2026-04-20 13:46:44 +08:00
protected ServiceContext Context { get; private set; }
2026-03-26 10:49:41 +08:00
2026-04-20 13:46:44 +08:00
protected bool IsInitialized { get; private set; }
2026-03-26 10:49:41 +08:00
2026-04-20 13:46:44 +08:00
void IServiceLifecycle.Initialize(ServiceContext context)
2026-03-26 10:49:41 +08:00
{
if (IsInitialized)
throw new System.InvalidOperationException($"{GetType().FullName} is already initialized.");
Context = context;
IsInitialized = true;
2026-03-26 19:55:46 +08:00
OnInitialize();
2026-03-26 10:49:41 +08:00
}
2026-04-20 13:46:44 +08:00
void IServiceLifecycle.Destroy()
2026-03-26 10:49:41 +08:00
{
if (!IsInitialized) return;
2026-03-26 19:55:46 +08:00
OnDestroyService();
2026-03-26 10:49:41 +08:00
IsInitialized = false;
Context = default;
}
2026-03-26 19:55:46 +08:00
protected virtual void OnInitialize() { }
2026-03-26 10:49:41 +08:00
2026-03-26 19:55:46 +08:00
protected virtual void OnDestroyService() { }
2026-03-26 10:49:41 +08:00
}
public abstract class MonoServiceBehaviour<TScope> : MonoServiceBehaviour
2026-03-26 19:50:59 +08:00
where TScope : IScope
2026-03-26 10:49:41 +08:00
{
[SerializeField] private bool _dontDestroyOnLoad = false;
private void Awake()
{
OnAwake();
}
private void Start()
{
var scope = ResolveOrCreateScope();
2026-03-26 10:49:41 +08:00
if (scope.HasContract(GetType()))
{
Destroy(gameObject);
return;
}
2026-03-26 13:51:09 +08:00
if (_dontDestroyOnLoad)
DontDestroyOnLoad(gameObject);
2026-03-26 10:49:41 +08:00
scope.Register(this);
}
private void OnDestroy()
{
if (!IsInitialized) return;
if (!AppServices.HasWorld) return;
if (!TryResolveScope(out var scope)) return;
2026-03-26 10:49:41 +08:00
scope.Unregister(this);
}
private static ServiceScope ResolveOrCreateScope()
{
if (typeof(TScope) == typeof(AppScope))
2026-04-20 13:46:44 +08:00
return AppServices.RequireWorld().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))
{
2026-04-20 13:46:44 +08:00
scope = AppServices.RequireWorld().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;
}
2026-03-26 10:49:41 +08:00
protected virtual void OnAwake() { }
}
}