BITKit/Packages/Runtime/BITAppForUnity.cs

145 lines
4.4 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine.InputSystem;
using UnityEngine.AddressableAssets;
namespace BITKit
{
public class MouseNotOverGameViewException : System.Exception
{
public MouseNotOverGameViewException()
{
}
}
public class CreateOnStart:System.Attribute
{
public readonly string path;
public CreateOnStart(params string[] path)
{
this.path = string.Join("/", path);
}
}
public class BITAppForUnity : MonoBehaviour, IDiagnostics
{
public struct Path
{
public const string Entity = nameof(Entity);
public const string Services = nameof(Services);
public const string Prefabs = nameof(Prefabs);
}
public static bool IsPlaying { get; private set; }
public static ValidHandle AllowCursor = new();
public static ValidHandle AllowTouchSupport = new();
public static GameObject GameObject;
public static void ThrowIfNotPlaying()
{
#if UNITY_EDITOR
if (IsPlaying is false)
{
throw new System.Exception("Editor Is Not Playing");
}
#endif
}
public static void ThrowIfWindowNotFocus()
{
#if UNITY_EDITOR
if (UnityEditor.EditorWindow.mouseOverWindow.ToString() is not " (UnityEditor.GameView)")
{
//Debug.Log(UnityEditor.EditorWindow.mouseOverWindow);
throw new MouseNotOverGameViewException();
}
#endif
}
[RuntimeInitializeOnLoadMethod]
static async void Reload()
{
IsPlaying = true;
BIT4Log.OnLog += Debug.Log;
BIT4Log.OnWarning += Debug.LogWarning;
BIT4Log.OnException += Debug.LogException;
BIT4Log.Log<BITApp>($"正在初始化");
BIT4Log.Log<BITApp>($"正在注册Log");
BIT4Log.Log<BITApp>($"正在加载配置文件");
var settings = Addressables.LoadAssetAsync<ScriptableObject>(nameof(BITApp.AppSettings)).WaitForCompletion() as BITSettingsSO;
BIT4Log.Log<BITApp>($"正在启动{Application.productName}");
BITApp.Start(Application.productName, settings!.appSettings);
AllowCursor = new();
AllowTouchSupport = new();
BIT4Log.Log<BITApp>($"正在创建{nameof(BITApp)}");
GameObject = new(nameof(BITApp));
GameObject.AddComponent<BITAppForUnity>();
DontDestroyOnLoad(GameObject);
AllowCursor.AddListener(ToggleCursor);
AllowTouchSupport.AddListener(ToggleTouchSupport);
foreach (var x in await ReflectionHelper.GetAttributes<CreateOnStart>())
{
var prefab = Addressables.LoadAssetAsync<GameObject>(x.path).WaitForCompletion();
var instance = Instantiate(prefab);
DontDestroyOnLoad(instance);
}
BIT4Log.Log<BITApp>($"已创建{nameof(BITApp)}");
}
static void ToggleCursor(bool active)
{
Cursor.lockState = active ? CursorLockMode.None : CursorLockMode.Locked;
Cursor.visible = active;
}
static void ToggleTouchSupport(bool active)
{
if (active)
{
UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport.Enable();
}
else
{
UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport.Disable();
}
}
[BITCommand]
public static async void Exit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
await BITApp.Stop();
#else
await BITApp.Stop();
Application.Quit();
#endif
}
void Start()
{
var cancellationToken = gameObject.GetCancellationTokenOnDestroy();
cancellationToken.Register(Exit);
BIT4Log.Log<BITApp>($"已创建{nameof(BITApp)}");
}
void OnDestroy()
{
IsPlaying = false;
}
public string GetName()
{
return nameof(BITApp);
}
public object GetDiagnostics()
{
return BITApp.State;
}
}
}