(client) feat:实现热重载
This commit is contained in:
@ -2,6 +2,7 @@ using System.Collections;
|
||||
using Logging;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
@ -58,11 +59,19 @@ namespace Base
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (!Program.Instance.needLoad)
|
||||
return;
|
||||
Base.Setting.Instance.Init();
|
||||
#if !DEBUG
|
||||
duration = Base.Setting.Instance.progressStepDuration;
|
||||
fadeDuration = Base.Setting.Instance.exitAnimationDuration;
|
||||
duration = Base.Setting.Instance.CurrentSettings.progressStepDuration;
|
||||
fadeDuration = Base.Setting.Instance.CurrentSettings.exitAnimationDuration;
|
||||
#endif
|
||||
Load();
|
||||
Program.Instance.needLoad = false;
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
loadingUI.SetActive(true);
|
||||
textColor = describeText.color;
|
||||
StartCoroutine(LoadAllManagers());
|
||||
@ -72,17 +81,35 @@ namespace Base
|
||||
{
|
||||
for (var i = 0; i < _loadingSteps.Length; i++)
|
||||
{
|
||||
// 更新描述文本
|
||||
// 更新描述文本(放在try外部)
|
||||
describeText.text = _loadingSteps[i];
|
||||
|
||||
// 获取当前阶段的目标进度
|
||||
var targetProgress = (float)(i + 1) / _loadingSteps.Length;
|
||||
|
||||
// 平滑过渡到下一个阶段
|
||||
// 平滑过渡到下一个阶段(放在try外部)
|
||||
yield return SmoothTransitionTo(targetProgress);
|
||||
|
||||
// 初始化对应的管理器
|
||||
switch (i)
|
||||
// 初始化对应的管理器(使用单独的方法处理可能抛出的异常)
|
||||
yield return InitializeManagerSafely(i);
|
||||
}
|
||||
|
||||
// 加载完成后的处理
|
||||
describeText.text = "加载完成!";
|
||||
Progress = 1f;
|
||||
|
||||
// 开始渐隐效果
|
||||
yield return FadeOutProgressBar();
|
||||
}
|
||||
|
||||
private IEnumerator InitializeManagerSafely(int stepIndex)
|
||||
{
|
||||
bool initSuccess = false;
|
||||
System.Exception initException = null;
|
||||
|
||||
try
|
||||
{
|
||||
switch (stepIndex)
|
||||
{
|
||||
case 0:
|
||||
UnityLogger.Init();
|
||||
@ -103,16 +130,25 @@ namespace Base
|
||||
Managers.ItemResourceManager.Instance.Init();
|
||||
break;
|
||||
}
|
||||
initSuccess = true;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
initException = ex;
|
||||
}
|
||||
|
||||
// 加载完成后的处理
|
||||
describeText.text = "加载完成!";
|
||||
Progress = 1f;
|
||||
if (!initSuccess && initException != null)
|
||||
{
|
||||
Debug.LogError($"初始化第 {stepIndex + 1} 个管理器时出错: {initException.Message}\n{initException.StackTrace}");
|
||||
describeText.text = $"{_loadingSteps[stepIndex]} (初始化失败)";
|
||||
|
||||
// 这里可以添加重试逻辑
|
||||
// yield return RetryInitialize(stepIndex);
|
||||
}
|
||||
|
||||
// 开始渐隐效果
|
||||
yield return FadeOutProgressBar();
|
||||
// 确保协程继续执行
|
||||
yield return null;
|
||||
}
|
||||
|
||||
private IEnumerator SmoothTransitionTo(float targetProgress)
|
||||
{
|
||||
var startProgress = _currentProgress;
|
||||
@ -146,5 +182,10 @@ namespace Base
|
||||
Opacity = 0f;
|
||||
loadingUI.SetActive(false);
|
||||
}
|
||||
|
||||
public static void ToScene(string scene)
|
||||
{
|
||||
SceneManager.LoadScene(scene);
|
||||
}
|
||||
}
|
||||
}
|
@ -64,6 +64,11 @@ namespace Base
|
||||
ApplyWindowSettings();
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
{
|
||||
ApplyAudioSettings();
|
||||
ApplyWindowSettings();
|
||||
}
|
||||
// 应用音频设置
|
||||
private void ApplyAudioSettings()
|
||||
{
|
||||
|
@ -121,6 +121,21 @@ namespace Base
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
public void Show(string uiName)
|
||||
{
|
||||
foreach (var window in _allWindows)
|
||||
{
|
||||
if (window.name == uiName)
|
||||
{
|
||||
Show(window);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Debug.LogWarning($"未找到窗口{uiName}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 公开的隐藏窗口方法
|
||||
/// </summary>
|
||||
@ -133,7 +148,25 @@ namespace Base
|
||||
windowToHide.Hide();
|
||||
needUpdate = true;
|
||||
}
|
||||
public void Hide(string uiName)
|
||||
{
|
||||
foreach (var visibleWindow in _visibleWindows)
|
||||
{
|
||||
if (visibleWindow.name == uiName)
|
||||
{
|
||||
Hide(visibleWindow);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void HideAll()
|
||||
{
|
||||
foreach (var visibleWindow in _visibleWindows)
|
||||
{
|
||||
Hide(visibleWindow);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据当前所有可见窗口的 needPause 属性来更新游戏时钟的暂停状态
|
||||
/// </summary>
|
||||
|
@ -37,7 +37,7 @@ namespace Managers
|
||||
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
|
||||
var path = Path.Combine(pack.packRootPath, ima.path);
|
||||
var texture = Configs.ConfigProcessor.LoadTextureByIO(path);
|
||||
if (texture == null)
|
||||
if (!texture)
|
||||
continue;
|
||||
|
||||
var packId = ima.packID;
|
||||
|
@ -13,8 +13,15 @@ namespace Map
|
||||
public void Start()
|
||||
{
|
||||
var perline= Utils.PerlinNoise.Instance;
|
||||
int size = 100;
|
||||
var size = 100;
|
||||
baseLevel.InitializeData(size, size);
|
||||
for (var i = 0; i < size; i++) {
|
||||
for (var j = 0; j < size; j++) {
|
||||
var dx = i - size/2;
|
||||
var dy = j - size/2;
|
||||
baseLevel.mapData[i][j] = (int)(Math.Sqrt(dx*dx + dy*dy) / 5) & 1;
|
||||
}
|
||||
}
|
||||
baseLevel.RefreshAllTiles();
|
||||
}
|
||||
|
||||
|
43
Client/Assets/Scripts/Prefab/HoverButtonPrefab.cs
Normal file
43
Client/Assets/Scripts/Prefab/HoverButtonPrefab.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
// [RequireComponent(typeof(CircleCollider2D))]
|
||||
public class HoverButtonPrefab:MonoBehaviour
|
||||
{
|
||||
public TMP_Text text;
|
||||
public Button button;
|
||||
// public CircleCollider2D circleCollider;
|
||||
|
||||
public float startRadius = 250;
|
||||
public float endRadius = 100;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// circleCollider.radius=startRadius;
|
||||
}
|
||||
|
||||
public void OnMouseOver()
|
||||
{
|
||||
var dir= (Input.mousePosition - transform.position).magnitude;
|
||||
|
||||
var color = button.image.color;
|
||||
color.a = Mathf.Min((startRadius - dir) / (startRadius - endRadius), 1);
|
||||
button.image.color = color;
|
||||
}
|
||||
|
||||
public void OnMouseEnter()
|
||||
{
|
||||
button.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void OnMouseExit()
|
||||
{
|
||||
button.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/HoverButtonPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/HoverButtonPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cda5c2669c504f77bb60cdd9ce591e63
|
||||
timeCreated: 1755663454
|
@ -1,18 +1,7 @@
|
||||
using Logging;
|
||||
using UnityEngine;
|
||||
|
||||
public class Program : MonoBehaviour
|
||||
public class Program : Utils.Singleton<Program>
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
UnityLogger.Init();
|
||||
Managers.DefineManager.Instance.Init();
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
Managers.TileManager.Instance.Init();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Debug.Log("Starting game...");
|
||||
}
|
||||
public bool needLoad = true;
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
@ -22,12 +24,19 @@ namespace UI
|
||||
|
||||
private void Init()
|
||||
{
|
||||
InitReloadGameButton();
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
InitMonster();
|
||||
InitBuilding();
|
||||
}
|
||||
|
||||
private void InitReloadGameButton()
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = "热重载Def";
|
||||
button.AddListener(HotReload);
|
||||
}
|
||||
|
||||
private void InitEvent()
|
||||
{
|
||||
@ -162,6 +171,13 @@ namespace UI
|
||||
entityPlacementUI.snapEnabled = true;
|
||||
Base.UIInputControl.Instance.Show(entityPlacementUI);
|
||||
}
|
||||
|
||||
private void HotReload()
|
||||
{
|
||||
UIInputControl.Instance.HideAll();
|
||||
Program.Instance.needLoad = true;
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@ -9,7 +11,7 @@ namespace UI
|
||||
Base.UIInputControl.Instance.Hide(this);
|
||||
}
|
||||
|
||||
public void ExitButton()
|
||||
public static void ExitButton()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false; // 停止编辑器播放模式
|
||||
@ -18,9 +20,16 @@ namespace UI
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SettingsButton()
|
||||
public static void SettingsButton()
|
||||
{
|
||||
|
||||
UIInputControl.Instance.Show("SettingUI");
|
||||
}
|
||||
|
||||
public static void ReturnMainMenu()
|
||||
{
|
||||
if (SceneManager.GetActiveScene().buildIndex == 0)
|
||||
return;
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
}
|
||||
}
|
72
Client/Assets/Scripts/UI/SettingUI.cs
Normal file
72
Client/Assets/Scripts/UI/SettingUI.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System.Globalization;
|
||||
using Base;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class SettingUI : UIBase
|
||||
{
|
||||
[SerializeField] private Scrollbar globalVolume;
|
||||
[SerializeField] private Toggle developerMode;
|
||||
[SerializeField] private Toggle friendlyFire;
|
||||
[SerializeField] private TMP_Dropdown windowMode;
|
||||
[SerializeField] private TMP_Dropdown windowResolution;
|
||||
[SerializeField] private TMP_InputField progressStepDuration;
|
||||
[SerializeField] private TMP_InputField exitAnimationDuration;
|
||||
|
||||
Base.Setting.GameSettings currentSettings;
|
||||
|
||||
public override void Show()
|
||||
{
|
||||
base.Show();
|
||||
currentSettings = Base.Setting.Instance.CurrentSettings;
|
||||
globalVolume.value = currentSettings.globalVolume;
|
||||
developerMode.isOn = currentSettings.developerMode;
|
||||
friendlyFire.isOn = currentSettings.friendlyFire;
|
||||
|
||||
progressStepDuration.text = currentSettings.progressStepDuration.ToString(CultureInfo.InvariantCulture);
|
||||
exitAnimationDuration.text = currentSettings.exitAnimationDuration.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
windowResolution.ClearOptions();
|
||||
var options = new System.Collections.Generic.List<TMP_Dropdown.OptionData>();
|
||||
foreach (var resolution in Base.Setting.CommonResolutions)
|
||||
{
|
||||
options.Add(new TMP_Dropdown.OptionData(resolution.ToString()));
|
||||
}
|
||||
windowResolution.AddOptions(options);
|
||||
if (currentSettings.windowResolution != null)
|
||||
{
|
||||
int resolutionIndex = System.Array.FindIndex(Base.Setting.CommonResolutions, r => r == currentSettings.windowResolution);
|
||||
windowResolution.value = resolutionIndex >= 0 ? resolutionIndex : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
windowResolution.value = 0;
|
||||
}
|
||||
}
|
||||
public void CancelSettings()
|
||||
{
|
||||
UIInputControl.Instance.Hide(this);
|
||||
}
|
||||
public void ApplySettings()
|
||||
{
|
||||
currentSettings.globalVolume = globalVolume.value;
|
||||
currentSettings.developerMode = developerMode.isOn;
|
||||
currentSettings.friendlyFire = friendlyFire.isOn;
|
||||
currentSettings.currentWindowMode = (Base.Setting.WindowMode)windowMode.value;
|
||||
currentSettings.windowResolution = Base.Setting.CommonResolutions[windowResolution.value];
|
||||
currentSettings.progressStepDuration = float.Parse(progressStepDuration.text, CultureInfo.InvariantCulture);
|
||||
currentSettings.exitAnimationDuration = float.Parse(exitAnimationDuration.text, CultureInfo.InvariantCulture);
|
||||
Base.Setting.Instance.CurrentSettings = currentSettings;
|
||||
Base.Setting.Instance.Apply();
|
||||
}
|
||||
public void EnterSetting()
|
||||
{
|
||||
ApplySettings();
|
||||
UIInputControl.Instance.Hide(this);
|
||||
Base.Setting.Instance.SaveSettings();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/SettingUI.cs.meta
Normal file
3
Client/Assets/Scripts/UI/SettingUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7f4127efa613491894a9b1a7e9918a26
|
||||
timeCreated: 1755600172
|
Reference in New Issue
Block a user