Init
This commit is contained in:
19
Assets/GameScripts/Main/UI/LoadingWindow.cs
Normal file
19
Assets/GameScripts/Main/UI/LoadingWindow.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace 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
|
||||
19
Assets/GameScripts/Main/UI/SimpleR3Test.cs
Normal file
19
Assets/GameScripts/Main/UI/SimpleR3Test.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using R3;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
public class SimpleR3Test : UIBase
|
||||
{
|
||||
public SerializableReactiveProperty<float> value = new(0);
|
||||
|
||||
public InputField inputField;
|
||||
public Scrollbar scrollbar;
|
||||
|
||||
public override void 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
|
||||
41
Assets/GameScripts/Main/UI/UIBase.cs
Normal file
41
Assets/GameScripts/Main/UI/UIBase.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
public class UIBase : MonoBehaviour
|
||||
{
|
||||
public RectTransform rectTransform
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_rectTransform == null)
|
||||
_rectTransform = GetComponent<RectTransform>();
|
||||
return _rectTransform;
|
||||
}
|
||||
}
|
||||
RectTransform _rectTransform;
|
||||
public UIBase parent;
|
||||
public virtual void OnCreate() { }
|
||||
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,false);
|
||||
//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
|
||||
85
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
85
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Tuan.GameFramework;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
namespace Tuan.GameScripts.Main
|
||||
{
|
||||
public class UIManager : Singleton<UIManager>
|
||||
{
|
||||
Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
|
||||
LinkedList<UIBase> uiLinkedList = new LinkedList<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 = AssetLoader.Inst.Load<GameObject>(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
Debug.Log($"UIManager.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
|
||||
{
|
||||
T ui = null;
|
||||
string uiType = typeof(T).Name;
|
||||
if (!openedUIs.ContainsKey(uiName))
|
||||
{
|
||||
GameObject uiPrefab = await AssetLoader.Inst.LoadAsync<GameObject>(uiType);
|
||||
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
||||
openedUIs[uiName] = ui;
|
||||
}
|
||||
else
|
||||
{
|
||||
ui = openedUIs[uiName] as T;
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
Debug.Log($"UIManager.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];
|
||||
Debug.Log($"UIManager.CloseUI====>name:{uiName} type:{ui}");
|
||||
ui.OnHide();
|
||||
ui.gameObject.SetActive(false);
|
||||
uiLinkedList.Remove(ui);
|
||||
}
|
||||
public void CloseUI(UIBase ui)
|
||||
{
|
||||
if (!openedUIs.ContainsKey(ui.gameObject.name)) return;
|
||||
Debug.Log($"UIManager.CloseUI====>name:{ui.gameObject.name} type:{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);
|
||||
uiObj.name = uiName;
|
||||
T ui = uiObj.GetComponent<T>();
|
||||
ui.OnCreate();
|
||||
if (parent) ui.SetParent(parent, isFull);
|
||||
Debug.Log($"UIManager.CreateUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
return ui;
|
||||
}
|
||||
}
|
||||
}
|
||||
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 Tuan.GameScripts.Main
|
||||
{
|
||||
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 SubscribeToInputField(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.SubscribeToInputField(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