UI框架开发中
This commit is contained in:
18
Assets/GameScripts/Main/UI/SimpleR3Test.cs
Normal file
18
Assets/GameScripts/Main/UI/SimpleR3Test.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using R3;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SimpleR3Test : UIBase
|
||||
{
|
||||
public SerializableReactiveProperty<float> value = new(0);
|
||||
|
||||
public InputField inputField;
|
||||
public Scrollbar scrollbar;
|
||||
|
||||
public override void OnCreate()
|
||||
{
|
||||
base.OnCreate();
|
||||
value.BindToScrollbar(scrollbar).AddTo(this);
|
||||
value.BindToInputField(inputField).AddTo(this);
|
||||
}
|
||||
}
|
||||
2
Assets/GameScripts/Main/UI/SimpleR3Test.cs.meta
Normal file
2
Assets/GameScripts/Main/UI/SimpleR3Test.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ab3d77b09181e94e9d6303d5dd80174
|
||||
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
|
||||
51
Assets/GameScripts/Main/UI/UnityUIBindings.cs
Normal file
51
Assets/GameScripts/Main/UI/UnityUIBindings.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using R3;
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace R3
|
||||
{
|
||||
public static class UnityUIBindings
|
||||
{
|
||||
public static IDisposable SubscribeToScrollbar(this ReactiveProperty<float> source, Scrollbar scrollbar)
|
||||
{
|
||||
return source.Subscribe(scrollbar, static (x, s) => s.value = x);
|
||||
}
|
||||
public static IDisposable SubscribeToText(this ReactiveProperty<float> source, InputField inputField)
|
||||
{
|
||||
return source.Subscribe(inputField, static (x, t) => t.text = x.ToString());
|
||||
}
|
||||
public static IDisposable BindToScrollbar(this ReactiveProperty<float> source, Scrollbar scrollbar)
|
||||
{
|
||||
var disposable = new CompositeDisposable();
|
||||
|
||||
// 值 → Scrollbar
|
||||
source.SubscribeToScrollbar(scrollbar).AddTo(disposable);
|
||||
|
||||
// Scrollbar → 值
|
||||
scrollbar.OnValueChangedAsObservable().Subscribe(_value => { source.Value = _value; }).AddTo(disposable);
|
||||
|
||||
return disposable;
|
||||
}
|
||||
public static IDisposable BindToInputField(this ReactiveProperty<float> source, InputField inputField)
|
||||
{
|
||||
var disposable = new CompositeDisposable();
|
||||
|
||||
// 值 → InputField
|
||||
source.SubscribeToText(inputField).AddTo(disposable);
|
||||
|
||||
// InputField → 值
|
||||
inputField.OnValueChangedAsObservable().Subscribe(_value =>
|
||||
{
|
||||
float result;
|
||||
if (float.TryParse(_value,out result))
|
||||
source.Value = result;
|
||||
}).AddTo(disposable);
|
||||
|
||||
return disposable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
2
Assets/GameScripts/Main/UI/UnityUIBindings.cs.meta
Normal file
2
Assets/GameScripts/Main/UI/UnityUIBindings.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f37692bbbf2a11c48b4cb9f3a32a4a77
|
||||
Reference in New Issue
Block a user