BITKit/Packages/Runtime~/Unity/Common/Scripts/BITAppForUnity.cs

128 lines
3.8 KiB
C#
Raw Normal View History

2023-06-29 14:57:11 +08:00
using System;
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 BITAppForUnity : MonoBehaviour, IDiagnostics
{
public struct Path
{
public const string Entity = nameof(Entity);
public const string Services = nameof(Services);
public const string Prefabs = nameof(Prefabs);
}
2023-06-07 18:38:07 +08:00
private static bool IsPlaying { get; set; }
2023-06-29 14:57:11 +08:00
public static bool IsPointerOverUI { get; set; }
2023-06-05 19:57:17 +08:00
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)
{
2023-06-07 18:38:07 +08:00
throw new AppIsNotPlayingException();
2023-06-05 19:57:17 +08:00
}
#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]
2023-06-29 14:57:11 +08:00
private static void Reload()
2023-06-05 19:57:17 +08:00
{
IsPlaying = true;
BIT4Log.OnLog += Debug.Log;
BIT4Log.OnWarning += Debug.LogWarning;
BIT4Log.OnException += Debug.LogException;
var settings = Addressables.LoadAssetAsync<ScriptableObject>(nameof(BITApp.AppSettings)).WaitForCompletion() as BITSettingsSO;
2023-06-29 14:57:11 +08:00
//启动BITApp
2023-06-05 19:57:17 +08:00
BITApp.Start(Application.productName, settings!.appSettings);
AllowCursor = new();
AllowTouchSupport = new();
GameObject = new(nameof(BITApp));
GameObject.AddComponent<BITAppForUnity>();
DontDestroyOnLoad(GameObject);
AllowCursor.AddListener(ToggleCursor);
AllowTouchSupport.AddListener(ToggleTouchSupport);
}
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
}
2023-06-29 14:57:11 +08:00
private void Start()
2023-06-05 19:57:17 +08:00
{
var cancellationToken = gameObject.GetCancellationTokenOnDestroy();
cancellationToken.Register(Exit);
BIT4Log.Log<BITApp>($"已创建{nameof(BITApp)}");
}
2023-06-29 14:57:11 +08:00
private void OnDestroy()
2023-06-05 19:57:17 +08:00
{
IsPlaying = false;
}
2023-06-29 14:57:11 +08:00
private void Update()
{
BITApp.Time.DeltaTime = Time.deltaTime;
BITApp.Time.TimeAsDouble = Time.timeAsDouble;
}
public string GetName()
2023-06-05 19:57:17 +08:00
{
return nameof(BITApp);
}
public object GetDiagnostics()
{
return BITApp.State;
}
}
}