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