116 lines
3.5 KiB
C#
116 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Fantasy;
|
|
using Fantasy.Async;
|
|
using Fantasy.Generated;
|
|
using UnityEngine;
|
|
|
|
namespace NBC
|
|
{
|
|
public partial class App : MonoBehaviour
|
|
{
|
|
public static App Inst { get; private set; }
|
|
|
|
private static Scene _scene;
|
|
public static Scene Main => _scene;
|
|
private static event Action OnInitialized;
|
|
|
|
public static event Action OnUpdate;
|
|
public static event Action OnLateUpdate;
|
|
public static event Action OnFixedUpdate;
|
|
public static event Action OnApplicationQuitAction;
|
|
public static event Action OnApplicationPauseAction;
|
|
|
|
/// <summary>
|
|
/// Scene下的事件系统组件
|
|
/// </summary>
|
|
public static UIComponent UI { get; internal set; }
|
|
|
|
public static void Init(Action callback = null)
|
|
{
|
|
if (Inst != null) return;
|
|
new GameObject("App").AddComponent<App>();
|
|
OnInitialized += callback;
|
|
// NBC_Fantasy_EntitySystemRegistrar
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
Inst = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartAsync().Coroutine();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
OnUpdate?.Invoke();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
OnLateUpdate?.Invoke();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
OnFixedUpdate?.Invoke();
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
OnApplicationQuitAction?.Invoke();
|
|
}
|
|
|
|
private void OnApplicationPause(bool pauseStatus)
|
|
{
|
|
OnApplicationPauseAction?.Invoke();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_scene?.Dispose();
|
|
}
|
|
|
|
private async FTask StartAsync()
|
|
{
|
|
// 初始化框架
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
List<System.Reflection.Assembly> loadAssemblies = new List<System.Reflection.Assembly>();
|
|
foreach (var assembly in assemblies)
|
|
{
|
|
// 跳过系统程序集以提高性能(可选)
|
|
if (IsSystemAssembly(assembly))
|
|
continue;
|
|
loadAssemblies.Add(assembly);
|
|
}
|
|
|
|
// 1. 初始化 Fantasy 框架
|
|
await Fantasy.Platform.Unity.Entry.Initialize();
|
|
|
|
// 2. 创建一个 Scene (客户端场景)
|
|
// Scene 是 Fantasy 框架的核心容器,所有功能都在 Scene 下运行
|
|
// SceneRuntimeMode.MainThread 表示在 Unity 主线程运行
|
|
_scene = await Scene.Create(SceneRuntimeMode.MainThread);
|
|
UI = _scene.AddComponent<UIComponent>();
|
|
OnInitialized?.Invoke();
|
|
}
|
|
|
|
// 判断是否是系统程序集(可选优化)
|
|
private static bool IsSystemAssembly(System.Reflection.Assembly assembly)
|
|
{
|
|
string assemblyName = assembly.FullName;
|
|
return assemblyName.StartsWith("System") ||
|
|
assemblyName.StartsWith("Microsoft.") ||
|
|
assemblyName.StartsWith("UnityEngine") ||
|
|
assemblyName.StartsWith("UnityEditor") ||
|
|
assemblyName.StartsWith("mscorlib") ||
|
|
assemblyName.StartsWith("netstandard") ||
|
|
assemblyName.StartsWith("nunit.") ||
|
|
assemblyName.StartsWith("Unity.");
|
|
}
|
|
}
|
|
} |