Files
ShrinkContext.AppAdapter/Runtime/ShrinkAppLoaderBootstrapper.cs
T
cneicy 2e8cc588c4
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:49:53 +08:00

68 lines
2.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#nullable enable
using Cysharp.Threading.Tasks;
using ShrinkApp;
using UnityEngine;
namespace ShrinkContext.AppAdapter
{
/// <summary>
/// ShrinkApp 加载器宿主的自动引导:
/// hostingMode == ContextLoader 时在 BeforeSceneLoad 创建常驻 GameObject 并启动 ShrinkAppLoaderHost
/// 同时把服务容器接回 ShrinkApp 静态入口(InitializeForExternalHost),保证场景脚本照常访问 ShrinkApp.Services。
/// ClassicHost 模式下完全不动——经典路径零行为变化。
/// </summary>
public sealed class ShrinkAppLoaderBootstrapper : MonoBehaviour
{
private static ShrinkAppLoaderBootstrapper? _instance;
/// <summary>
/// Starter/组合包在 SubsystemRegistration 阶段设置的默认装配表。
/// AppAdapter 保持不依赖具体业务模块,组合根负责注册原生组件;若兼容 installer 已被发现则覆盖同 id。
/// </summary>
public static System.Action<ShrinkAppLoaderHost>? DefaultComposition { get; set; }
public static ShrinkAppLoaderBootstrapper? Instance => _instance;
public ShrinkAppLoaderHost Host { get; private set; } = null!;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void AutoBootstrap()
{
if (ShrinkAppSettings.Instance.hostingMode != ShrinkAppHostingMode.ContextLoader)
return;
var gameObject = new GameObject("ShrinkAppLoaderHost");
DontDestroyOnLoad(gameObject);
gameObject.AddComponent<ShrinkAppLoaderBootstrapper>();
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
Host = new ShrinkAppLoaderHost();
DefaultComposition?.Invoke(Host);
var profile = ShrinkAppCompositionProfile.LoadDefault();
if (profile != null)
Host.ApplyComposition(profile.ResolveDocument());
global::ShrinkApp.ShrinkApp.InitializeForExternalHost(Host.Services);
Host.StartAsync().Forget(ex =>
{
Debug.LogException(ex);
Debug.LogError("[ShrinkApp.LoaderHost] 启动失败: " + ex.Message);
});
}
private void OnDestroy()
{
if (_instance == this)
_instance = null;
}
}
}