AlicizaX/Client/Assets/Scripts/New/ServiceFramework/Unity/GameServiceRoot.cs

69 lines
1.8 KiB
C#
Raw Normal View History

2026-03-25 18:33:59 +08:00
using UnityEngine;
namespace Aliciza.Services
{
[DefaultExecutionOrder(-32000)]
[DisallowMultipleComponent]
public class GameServiceRoot : MonoBehaviour
{
private static GameServiceRoot 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 = !GameServices.HasWorld;
var world = GameServices.EnsureWorld(_appScopeOrder);
_ownsWorld = createdWorld;
if (createdWorld)
RegisterAppServices(world.AppScope);
}
protected virtual void Update()
{
if (GameServices.HasWorld) GameServices.World.Tick(Time.deltaTime);
}
protected virtual void LateUpdate()
{
if (GameServices.HasWorld) GameServices.World.LateTick(Time.deltaTime);
}
protected virtual void FixedUpdate()
{
if (GameServices.HasWorld) GameServices.World.FixedTick(Time.fixedDeltaTime);
}
protected virtual void OnDrawGizmos()
{
if (GameServices.HasWorld) GameServices.World.DrawGizmos();
}
protected virtual void OnDestroy()
{
if (s_activeRoot == this)
s_activeRoot = null;
if (_ownsWorld && GameServices.HasWorld)
GameServices.Shutdown();
}
protected virtual void RegisterAppServices(ServiceScope appScope) { }
}
}