com.alicizax.unity.framework/Runtime/ABase/Base/Service/Unity/AppServiceRoot.cs
陈思海 cad7722e44 优化
1.重名所有App级模块为Service
2.移除Module中心 移除SingletonManager
3.引入Service Scope Context概念  避免上下文Manager到处引用
4.修复部分bug
2026-03-26 16:14:05 +08:00

74 lines
1.9 KiB
C#

using Cysharp.Threading.Tasks;
using UnityEngine;
namespace AlicizaX
{
[DefaultExecutionOrder(-32000)]
[DisallowMultipleComponent]
public class AppServiceRoot : MonoBehaviour
{
private static AppServiceRoot s_activeRoot;
[SerializeField] private bool _dontDestroyOnLoad = true;
[SerializeField] private int _appScopeOrder = -10000;
private bool _ownsWorld;
protected virtual void Awake()
{
if (s_activeRoot != null && s_activeRoot != this)
{
enabled = false;
return;
}
s_activeRoot = this;
if (_dontDestroyOnLoad)
DontDestroyOnLoad(gameObject);
var createdWorld = !AppServices.HasWorld;
var world = AppServices.EnsureWorld(_appScopeOrder);
_ownsWorld = createdWorld;
if (createdWorld)
RegisterAppServices(world.AppScope);
}
protected virtual void Update()
{
if (AppServices.HasWorld) AppServices.World.Tick(Time.deltaTime);
}
protected virtual void LateUpdate()
{
if (AppServices.HasWorld) AppServices.World.LateTick(Time.deltaTime);
}
protected virtual void FixedUpdate()
{
if (AppServices.HasWorld) AppServices.World.FixedTick(Time.fixedDeltaTime);
}
protected virtual void OnDrawGizmos()
{
if (AppServices.HasWorld) AppServices.World.DrawGizmos();
}
protected virtual async void OnDestroy()
{
if (s_activeRoot == this)
s_activeRoot = null;
if (_ownsWorld && AppServices.HasWorld)
{
await UniTask.Yield();
AppServices.Shutdown();
}
}
protected virtual void RegisterAppServices(ServiceScope appScope) { }
}
}