更新UI框架

更新UI框架
This commit is contained in:
ALEXTANG
2022-07-06 15:33:51 +08:00
parent 1c56a4eaed
commit 0927e18e73
6 changed files with 322 additions and 15 deletions

View File

@@ -2,6 +2,6 @@
{
interface IUIController
{
void ResigterUIEvent();
void RegisterUIEvent();
}
}

View File

@@ -61,9 +61,26 @@ namespace UI
private Transform m_uiManagerTransform;
private Camera m_uiCamera;
public Camera Camera => m_uiCamera;
private bool m_init = false;
public UIManager()
{
InitUiMgr();
}
public override void Active()
{
base.Active();
InitUiMgr();
}
private void InitUiMgr()
{
if (m_init == true)
{
return;
}
m_uiManagerGo = TResources.Load("UI/UIRoot.prefab");
Object.DontDestroyOnLoad(m_uiManagerGo);
m_uiManagerTransform = m_uiManagerGo.transform;
@@ -87,6 +104,8 @@ namespace UI
baseOrder += 1000;
}
CalcCameraRect();
m_init = true;
}
void CalcCameraRect()

View File

@@ -8,18 +8,12 @@ namespace UI
{
public static int DesginWidth
{
get
{
return 750;
}
get { return 750; }
}
public static int DesginHeight
{
get
{
return 1624;
}
get { return 1624; }
}
public static int ScreenWidth;
@@ -54,7 +48,7 @@ namespace UI
private void RegistAllController()
{
//AddController<LoadingUIController>();
//AddController<GameUIController>();
}
private void AddController<T>() where T : IUIController, new()
@@ -75,12 +69,12 @@ namespace UI
m_listController.Add(controller);
controller.ResigterUIEvent();
controller.RegisterUIEvent();
}
public static void ShowTipMsg(string str)
{
GameEventMgr.Instance.Send(TipsEvent.Log,str);
GameEventMgr.Instance.Send(TipsEvent.Log, str);
#if UNITY_EDITOR
TLogger.LogInfo(str);
#endif
@@ -117,6 +111,7 @@ namespace UI
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Left、Top 属性
/// </summary>
@@ -129,6 +124,7 @@ namespace UI
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Right、Bottom 属性
/// </summary>
@@ -141,6 +137,7 @@ namespace UI
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
}
/// <summary>
/// 调整 RectTransform 组件中的 Right、Top 属性
/// </summary>
@@ -153,6 +150,7 @@ namespace UI
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
}
public static void SetCenter(this RectTransform rectTransform, float x = 0, float y = 0)
{
rectTransform.localPosition = new Vector3(0, 0, 0);
@@ -165,5 +163,4 @@ namespace UI
rectTransform.localPosition = new Vector2(x, y);
}
}
}
}

View File

@@ -5,7 +5,7 @@ using UnityEngine;
namespace UI
{
public class UIWindowBase : UIBase
partial class UIWindowBase : UIBase
{
/// <summary>
/// 所属的window

View File

@@ -0,0 +1,280 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
namespace UI
{
partial class UIWindowBase
{
/**
* 创建子控件,
* goPath 控件的gameobject相当于本window的位置
*/
public T CreateWidget<T>(string goPath, bool visible = true) where T : UIWindowWidget, new()
{
var goRootTrans = FindChild(goPath);
if (goRootTrans != null)
{
return CreateWidget<T>(goRootTrans.gameObject, visible);
}
TLogger.LogError("CreateWidget failed, path: {0}, widget type: {1}", goPath, typeof(T).FullName);
return null;
}
public T CreateWidget<T>(Transform parent, string goPath, bool visible = true) where T : UIWindowWidget, new()
{
var goRootTrans = FindChild(parent, goPath);
if (goRootTrans != null)
{
return CreateWidget<T>(goRootTrans.gameObject, visible);
}
return null;
}
public T[] CreateWidgets<T>(Transform parent) where T : UIWindowWidget, new()
{
T[] array = new T[parent.childCount];
for (int i = 0; i < parent.childCount; ++i)
{
Transform child = parent.GetChild(i);
array[i] = CreateWidget<T>(child.gameObject);
}
return array;
}
public T[] CreateWidgets<T>(string path) where T : UIWindowWidget, new()
{
var parent = FindChild(path);
return CreateWidgets<T>(parent);
}
public T CreateWidget<T>(GameObject goRoot, bool visible = true) where T : UIWindowWidget, new()
{
var widget = new T();
if (!widget.Create(this, goRoot, visible))
{
return null;
}
return widget;
}
//public UIWindowWidget CreateWidgetByPrefab(Type type, GameObject goPrefab, Transform parent, bool visible = true)
//{
// var widget = Activator.CreateInstance(type) as UIWindowWidget;
// if (!widget.CreateByPrefab(this, goPrefab, parent, visible))
// {
// return null;
// }
// return widget;
//}
public T CreateWidgetByPrefab<T>(GameObject goPrefab, Transform parent, bool visible = true) where T : UIWindowWidget, new()
{
var widget = new T();
if (!widget.CreateByPrefab(this, goPrefab, parent, visible))
{
return null;
}
return widget;
}
public T CreateWidgetByType<T>(Transform parent, string goPath, bool visible = true) where T : UIWindowWidget, new()
{
var goRootTrans = FindChild(parent, goPath);
if (goRootTrans != null)
{
string resPath = string.Format("UI/{0}", typeof(T).Name);
return CreateWidgetByResPath<T>(resPath, goRootTrans, visible);
}
return null;
}
public T CreateWidgetByType<T>(string goPath, bool visible = true) where T : UIWindowWidget, new()
{
return CreateWidgetByType<T>(transform, goPath, visible);
}
public T CreateWidgetByType<T>(Transform parent, bool visible = true) where T : UIWindowWidget, new()
{
string resPath = string.Format("UI/{0}", typeof(T).Name);
return CreateWidgetByResPath<T>(resPath, parent, visible);
}
//public UIWindowWidget CreateWidgetByType(Type type, Transform parent, bool visible = true)
//{
// string resPath = string.Format("UI/{0}", type.Name);
// return CreateWidgetByResPath(type, resPath, parent, visible);
//}
//public UIWindowWidget CreateWidgetByResPath(Type type, string resPath, Transform parent, bool visible = true)
//{
// var widget = Activator.CreateInstance(type) as UIWindowWidget;
// if (!widget.CreateByPath(resPath, this, parent, visible))
// {
// return null;
// }
// return widget;
//}
public T CreateWidgetByResPath<T>(string resPath, Transform parent, bool visible = true) where T : UIWindowWidget, new()
{
var widget = new T();
if (!widget.CreateByPath(resPath, this, parent, visible))
{
return null;
}
return widget;
}
/// <summary>
/// 调整图标数量
/// </summary>
public void AdjustIconNum<T>(List<T> listIcon, int tarNum, Transform parent, GameObject prefab = null) where T : UIWindowWidget, new()
{
if (listIcon == null)
{
TLogger.LogError("List is null");
return;
}
if (listIcon.Count < tarNum) // 不足则添加
{
T tmpT;
int needNum = tarNum - listIcon.Count;
for (int iconIdx = 0; iconIdx < needNum; iconIdx++)
{
if (prefab == null)
{
tmpT = CreateWidgetByType<T>(parent);
}
else
{
tmpT = CreateWidgetByPrefab<T>(prefab, parent);
}
listIcon.Add(tmpT);
}
}
else if (listIcon.Count > tarNum) // 多则删除
{
RemoveUnuseItem<T>(listIcon, tarNum);
}
}
public void AsyncAdjustIconNum<T>(string name, List<T> listIcon, int tarNum, Transform parent, int maxNumPerFrame = 5,
Action<T, int> updateAction = null, GameObject prefab = null) where T : UIWindowWidget, new()
{
StartCoroutine( AsyncAdjustIconNumIE(listIcon, tarNum, parent, maxNumPerFrame, updateAction, prefab));
}
/// <summary>
/// 异步创建接口maxNumPerFrame单帧最多的创建数量
/// 注意disable的对象无法运行协程
/// </summary>
public IEnumerator AsyncAdjustIconNumIE<T>(List<T> listIcon, int tarNum, Transform parent, int maxNumPerFrame, Action<T, int> updateAction, GameObject prefab) where T : UIWindowWidget, new()
{
if (listIcon == null)
{
TLogger.LogError("List is null");
yield break;
}
int createCnt = 0;
for (int i = 0; i < tarNum; i++)
{
T tmpT;
if (i < listIcon.Count)
{
tmpT = listIcon[i];
}
else
{
if (prefab == null)
{
tmpT = CreateWidgetByType<T>(parent);
}
else
{
tmpT = CreateWidgetByPrefab<T>(prefab, parent);
}
listIcon.Add(tmpT);
}
int index = i;
if (updateAction != null)
{
updateAction(tmpT, index);
}
createCnt++;
if (createCnt >= maxNumPerFrame)
{
createCnt = 0;
yield return null;
}
}
if (listIcon.Count > tarNum) // 多则删除
{
RemoveUnuseItem(listIcon, tarNum);
}
}
private void RemoveUnuseItem<T>(List<T> listIcon, int tarNum) where T : UIWindowWidget, new()
{
for (int i = 0; i < listIcon.Count; i++)
{
var icon = listIcon[i];
if (i >= tarNum)
{
listIcon.RemoveAt(i);
icon.Destroy();
--i;
}
}
}
public void SafeDestroyDelay(GameObject go, float time)
{
if (go != null)
{
AutoDestroyBehaviour w =
CreateWidget<AutoDestroyBehaviour>(go);
if (w != null)
{
w.Init(time);
}
}
}
}
class AutoDestroyBehaviour : UIWindowWidget
{
private float m_timer;
private bool m_isValid;
public void Init(float time)
{
m_timer = time;
m_isValid = true;
}
protected override void OnUpdate()
{
if (!m_isValid)
{
Destroy();
return;
}
m_timer -= GameTime.deltaTime;
if (m_timer <= 0)
{
Destroy();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 071828a80bfbbe94c8f17ccd66e3a6d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: