UI框架开发中
This commit is contained in:
@@ -13,8 +13,11 @@ public class GameStart : MonoBehaviour
|
||||
}
|
||||
async UniTask LoadSimpleR3Test()
|
||||
{
|
||||
AssetHandle handle = YooAssets.LoadAssetAsync<GameObject>("SimpleR3Test");
|
||||
await handle;
|
||||
GameObject.Instantiate(handle.AssetObject, MainUICanvas.Inst.Medium);
|
||||
UIManager.Inst.ShowUI<SimpleR3Test>("test1", MainUICanvas.Inst.Medium);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test2", MainUICanvas.Inst.Medium);
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test2");
|
||||
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test3");
|
||||
await UIManager.Inst.TestShowUIAsync("FullTest", "test4", MainUICanvas.Inst.Medium,true);
|
||||
UIManager.Inst.TestShowUI("FullTest","test5", MainUICanvas.Inst.Medium);
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/GameScripts/Main/UI.meta
Normal file
8
Assets/GameScripts/Main/UI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43d2406acc71f48448efde6aa488d064
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,16 +2,16 @@ using R3;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SimpleR3Test : MonoBehaviour
|
||||
public class SimpleR3Test : UIBase
|
||||
{
|
||||
public SerializableReactiveProperty<float> value = new(0);
|
||||
|
||||
public InputField inputField;
|
||||
public Scrollbar scrollbar;
|
||||
|
||||
|
||||
void Start()
|
||||
public override void OnCreate()
|
||||
{
|
||||
base.OnCreate();
|
||||
value.BindToScrollbar(scrollbar).AddTo(this);
|
||||
value.BindToInputField(inputField).AddTo(this);
|
||||
}
|
||||
32
Assets/GameScripts/Main/UI/UIBase.cs
Normal file
32
Assets/GameScripts/Main/UI/UIBase.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class UIBase : MonoBehaviour
|
||||
{
|
||||
public RectTransform rectTransform;
|
||||
public UIBase parent;
|
||||
public virtual void OnCreate()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
public virtual void OnShow() { }
|
||||
public virtual void OnHide() { }
|
||||
|
||||
public void SetFull()
|
||||
{
|
||||
rectTransform.anchorMin = Vector2.zero;
|
||||
rectTransform.anchorMax = Vector2.one;
|
||||
rectTransform.offsetMin = Vector2.zero;
|
||||
rectTransform.offsetMax = Vector2.zero;
|
||||
rectTransform.sizeDelta = Vector2.zero;
|
||||
}
|
||||
|
||||
public void SetParent(RectTransform ui, bool isFull = false)
|
||||
{
|
||||
rectTransform.SetParent(ui);
|
||||
rectTransform.localScale = Vector3.one;
|
||||
rectTransform.localPosition = Vector3.zero;
|
||||
rectTransform.localRotation = Quaternion.identity;
|
||||
if (isFull)
|
||||
SetFull();
|
||||
}
|
||||
}
|
||||
2
Assets/GameScripts/Main/UI/UIBase.cs.meta
Normal file
2
Assets/GameScripts/Main/UI/UIBase.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11bbb38035ff49641be77ec96d27de0c
|
||||
137
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
137
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
public class UIManager : Singleton<UIManager>
|
||||
{
|
||||
public Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
|
||||
Stack<UIBase> uiStack = new Stack<UIBase>();
|
||||
|
||||
public T ShowUI<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
{
|
||||
T ui = null;
|
||||
string uiType = typeof(T).Name;
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = LoadUI(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
uiStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
}
|
||||
Debug.Log($"ShowUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
ui.OnShow();
|
||||
return ui;
|
||||
}
|
||||
public async UniTask<T> ShowUIAsync<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
{
|
||||
T ui = null;
|
||||
string uiType = typeof(T).Name;
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = await LoadUIAsync(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
uiStack.Push(ui);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
}
|
||||
Debug.Log($"ShowUIAsync====>name:{uiName} type:{typeof(T).Name}");
|
||||
ui.OnShow();
|
||||
return ui;
|
||||
}
|
||||
T CreateUI<T>(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
|
||||
{
|
||||
GameObject uiObj = GameObject.Instantiate(uiPrefab);
|
||||
uiObj.name = uiName;
|
||||
T ui = uiObj.GetComponent<T>();
|
||||
ui.OnCreate();
|
||||
if (parent) ui.SetParent(parent, isFull);
|
||||
Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
return ui;
|
||||
}
|
||||
GameObject LoadUI(string uiType)
|
||||
{
|
||||
AssetHandle uiHandle = YooAssets.LoadAssetSync<GameObject>(uiType);
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
async UniTask<GameObject> LoadUIAsync(string uiType)
|
||||
{
|
||||
AssetHandle uiHandle = YooAssets.LoadAssetAsync<GameObject>(uiType);
|
||||
await uiHandle.ToUniTask();
|
||||
return (GameObject)uiHandle.AssetObject;
|
||||
}
|
||||
|
||||
#region 用于测试没挂载UIBase的
|
||||
public 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
|
||||
}
|
||||
2
Assets/GameScripts/Main/UI/UIManager.cs.meta
Normal file
2
Assets/GameScripts/Main/UI/UIManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a59f1c308a49e0742878b0c58f906538
|
||||
Reference in New Issue
Block a user