修复加载问题

This commit is contained in:
yanghua 2025-03-31 17:36:31 +08:00
parent 836b9d50e4
commit 1fac5ecda0
19 changed files with 155 additions and 131 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 739cb8634b1a29c4fa4f3c4210b1c5e5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -13,22 +13,23 @@ public class Bootstrap : SingletonMono<Bootstrap>
protected override void Awake()
{
base.Awake();
uiManager = new UIManager();
uiManager = new UIManager(this);
eventCenter = new EventCenter();
scenesManager = new ScenesManager();
scenesManager = new ScenesManager(this);
DontDestroyOnLoad(gameObject);
}
private void Start()
{
Debug.Log("<color=yellow>按键S显示UI按键H关闭UI按键E控制物体变色</color>");
uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
Debug.Log("<color=yellow>按键S显示UI按键H关闭UI按键E控制物体变色按键V跳场景</color>");
uiManager.ShowPanel<UI_LoadingPanel>(E_UI_Layer.System, (panel) =>
{
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
scenesManager.LoadSceneAsyn(this, "TestScene", () =>
///1 为进度条速度
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 1f);
scenesManager.LoadSceneAsyn("TestScene", () =>
{
Debug.Log("加载场景成功");
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 5f);
});
});
@ -40,7 +41,7 @@ public class Bootstrap : SingletonMono<Bootstrap>
if (Input.GetKeyDown("s"))
{
Debug.Log("Input.GetKeyDown(\"u\")");
uiManager.ShowPanel<UI_TestPanel>(this, E_UI_Layer.Bot, (panel) =>
uiManager.ShowPanel<UI_TestPanel>( E_UI_Layer.Bot, (panel) =>
{
panel.OnInit();
Debug.Log("UI_TestPanel显示成功");

View File

@ -6,7 +6,11 @@ using UnityEngine.SceneManagement;
public class ScenesManager
{
public ScenesManager() { }
private MonoBehaviour _target;
public ScenesManager(MonoBehaviour target)
{
_target = target;
}
/// <summary>
/// 同步加载场景
/// </summary>
@ -25,10 +29,9 @@ public class ScenesManager
/// <param name="sceneName">场景名称</param>
/// <param name="action">委托</param>
/// <param name="loadSceneMode">加载场景方式</param>
public void LoadSceneAsyn(MonoBehaviour taget, string sceneName, UnityAction action = null,
LoadSceneMode loadSceneMode = LoadSceneMode.Single)
public void LoadSceneAsyn(string sceneName, UnityAction action = null)
{
taget.StartCoroutine(ReallyLoadScene(sceneName, action));
_target.StartCoroutine(ReallyLoadScene(sceneName, action));
}
/// <summary>
@ -44,8 +47,8 @@ public class ScenesManager
AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
while (!ao.isDone)
{
action?.Invoke();
yield return null;
}
action?.Invoke();
}
}

View File

@ -21,6 +21,7 @@ public class BasePanel : MonoBehaviour
{
FindChildrenControl<Button>();
FindChildrenControl<Image>();
FindChildrenControl<RawImage>();
FindChildrenControl<Text>();
FindChildrenControl<Toggle>();
FindChildrenControl<Slider>();

View File

@ -4,6 +4,7 @@ using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using static UnityEngine.GraphicsBuffer;
/// <summary>
/// 可以根据需求继续增加层级
@ -52,13 +53,15 @@ public class UIManager
/// </summary>
private readonly Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();
private MonoBehaviour _target;
/// <summary>
/// 构造函数
/// </summary>
public UIManager()
public UIManager(MonoBehaviour target)
{
_target = target;
canvas = ResourcesManager.Load<GameObject>("UI/Base/Canvas").GetComponent<RectTransform>();
GameObject.DontDestroyOnLoad(canvas.gameObject);
layers = new Transform[]
@ -76,7 +79,7 @@ public class UIManager
/// <typeparam name="T">面板类型 面板名称</typeparam>
/// <param name="layer">显示在哪一层</param>
/// <param name="action">加载完成后 要做的</param>
public void ShowPanel<T>(MonoBehaviour target, E_UI_Layer layer = E_UI_Layer.Mid, UnityAction<T> action = null) where T : BasePanel
public void ShowPanel<T>( E_UI_Layer layer = E_UI_Layer.Mid, UnityAction<T> action = null) where T : BasePanel
{
string panelName = typeof(T).Name;
//重复调用问题 1 已经存在的panel
@ -88,7 +91,7 @@ public class UIManager
}
string resourcePath = "UI/UIPanel/" + panelName;
//重复调用 同一帧调用多次问题
ResourcesManager.LoadAsync<GameObject>(target, resourcePath, (panelObj) =>
ResourcesManager.LoadAsync<GameObject>(_target, resourcePath, (panelObj) =>
{
if (panelObj == null)
{

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5a9eaf3675d0fd440b1b9b71ed5b5320
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -16,13 +16,14 @@ public class TestScene1Manager : MonoBehaviour
{
if (Input.GetKeyDown("v"))
{
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(E_UI_Layer.System, (panel) =>
{
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "TestScene", () =>
///2 为进度条速度
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn("TestScene", () =>
{
Debug.Log("加载场景成功");
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 5f);
});
});
}

View File

@ -17,13 +17,13 @@ public class TestSceneManager : MonoBehaviour
{
if (Input.GetKeyDown("v"))
{
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(E_UI_Layer.System, (panel) =>
{
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "TestScene1", () =>
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn("TestScene1", () =>
{
Debug.Log("加载场景成功");
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 5f);
});
});
}

View File

@ -8,7 +8,7 @@ public class UI_LoadingPanel : BasePanel
public Slider loadSlider;
private float currentProgress = 0;
public float targetProgress;
public float speed;
public bool isLoading = false;
protected override void Awake()
{
@ -19,42 +19,41 @@ public class UI_LoadingPanel : BasePanel
public override void ShowMe()
{
base.ShowMe();
Debug.Log("UI_LoadingPanel ShowMe");
Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
public override void HideMe()
{
base.HideMe();
Debug.Log("UI_LoadingPanel HideMe");
loadSlider.value = 0;
currentProgress = 0;
speed = 0;
Bootstrap.Instance.eventCenter.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
private void Update()
{
if (isLoading)
{
if (currentProgress < targetProgress)
if (currentProgress < 0.98f)
{
currentProgress += Time.deltaTime;
if (currentProgress >= targetProgress)
currentProgress = targetProgress;
currentProgress += Time.deltaTime * speed;
loadSlider.value = currentProgress;
}
else
{
isLoading = false;
Bootstrap.Instance.uiManager.HidePanel<UI_LoadingPanel>();
HideMe();
}
}
}
private void UpdateProgress(float progress)
private void UpdateProgress(float speed)
{
isLoading = true;
targetProgress += progress;
this.speed = speed;
}
}

View File

@ -6,12 +6,12 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/Scenes/Init.unity
path: Assets/Scenes/TestScenes/Init.unity
guid: 9fc0d4010bbf28b4594072e72b8655ab
- enabled: 1
path: Assets/Scenes/TestScene.unity
path: Assets/Scenes/TestScenes/TestScene.unity
guid: 4e2c55ee46fa8d2459c2f30c97ccf553
- enabled: 1
path: Assets/Scenes/TestScene1.unity
path: Assets/Scenes/TestScenes/TestScene1.unity
guid: e805b33f7cd608f4d8f843b5e7be28e7
m_configObjects: {}

View File

@ -14,10 +14,10 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_PixelRect:
serializedVersion: 2
x: 2560
y: 43
width: 1920
height: 989
x: 0
y: 43.2
width: 2048
height: 1188.8
m_ShowMode: 4
m_Title: Console
m_RootView: {fileID: 4}
@ -40,11 +40,11 @@ MonoBehaviour:
m_Position:
serializedVersion: 2
x: 0
y: 408
width: 339
height: 531
m_MinSize: {x: 102, y: 121}
m_MaxSize: {x: 4002, y: 4021}
y: 494.4
width: 264
height: 644.4
m_MinSize: {x: 100, y: 100}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 14}
m_Panes:
- {fileID: 14}
@ -67,14 +67,14 @@ MonoBehaviour:
- {fileID: 2}
m_Position:
serializedVersion: 2
x: 806
x: 1060
y: 0
width: 339
height: 939
width: 264
height: 1138.8
m_MinSize: {x: 100, y: 200}
m_MaxSize: {x: 8096, y: 16192}
vertical: 1
controlID: 41
controlID: 15
--- !u!114 &4
MonoBehaviour:
m_ObjectHideFlags: 52
@ -95,8 +95,8 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 1920
height: 989
width: 2048
height: 1188.8
m_MinSize: {x: 875, y: 300}
m_MaxSize: {x: 10000, y: 10000}
m_UseTopView: 1
@ -120,7 +120,7 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 1920
width: 2048
height: 30
m_MinSize: {x: 0, y: 0}
m_MaxSize: {x: 0, y: 0}
@ -146,12 +146,12 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 30
width: 1920
height: 939
width: 2048
height: 1138.8
m_MinSize: {x: 400, y: 200}
m_MaxSize: {x: 32384, y: 16192}
vertical: 0
controlID: 40
controlID: 97
--- !u!114 &7
MonoBehaviour:
m_ObjectHideFlags: 52
@ -168,8 +168,8 @@ MonoBehaviour:
m_Position:
serializedVersion: 2
x: 0
y: 969
width: 1920
y: 1168.8
width: 2048
height: 20
m_MinSize: {x: 0, y: 0}
m_MaxSize: {x: 0, y: 0}
@ -192,12 +192,12 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 806
height: 939
width: 1060
height: 1138.8
m_MinSize: {x: 100, y: 200}
m_MaxSize: {x: 8096, y: 16192}
vertical: 1
controlID: 86
controlID: 98
--- !u!114 &9
MonoBehaviour:
m_ObjectHideFlags: 52
@ -215,10 +215,10 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 339
height: 408
m_MinSize: {x: 202, y: 221}
m_MaxSize: {x: 4002, y: 4021}
width: 264
height: 494.4
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 15}
m_Panes:
- {fileID: 15}
@ -239,10 +239,10 @@ MonoBehaviour:
m_Children: []
m_Position:
serializedVersion: 2
x: 1145
x: 1324
y: 0
width: 343
height: 939
width: 261.59998
height: 1138.8
m_MinSize: {x: 232, y: 271}
m_MaxSize: {x: 10002, y: 10021}
m_ActualView: {fileID: 17}
@ -265,12 +265,12 @@ MonoBehaviour:
m_Children: []
m_Position:
serializedVersion: 2
x: 1488
x: 1585.6
y: 0
width: 432
height: 939
m_MinSize: {x: 276, y: 71}
m_MaxSize: {x: 4001, y: 4021}
width: 462.40002
height: 1138.8
m_MinSize: {x: 275, y: 50}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 16}
m_Panes:
- {fileID: 16}
@ -293,10 +293,10 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 806
height: 407
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
width: 1060
height: 492.8
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 19}
m_Panes:
- {fileID: 19}
@ -318,11 +318,11 @@ MonoBehaviour:
m_Position:
serializedVersion: 2
x: 0
y: 407
width: 806
height: 532
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
y: 492.8
width: 1060
height: 646.00006
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 18}
m_Panes:
- {fileID: 18}
@ -348,10 +348,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 3366
y: 481
width: 337
height: 510
x: 1060
y: 568
width: 262
height: 623.4
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default
@ -377,10 +377,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 3366
y: 73
width: 337
height: 387
x: 1060
y: 73.6
width: 262
height: 473.4
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default
@ -389,9 +389,9 @@ MonoBehaviour:
m_SceneHierarchy:
m_TreeViewState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: 4a580000
m_SelectedIDs: 1c580000
m_LastClickedID: 0
m_ExpandedIDs: 34fbffff
m_ExpandedIDs: 36f4ffff42f4ffff4cf4ffff34fbfffff4ffffff
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
@ -431,14 +431,14 @@ MonoBehaviour:
m_MaxSize: {x: 4000, y: 4000}
m_TitleContent:
m_Text: Inspector
m_Image: {fileID: -2667387946076563598, guid: 0000000000000000d000000000000000, type: 0}
m_Image: {fileID: -440750813802333266, guid: 0000000000000000d000000000000000, type: 0}
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 4048
y: 73
width: 431
height: 918
x: 1585.6
y: 73.6
width: 461.40002
height: 1117.8
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default
@ -477,10 +477,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 3705
y: 73
width: 341
height: 918
x: 1324
y: 73.6
width: 259.59998
height: 1117.8
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default
@ -497,7 +497,7 @@ MonoBehaviour:
m_SkipHidden: 0
m_SearchArea: 1
m_Folders:
- Assets/Scenes
- Assets
m_Globs: []
m_OriginalText:
m_FilterByTypeIntersection: 0
@ -541,21 +541,21 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: ffffffff000000001c5800001e58000020580000
m_ExpandedIDs: ffffffff000000001c580000205800004e580000505800005458000056580000585800005a58000066580000ffffff7f
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
m_OriginalName:
m_Name: AsyncWebReq
m_OriginalName: AsyncWebReq
m_EditFieldRect:
serializedVersion: 2
x: 0
y: 0
width: 0
height: 0
m_UserData: 0
m_UserData: 22960
m_IsWaitingForDelay: 0
m_IsRenaming: 0
m_OriginalEventType: 11
m_OriginalEventType: 0
m_IsRenamingFilename: 1
m_ClientGUIView: {fileID: 10}
m_SearchString:
@ -617,10 +617,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 2560
y: 480
width: 805
height: 511
x: 0
y: 566.4
width: 1059
height: 625.00006
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default
@ -647,10 +647,10 @@ MonoBehaviour:
m_VRangeLocked: 0
hZoomLockedByDefault: 0
vZoomLockedByDefault: 0
m_HBaseRangeMin: -960
m_HBaseRangeMax: 960
m_VBaseRangeMin: -540
m_VBaseRangeMax: 540
m_HBaseRangeMin: -768
m_HBaseRangeMax: 768
m_VBaseRangeMin: -432
m_VBaseRangeMax: 432
m_HAllowExceedBaseRangeMin: 1
m_HAllowExceedBaseRangeMax: 1
m_VAllowExceedBaseRangeMin: 1
@ -668,23 +668,23 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 21
width: 805
height: 490
m_Scale: {x: 0.41927084, y: 0.4192708}
m_Translation: {x: 402.5, y: 245}
width: 1059
height: 604.00006
m_Scale: {x: 0.6894531, y: 0.6894531}
m_Translation: {x: 529.5, y: 302.00003}
m_MarginLeft: 0
m_MarginRight: 0
m_MarginTop: 0
m_MarginBottom: 0
m_LastShownAreaInsideMargins:
serializedVersion: 2
x: -960
y: -584.34784
width: 1920
height: 1168.6957
x: -768
y: -438.02838
width: 1536
height: 876.05676
m_MinimalGUI: 1
m_defaultScale: 0.41927084
m_LastWindowPixelSize: {x: 805, y: 511}
m_defaultScale: 0.6894531
m_LastWindowPixelSize: {x: 1323.75, y: 781.25006}
m_ClearInEditMode: 1
m_NoCameraWarning: 1
m_LowResolutionForAspectRatios: 00000000000000000000
@ -710,10 +710,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 2560
y: 73
width: 805
height: 386
x: 0
y: 73.6
width: 1059
height: 471.8
m_ViewDataDictionary: {fileID: 0}
m_OverlayCanvas:
m_LastAppliedPresetName: Default