UI
This commit is contained in:
ALEXTANG
2023-04-06 18:01:16 +08:00
parent 04a646d330
commit 1660c53035
3 changed files with 224 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
@@ -14,10 +15,12 @@ namespace TEngine
/// 类型无。 /// 类型无。
/// </summary> /// </summary>
None, None,
/// <summary> /// <summary>
/// 类型Windows。 /// 类型Windows。
/// </summary> /// </summary>
Window, Window,
/// <summary> /// <summary>
/// 类型Widget。 /// 类型Widget。
/// </summary> /// </summary>
@@ -27,7 +30,7 @@ namespace TEngine
/// <summary> /// <summary>
/// UI基类。 /// UI基类。
/// </summary> /// </summary>
public class UIBase:IUIBehaviour public class UIBase : IUIBehaviour
{ {
/// <summary> /// <summary>
/// 所属UI父节点。 /// 所属UI父节点。
@@ -59,6 +62,11 @@ namespace TEngine
/// </summary> /// </summary>
public AssetOperationHandle Handle { protected set; get; } public AssetOperationHandle Handle { protected set; get; }
/// <summary>
/// 资源组数据。
/// </summary>
public AssetGroup AssetGroup { protected set; get; }
/// <summary> /// <summary>
/// 资源是否准备完毕。 /// 资源是否准备完毕。
/// </summary> /// </summary>
@@ -118,6 +126,7 @@ namespace TEngine
/// 是否需要Update /// 是否需要Update
/// </summary> /// </summary>
protected bool HasOverrideUpdate = true; protected bool HasOverrideUpdate = true;
/// <summary> /// <summary>
/// 窗口更新 /// 窗口更新
/// </summary> /// </summary>
@@ -225,7 +234,8 @@ namespace TEngine
#endregion #endregion
#region UIWidget #region UIWidget
public T CreateWidget<T>(string goPath, bool visible = true) where T : UIWidget,new()
public T CreateWidget<T>(string goPath, bool visible = true) where T : UIWidget, new()
{ {
var goRootTrans = FindChild(goPath); var goRootTrans = FindChild(goPath);
@@ -237,7 +247,7 @@ namespace TEngine
return null; return null;
} }
public T CreateWidget<T>(GameObject goRoot, bool visible = true) where T : UIWidget,new() public T CreateWidget<T>(GameObject goRoot, bool visible = true) where T : UIWidget, new()
{ {
var widget = new T(); var widget = new T();
if (widget.Create(this, goRoot, visible)) if (widget.Create(this, goRoot, visible))
@@ -248,6 +258,163 @@ namespace TEngine
return null; return null;
} }
public T CreateWidget<T>(Transform parentTrans, string goPath, bool visible = true) where T : UIWidget, new()
{
var goRootTrans = FindChild(parentTrans, goPath);
if (goRootTrans != null)
{
return CreateWidget<T>(goRootTrans.gameObject, visible);
}
return null;
}
public T CreateWidgetByPath<T>(Transform parentTrans, string assetPath, bool visible = true) where T : UIWidget, new()
{
if (AssetGroup == null)
{
AssetGroup = AssetGroup.Alloc();
}
GameObject goInst = AssetGroup.LoadAsset<GameObject>(assetPath, parentTrans);
return CreateWidget<T>(goInst, visible);
}
public T CreateWidgetByPrefab<T>(GameObject goPrefab, Transform parentTrans = null, bool visible = true) where T : UIWidget, new()
{
var widget = new T();
if (!widget.CreateByPrefab(this, goPrefab, parentTrans, visible))
{
return null;
}
return widget;
}
public T CreateWidgetByType<T>(Transform parentTrans, bool visible = true) where T : UIWidget, new()
{
return CreateWidgetByPath<T>(parentTrans, typeof(T).Name, visible);
}
/// <summary>
/// 调整图标数量。
/// </summary>
public void AdjustIconNum<T>(List<T> listIcon, int tarNum, Transform parent, GameObject prefab = null, string assetPath = "") where T : UIWidget, new()
{
if (listIcon == null)
{
listIcon = new List<T>();
}
if (listIcon.Count < tarNum)
{
int needNum = tarNum - listIcon.Count;
for (int iconIdx = 0; iconIdx < needNum; iconIdx++)
{
T tmpT = prefab == null ? CreateWidgetByType<T>(parent) : CreateWidgetByPrefab<T>(prefab, parent);
listIcon.Add(tmpT);
}
}
else if (listIcon.Count > tarNum)
{
RemoveUnUseItem<T>(listIcon, tarNum);
}
}
/// <summary>
/// 异步调整图标数量。
/// </summary>
/// <param name="listIcon"></param>
/// <param name="tarNum"></param>
/// <param name="parentTrans"></param>
/// <param name="prefab"></param>
/// <param name="assetPath"></param>
/// <param name="maxNumPerFrame"></param>
/// <param name="updateAction"></param>
/// <typeparam name="T"></typeparam>
public void AsyncAdjustIconNum<T>(List<T> listIcon, int tarNum, Transform parentTrans, GameObject prefab = null,
string assetPath = "", int maxNumPerFrame = 5,
Action<T, int> updateAction = null) where T : UIWidget, new()
{
AsyncAdjustIconNumInternal(listIcon, tarNum, parentTrans, maxNumPerFrame, updateAction, prefab, assetPath).Forget();
}
/// <summary>
/// 异步创建接口。
/// </summary>
private async UniTaskVoid AsyncAdjustIconNumInternal<T>(List<T> listIcon, int tarNum, Transform parentTrans, int maxNumPerFrame,
Action<T, int> updateAction, GameObject prefab, string assetPath) where T : UIWidget, new()
{
if (listIcon == null)
{
listIcon = new List<T>();
}
int createCnt = 0;
for (int i = 0; i < tarNum; i++)
{
T tmpT = null;
if (i < listIcon.Count)
{
tmpT = listIcon[i];
}
else
{
if (prefab == null)
{
tmpT = CreateWidgetByPath<T>(parentTrans, assetPath);
}
else
{
tmpT = CreateWidgetByPrefab<T>(prefab, parentTrans);
}
listIcon.Add(tmpT);
}
int index = i;
if (updateAction != null)
{
updateAction(tmpT, index);
}
createCnt++;
if (createCnt >= maxNumPerFrame)
{
createCnt = 0;
await UniTask.Yield();
}
}
if (listIcon.Count > tarNum)
{
RemoveUnUseItem(listIcon, tarNum);
}
}
private void RemoveUnUseItem<T>(List<T> listIcon, int tarNum) where T : UIWidget
{
var removeIcon = new List<T>();
for (int iconIdx = 0; iconIdx < listIcon.Count; iconIdx++)
{
var icon = listIcon[iconIdx];
if (iconIdx >= tarNum)
{
removeIcon.Add(icon);
}
}
for (var index = 0; index < removeIcon.Count; index++)
{
var icon = removeIcon[index];
listIcon.Remove(icon);
icon.OnDestroy();
ListChild.Remove(icon);
UnityEngine.Object.Destroy(icon.gameObject);
}
}
#endregion #endregion
} }
} }

View File

@@ -131,6 +131,22 @@ namespace TEngine
return CreateImp(parentUI, widgetRoot, false, visible); return CreateImp(parentUI, widgetRoot, false, visible);
} }
/// <summary>
/// 根据prefab或者模版来创建新的 widget。
/// </summary>
/// <param name="parentUI"></param>
/// <param name="goPrefab"></param>
/// <param name="parentTrans"></param>
/// <param name="visible"></param>
/// <returns></returns>
public bool CreateByPrefab(UIBase parentUI, GameObject goPrefab, Transform parentTrans, bool visible = true)
{
if (parentTrans == null)
{
parentTrans = parentUI.rectTransform;
}
return CreateImp(parentUI, Object.Instantiate(goPrefab, parentTrans), true, visible);
}
private bool CreateImp(UIBase parentUI, GameObject widgetRoot, bool bindGo, bool visible = true) private bool CreateImp(UIBase parentUI, GameObject widgetRoot, bool bindGo, bool visible = true)
{ {
@@ -171,6 +187,9 @@ namespace TEngine
{ {
return; return;
} }
if (gameObject != null)
{
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true); var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (var index = 0; index < listCanvas.Length; index++) for (var index = 0; index < listCanvas.Length; index++)
{ {
@@ -178,6 +197,7 @@ namespace TEngine
childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIModule.WINDOW_DEEP; childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIModule.WINDOW_DEEP;
} }
} }
}
#endregion #endregion
} }
} }

View File

@@ -248,6 +248,9 @@ namespace TEngine
if (_isCreate == false) if (_isCreate == false)
{ {
_isCreate = true; _isCreate = true;
ScriptGenerator();
BindMemberProperty();
RegisterEvent();
OnCreate(); OnCreate();
} }
} }
@@ -334,6 +337,11 @@ namespace TEngine
RemoveAllUIEvent(); RemoveAllUIEvent();
for (int i = 0; i < ListChild.Count; i++)
{
ListChild[i].OnDestroy();
}
// 注销回调函数 // 注销回调函数
_prepareCallback = null; _prepareCallback = null;
@@ -344,6 +352,12 @@ namespace TEngine
Handle = null; Handle = null;
} }
if (AssetGroup != null)
{
AssetGroup.Release(AssetGroup);
AssetGroup = null;
}
// 销毁面板对象 // 销毁面板对象
if (_panel != null) if (_panel != null)
{ {