111
This commit is contained in:
52
Assets/GameScripts/Main/AssetLoad.cs
Normal file
52
Assets/GameScripts/Main/AssetLoad.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
using Tuan.GameFramework;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
public class AssetLoad :Singleton <AssetLoad>
|
||||
{
|
||||
Dictionary<string, AssetHandle> assetHandles = new Dictionary<string, AssetHandle>();
|
||||
public async UniTask<T> LoadAsync<T>(string name)where T: Object
|
||||
{
|
||||
if (!assetHandles.ContainsKey(name))
|
||||
{
|
||||
AssetHandle handle = YooAssets.LoadAssetAsync<T>(name);
|
||||
await handle.ToUniTask();
|
||||
assetHandles.Add(name, handle);
|
||||
Debug.Log($"AssetLoad:{name}");
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
AssetHandle handle = assetHandles[name];
|
||||
Debug.Log($"AssetLoad:{name}(已缓存handle)");
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
}
|
||||
public T Load<T>(string name) where T : Object
|
||||
{
|
||||
if (!assetHandles.ContainsKey(name))
|
||||
{
|
||||
AssetHandle handle = YooAssets.LoadAssetSync<T>(name);
|
||||
assetHandles.Add(name, handle);
|
||||
Debug.Log($"AssetLoad:{name}");
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
AssetHandle handle = assetHandles[name];
|
||||
Debug.Log($"AssetLoad:{name}(已缓存handle)");
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
}
|
||||
public AssetHandle GetHandle(string name)
|
||||
{
|
||||
if (assetHandles.ContainsKey(name))
|
||||
return assetHandles[name];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/GameScripts/Main/AssetLoad.cs.meta
Normal file
2
Assets/GameScripts/Main/AssetLoad.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44bc8a9986f966a4ebae93b30342d02f
|
||||
45
Assets/GameScripts/Main/GameSceneManager.cs
Normal file
45
Assets/GameScripts/Main/GameSceneManager.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using YooAsset;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Tuan.GameFramework;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
public class GameSceneManager : Singleton<GameSceneManager>
|
||||
{
|
||||
Dictionary<string, SceneHandle> sceneHandles = new Dictionary<string, SceneHandle>();
|
||||
public async UniTask LoadSceneAsync(string sceneName)
|
||||
{
|
||||
SceneHandle sceneHandle = null;
|
||||
LoadingWindow loadingWindow = await UIManager.Inst.ShowUIAsync<LoadingWindow>($"Loading{sceneName}", MainUICanvas.Inst.Medium, true);
|
||||
if (!sceneHandles.ContainsKey(sceneName))
|
||||
{
|
||||
sceneHandle = YooAssets.LoadSceneAsync("Test");
|
||||
await sceneHandle.ToUniTask(loadingWindow);
|
||||
sceneHandles[sceneName] = sceneHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneHandle = sceneHandles[sceneName];
|
||||
}
|
||||
sceneHandle.ActivateScene();
|
||||
}
|
||||
public async void LoadScene(string sceneName)
|
||||
{
|
||||
SceneHandle sceneHandle = null;
|
||||
if (!sceneHandles.ContainsKey(sceneName))
|
||||
{
|
||||
LoadingWindow loadingWindow = await UIManager.Inst.ShowUIAsync<LoadingWindow>($"Loading{sceneName}", MainUICanvas.Inst.Medium, true);
|
||||
sceneHandle = YooAssets.LoadSceneSync("Test");
|
||||
sceneHandles[sceneName] = sceneHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneHandle = sceneHandles[sceneName];
|
||||
}
|
||||
sceneHandle.ActivateScene();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
2
Assets/GameScripts/Main/GameSceneManager.cs.meta
Normal file
2
Assets/GameScripts/Main/GameSceneManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce14e279825befb4e8d30f8929445030
|
||||
@@ -2,6 +2,7 @@ using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Tuan.GameFramework;
|
||||
using Tuan.GameScripts.Preload;
|
||||
using YooAsset;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
@@ -12,8 +13,13 @@ namespace Tuan.GameScripts.Main
|
||||
{
|
||||
Debug.Log("GameStart");
|
||||
GamePreload.Inst.Test("call by GameStart");
|
||||
// _ = FrameSplittingTest(1);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test1", MainUICanvas.Inst.Medium);
|
||||
_ = LoadSceneTest();
|
||||
// _ = FrameSplittingTest(1);
|
||||
SimpleR3Test ui = await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test1", MainUICanvas.Inst.Medium);
|
||||
UIManager.Inst.CloseUI(ui);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test2", MainUICanvas.Inst.Medium);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test3", MainUICanvas.Inst.Medium);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test1");
|
||||
}
|
||||
async UniTask FrameSplittingTest(int delayFrame)
|
||||
{
|
||||
@@ -23,5 +29,10 @@ namespace Tuan.GameScripts.Main
|
||||
await UniTask.DelayFrame(delayFrame);
|
||||
}
|
||||
}
|
||||
async UniTask LoadSceneTest()
|
||||
{
|
||||
PatchEvent.ClosePatchWindow();
|
||||
await GameSceneManager.Inst.LoadSceneAsync("Test");
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Assets/GameScripts/Main/UI/LoadingWindow.cs
Normal file
16
Assets/GameScripts/Main/UI/LoadingWindow.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Tuan.GameScripts.Main;
|
||||
|
||||
public class LoadingWindow : UIBase,IProgress<float>
|
||||
{
|
||||
public Slider slider;
|
||||
public Text progressText;
|
||||
public void Report(float value)
|
||||
{
|
||||
slider.value = value;
|
||||
progressText.text = $"{value * 100:F0}%";
|
||||
Debug.Log($"{gameObject.name};{value}");
|
||||
}
|
||||
}
|
||||
2
Assets/GameScripts/Main/UI/LoadingWindow.cs.meta
Normal file
2
Assets/GameScripts/Main/UI/LoadingWindow.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0cfeb8d9ed2b7d45841727433c72b07
|
||||
@@ -10,8 +10,7 @@ namespace Tuan.GameScripts.Main
|
||||
public class UIManager : Singleton<UIManager>
|
||||
{
|
||||
Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
|
||||
Dictionary<string, AssetHandle> assetHandles = new Dictionary<string, AssetHandle>();
|
||||
Stack<UIBase> uiStack = new Stack<UIBase>();
|
||||
LinkedList<UIBase> uiLinkedList = new LinkedList<UIBase>();
|
||||
|
||||
public T ShowUI<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
{
|
||||
@@ -19,17 +18,19 @@ namespace Tuan.GameScripts.Main
|
||||
string uiType = typeof(T).Name;
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = LoadUI(uiType);
|
||||
GameObject uiPrefab = AssetLoad.Inst.Load<GameObject>(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
uiStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
Debug.Log($"ShowUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
ui.gameObject.SetActive(true);
|
||||
ui.OnShow();
|
||||
uiLinkedList.AddLast(ui);
|
||||
return ui;
|
||||
}
|
||||
public async UniTask<T> ShowUIAsync<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
@@ -38,19 +39,36 @@ namespace Tuan.GameScripts.Main
|
||||
string uiType = typeof(T).Name;
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = await LoadUIAsync(uiType);
|
||||
GameObject uiPrefab = await AssetLoad.Inst.LoadAsync<GameObject>(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
uiStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
Debug.Log($"ShowUIAsync====>name:{uiName} type:{typeof(T).Name}");
|
||||
ui.gameObject.SetActive(true);
|
||||
ui.OnShow();
|
||||
uiLinkedList.AddLast(ui);
|
||||
return ui;
|
||||
}
|
||||
public void CloseUI(string uiName)
|
||||
{
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
return;
|
||||
var ui = openedUIs[uiName];
|
||||
ui.OnHide();
|
||||
ui.gameObject.SetActive(false);
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
public void CloseUI(UIBase ui)
|
||||
{
|
||||
ui.OnHide();
|
||||
ui.gameObject.SetActive(false);
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
T CreateUI<T>(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
{
|
||||
GameObject uiObj = GameObject.Instantiate(uiPrefab);
|
||||
@@ -61,100 +79,5 @@ namespace Tuan.GameScripts.Main
|
||||
Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
return ui;
|
||||
}
|
||||
GameObject LoadUI(string uiType)
|
||||
{
|
||||
if (!assetHandles.ContainsKey(uiType))
|
||||
{
|
||||
AssetHandle uiHandle = YooAssets.LoadAssetSync<GameObject>(uiType);
|
||||
assetHandles.Add(uiType, uiHandle);
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
AssetHandle uiHandle = assetHandles[uiType];
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
}
|
||||
async UniTask<GameObject> LoadUIAsync(string uiType)
|
||||
{
|
||||
if (!assetHandles.ContainsKey(uiType))
|
||||
{
|
||||
AssetHandle uiHandle = YooAssets.LoadAssetAsync<GameObject>(uiType);
|
||||
await uiHandle.ToUniTask();
|
||||
assetHandles.Add(uiType, uiHandle);
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
AssetHandle uiHandle = assetHandles[uiType];
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
}
|
||||
|
||||
#region 用于测试没挂载UIBase的
|
||||
Dictionary<string, GameObject> testOpenedUIs = new Dictionary<string, GameObject>();
|
||||
Stack<GameObject> testUIStack = new Stack<GameObject>();
|
||||
public GameObject TestShowUI(string path, string uiName, RectTransform parent = null, bool isFull = false)
|
||||
{
|
||||
GameObject ui = null;
|
||||
if (!testOpenedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = LoadUI(path);
|
||||
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
|
||||
testOpenedUIs[uiName] = ui;
|
||||
testUIStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = testOpenedUIs[uiName];
|
||||
}
|
||||
Debug.Log($"TestShowUI====>name:{uiName}");
|
||||
return ui;
|
||||
}
|
||||
public async UniTask<GameObject> TestShowUIAsync(string path, string uiName, RectTransform parent = null, bool isFull = false)
|
||||
{
|
||||
GameObject ui = null;
|
||||
if (!testOpenedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = await LoadUIAsync(path);
|
||||
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
|
||||
testOpenedUIs[uiName] = ui;
|
||||
testUIStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = testOpenedUIs[uiName];
|
||||
}
|
||||
Debug.Log($"TestShowUI====>name:{uiName}");
|
||||
return ui;
|
||||
}
|
||||
GameObject TestCreateUI(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false)
|
||||
{
|
||||
GameObject uiObj = GameObject.Instantiate(uiPrefab);
|
||||
uiObj.name = uiName;
|
||||
RectTransform ui = uiObj.GetComponent<RectTransform>();
|
||||
if (parent) TestSetParent(ui, parent, isFull);
|
||||
Debug.Log($"TestCreateUI====>name:{uiName}");
|
||||
return uiObj;
|
||||
}
|
||||
void TestSetFull(RectTransform rectTransform)
|
||||
{
|
||||
rectTransform.anchorMin = Vector2.zero;
|
||||
rectTransform.anchorMax = Vector2.one;
|
||||
rectTransform.offsetMin = Vector2.zero;
|
||||
rectTransform.offsetMax = Vector2.zero;
|
||||
rectTransform.sizeDelta = Vector2.zero;
|
||||
}
|
||||
|
||||
void TestSetParent(RectTransform ui, RectTransform parent, bool isFull = false)
|
||||
{
|
||||
ui.SetParent(parent);
|
||||
ui.localScale = Vector3.one;
|
||||
ui.localPosition = Vector3.zero;
|
||||
ui.localRotation = Quaternion.identity;
|
||||
if (isFull)
|
||||
TestSetFull(ui);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user