BITFALL/Assets/BITKit/Unity/Scripts/BITAppForUnity.cs

147 lines
4.2 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System;
2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
2023-08-23 01:59:40 +08:00
using System.Diagnostics;
2023-06-08 14:09:50 +08:00
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine.InputSystem;
using UnityEngine.AddressableAssets;
2023-08-23 01:59:40 +08:00
using Debug = UnityEngine.Debug;
2023-06-08 14:09:50 +08:00
namespace BITKit
{
public class MouseNotOverGameViewException : System.Exception
{
public MouseNotOverGameViewException()
{
}
}
public class BITAppForUnity : MonoBehaviour, IDiagnostics
{
2023-08-23 01:59:40 +08:00
[Serializable]
public class OpenUrl:IAction
{
[SerializeField] private string url;
public void Execute()
{
Application.OpenURL(url);
}
}
[RuntimeInitializeOnLoadMethod]
public static void Initialize()
{
stopWatcher = new Stopwatch();
stopWatcher.Start();
}
2023-06-08 14:09:50 +08:00
public struct Path
{
public const string Entity = nameof(Entity);
public const string Services = nameof(Services);
public const string Prefabs = nameof(Prefabs);
}
private static bool IsPlaying { get; set; }
2023-08-12 01:43:24 +08:00
public static bool IsPointerOverUI { get; set; }
2023-06-08 14:09:50 +08:00
public static ValidHandle AllowCursor = new();
public static ValidHandle AllowTouchSupport = new();
public static GameObject GameObject;
2023-08-23 01:59:40 +08:00
private static Stopwatch stopWatcher;
2023-06-08 14:09:50 +08:00
public static void ThrowIfNotPlaying()
{
#if UNITY_EDITOR
if (IsPlaying is false)
{
throw new AppIsNotPlayingException();
}
#endif
}
public static void ThrowIfWindowNotFocus()
{
#if UNITY_EDITOR
2023-08-12 01:43:24 +08:00
var window = UnityEditor.EditorWindow.focusedWindow;
var windowName = window is not null ? window.ToString() : string.Empty;
switch (windowName)
2023-06-08 14:09:50 +08:00
{
2023-08-12 01:43:24 +08:00
case " (UnityEditor.GameView)":
return;
default:
throw new MouseNotOverGameViewException();
2023-06-08 14:09:50 +08:00
}
#endif
}
[RuntimeInitializeOnLoadMethod]
2023-06-17 16:30:53 +08:00
private static void Reload()
2023-06-08 14:09:50 +08:00
{
IsPlaying = true;
BIT4Log.OnLog += Debug.Log;
BIT4Log.OnWarning += Debug.LogWarning;
BIT4Log.OnException += Debug.LogException;
2023-06-17 16:30:53 +08:00
//启动BITApp
2023-10-02 23:24:56 +08:00
BITApp.Start(Application.productName, new BITApp.AppSettings()).Forget();
2023-06-08 14:09:50 +08:00
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]
2023-08-23 01:59:40 +08:00
public static void Exit()
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
BITApp.Stop();
2023-06-08 14:09:50 +08:00
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
2023-08-12 01:43:24 +08:00
private void Start()
2023-06-08 14:09:50 +08:00
{
BIT4Log.Log<BITApp>($"已创建{nameof(BITApp)}");
2023-08-23 01:59:40 +08:00
stopWatcher.Stop();
BIT4Log.Log<BITAppForUnity>($"从初始化到创建{nameof(BITApp)}耗时:{stopWatcher.ElapsedMilliseconds}ms");
2023-06-08 14:09:50 +08:00
}
2023-08-12 01:43:24 +08:00
private void OnDestroy()
2023-06-08 14:09:50 +08:00
{
IsPlaying = false;
2023-08-23 01:59:40 +08:00
Exit();
2023-06-08 14:09:50 +08:00
}
2023-08-12 01:43:24 +08:00
private void Update()
{
BITApp.Time.DeltaTime = Time.deltaTime;
BITApp.Time.TimeAsDouble = Time.timeAsDouble;
}
public string GetName()
2023-06-08 14:09:50 +08:00
{
return nameof(BITApp);
}
public object GetDiagnostics()
{
return BITApp.State;
}
}
}