319 lines
10 KiB
C#
319 lines
10 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using GameBase;
|
|
using GameLogic;
|
|
using TEngine;
|
|
using UnityEngine;
|
|
using UnityEngine.Playables;
|
|
|
|
/// <summary>
|
|
/// 游戏App。
|
|
/// </summary>
|
|
public partial class GameApp : Singleton<GameApp>
|
|
{
|
|
private static List<Assembly> _hotfixAssembly;
|
|
|
|
private static List<ProcedureBase> _procedureList;
|
|
|
|
/// <summary>
|
|
/// 热更域App主入口。
|
|
/// </summary>
|
|
/// <param name="objects"></param>
|
|
public static void Entrance(object[] objects)
|
|
{
|
|
_hotfixAssembly = (List<Assembly>)objects[0];
|
|
Log.Warning("======= 看到此条日志代表你成功运行了热更新代码 =======");
|
|
Log.Warning("======= Entrance GameApp =======");
|
|
Instance.Active();
|
|
Instance.Start();
|
|
Utility.Unity.AddUpdateListener(Instance.Update);
|
|
Utility.Unity.AddFixedUpdateListener(Instance.FixedUpdate);
|
|
Utility.Unity.AddLateUpdateListener(Instance.LateUpdate);
|
|
Utility.Unity.AddDestroyListener(Instance.OnDestroy);
|
|
Utility.Unity.AddOnDrawGizmosListener(Instance.OnDrawGizmos);
|
|
Utility.Unity.AddOnApplicationPauseListener(Instance.OnApplicationPause);
|
|
|
|
_procedureList = new List<ProcedureBase>();
|
|
_procedureList.Add(new GameLogic.OnEnterGameAppProcedure());
|
|
_procedureList.Add(new GameLogic.GameChangeSceneProcedure());
|
|
_procedureList.Add(new GameLogic.GameSceneProcedure());
|
|
GameModule.Procedure.RestartProcedure(_procedureList.ToArray());
|
|
|
|
PlayableAsset playableAsset;
|
|
|
|
Instance.StartGameLogic();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始游戏业务层逻辑。
|
|
/// <remarks>显示UI、加载场景等。</remarks>
|
|
/// </summary>
|
|
private void StartGameLogic()
|
|
{
|
|
GameModule.Audio.UISoundVolume = 0.7f;
|
|
GameModule.UI.ShowUI<UIGameStarForm>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭游戏。
|
|
/// </summary>
|
|
/// <param name="shutdownType">关闭游戏框架类型。</param>
|
|
public static void Shutdown(ShutdownType shutdownType)
|
|
{
|
|
Log.Info("GameApp Shutdown");
|
|
if (shutdownType == ShutdownType.None)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (shutdownType == ShutdownType.Restart)
|
|
{
|
|
Utility.Unity.RemoveUpdateListener(Instance.Update);
|
|
Utility.Unity.RemoveFixedUpdateListener(Instance.FixedUpdate);
|
|
Utility.Unity.RemoveLateUpdateListener(Instance.LateUpdate);
|
|
Utility.Unity.RemoveDestroyListener(Instance.OnDestroy);
|
|
Utility.Unity.RemoveOnDrawGizmosListener(Instance.OnDrawGizmos);
|
|
Utility.Unity.RemoveOnApplicationPauseListener(Instance.OnApplicationPause);
|
|
}
|
|
|
|
SingletonSystem.Release();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
logic.OnStart();
|
|
}
|
|
|
|
if (Application.isEditor == false)
|
|
{
|
|
//Application.onBeforeRender += OnBeforeRender;
|
|
}
|
|
|
|
}
|
|
bool _auto_size = true;
|
|
private void OnBeforeRender()
|
|
{
|
|
if (_auto_size == false) { return; }
|
|
//Screen.currentResolution.width 1440
|
|
//Screen.currentResolution.height 3440
|
|
//1440 3440 (0.00, 0.00) 1796 3380 2.1125
|
|
Vector2 currentScreenSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
|
|
//bool currentFullScreen = Screen.fullScreen;
|
|
var r = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
|
|
|
|
if (Screen.currentResolution.width < Screen.currentResolution.height)
|
|
{
|
|
//2.1125
|
|
//2.075
|
|
float scale = (Screen.currentResolution.height - 60) / 1080.0f;//1.28倍数 1.16
|
|
|
|
float _scale2 = Screen.currentResolution.width / 607.0f;//1.694117647058824
|
|
|
|
|
|
scale = Mathf.Min(scale, _scale2);
|
|
|
|
var w = Mathf.RoundToInt(607.0f * scale);// * 0.95f);
|
|
var h = Mathf.RoundToInt(1080.0f * scale);//* 0.95f);
|
|
|
|
|
|
if (w != Screen.width || h != Screen.height)
|
|
{
|
|
Screen.SetResolution(w, h, false);
|
|
//Debug.LogError(Screen.currentResolution.width + " " + Screen.currentResolution.height+" "+current + " " + w + " " + h+" "+ Screen.height+" "+ Screen.width);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
float scale = (Screen.currentResolution.height - 60) / 607.0f;//1.1625
|
|
float _scale2 = Screen.currentResolution.width / 1080.0f;//1.694
|
|
scale = Mathf.Min(scale, _scale2);
|
|
|
|
var w = Mathf.RoundToInt(607.0f * scale);
|
|
var h = Mathf.RoundToInt(1080.0f * scale);
|
|
|
|
if (w != Screen.width || h != Screen.height)
|
|
{
|
|
Screen.SetResolution(w, h, false);
|
|
//Debug.LogError(Screen.currentResolution.width + " " + Screen.currentResolution.height + " " + current+" "+w+" "+h + " " + Screen.height + " " + Screen.width);
|
|
}
|
|
}
|
|
|
|
//Debug.LogError(Screen.width + "," + Screen.height);
|
|
//if (currentScreenSize != target)
|
|
//{
|
|
// Screen.SetResolution(target.x, target.y, false);
|
|
// Debug.LogError("当前跟目标不相等");
|
|
//}
|
|
}
|
|
private void Update()
|
|
{
|
|
// CheckDisplayChange();
|
|
|
|
if (Input.GetKeyDown(KeyCode.F11))
|
|
{
|
|
OnBeforeRender();
|
|
}
|
|
|
|
|
|
TProfiler.BeginFirstSample("Update");
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
TProfiler.BeginSample(logic.GetType().FullName);
|
|
logic.OnUpdate();
|
|
TProfiler.EndSample();
|
|
}
|
|
TProfiler.EndFirstSample();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
TProfiler.BeginFirstSample("FixedUpdate");
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
TProfiler.BeginSample(logic.GetType().FullName);
|
|
logic.OnFixedUpdate();
|
|
TProfiler.EndSample();
|
|
}
|
|
TProfiler.EndFirstSample();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
TProfiler.BeginFirstSample("LateUpdate");
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
TProfiler.BeginSample(logic.GetType().FullName);
|
|
logic.OnLateUpdate();
|
|
TProfiler.EndSample();
|
|
}
|
|
TProfiler.EndFirstSample();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
logic.OnDestroy();
|
|
}
|
|
Shutdown(ShutdownType.Restart);
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
#if UNITY_EDITOR
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
logic.OnDrawGizmos();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private void OnApplicationPause(bool isPause)
|
|
{
|
|
var listLogic = _listLogicMgr;
|
|
var logicCnt = listLogic.Count;
|
|
for (int i = 0; i < logicCnt; i++)
|
|
{
|
|
var logic = listLogic[i];
|
|
logic.OnApplicationPause(isPause);
|
|
}
|
|
}
|
|
|
|
|
|
#region 窗口自适应的问题
|
|
private Vector2 _baseResolution = new Vector2(679, 1280); // 基准分辨率
|
|
private Resolution _lastResolution;
|
|
private int _lastDisplayIndex;
|
|
private Vector2 _targetResolution;
|
|
private void SetPCWindowResolution()
|
|
{
|
|
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
|
|
AdjustResolutionWithAspectRatio(new Vector2(Screen.width, Screen.height));
|
|
#endif
|
|
}
|
|
|
|
private void CheckDisplayChange()
|
|
{
|
|
Resolution currentResolution = Screen.currentResolution;
|
|
int currentDisplayIndex = GetActiveDisplayIndex();
|
|
|
|
if (currentResolution.width != _lastResolution.width ||
|
|
currentResolution.height != _lastResolution.height ||
|
|
currentDisplayIndex != _lastDisplayIndex)
|
|
{
|
|
// 只有当当前分辨率小于目标分辨率时才更新
|
|
if (currentResolution.width < _targetResolution.x ||
|
|
currentResolution.height < _targetResolution.y)
|
|
{
|
|
OnDisplayChanged(currentDisplayIndex);
|
|
}
|
|
else
|
|
{
|
|
Log.Info("检测到窗口变大,不更新分辨率");
|
|
}
|
|
}
|
|
|
|
_lastResolution = currentResolution;
|
|
_lastDisplayIndex = currentDisplayIndex;
|
|
}
|
|
|
|
private int GetActiveDisplayIndex()
|
|
{
|
|
for (int i = 0; i < Display.displays.Length; i++)
|
|
{
|
|
if (Display.displays[i].active)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return 0; // 如果没有找到活动显示器,返回主显示器索引
|
|
}
|
|
private void OnDisplayChanged(int newDisplayIndex)
|
|
{
|
|
Log.Info($"游戏窗口已移动到显示器 {newDisplayIndex + 1}");
|
|
// 在这里添加你想要执行的逻辑
|
|
SetPCWindowResolution(); // 重新设置窗口分辨率
|
|
}
|
|
|
|
private void AdjustResolutionWithAspectRatio(Vector2 newResolution)
|
|
{
|
|
float scale = Mathf.Min(newResolution.x / _baseResolution.x, newResolution.y / _baseResolution.y);
|
|
Log.Debug($"scale: {scale}");
|
|
int targetWidth = Mathf.RoundToInt(_baseResolution.x * scale);
|
|
int targetHeight = Mathf.RoundToInt(_baseResolution.y * scale);
|
|
|
|
int minWidth = 679 / 2;
|
|
int minHeight = 1280 / 2;
|
|
targetWidth = Mathf.Max(targetWidth, minWidth);
|
|
targetHeight = Mathf.Max(targetHeight, minHeight);
|
|
|
|
if (targetWidth != Screen.width || targetHeight != Screen.height)
|
|
{
|
|
Log.Info($"应用新分辨率:{targetWidth}x{targetHeight}");
|
|
Screen.SetResolution(targetWidth, targetHeight, Screen.fullScreen);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|