mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
UI
UI
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
@@ -14,10 +15,12 @@ namespace TEngine
|
||||
/// 类型无。
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// 类型Windows。
|
||||
/// </summary>
|
||||
Window,
|
||||
|
||||
/// <summary>
|
||||
/// 类型Widget。
|
||||
/// </summary>
|
||||
@@ -27,7 +30,7 @@ namespace TEngine
|
||||
/// <summary>
|
||||
/// UI基类。
|
||||
/// </summary>
|
||||
public class UIBase:IUIBehaviour
|
||||
public class UIBase : IUIBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属UI父节点。
|
||||
@@ -59,6 +62,11 @@ namespace TEngine
|
||||
/// </summary>
|
||||
public AssetOperationHandle Handle { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源组数据。
|
||||
/// </summary>
|
||||
public AssetGroup AssetGroup { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源是否准备完毕。
|
||||
/// </summary>
|
||||
@@ -118,6 +126,7 @@ namespace TEngine
|
||||
/// 是否需要Update
|
||||
/// </summary>
|
||||
protected bool HasOverrideUpdate = true;
|
||||
|
||||
/// <summary>
|
||||
/// 窗口更新
|
||||
/// </summary>
|
||||
@@ -225,7 +234,8 @@ namespace TEngine
|
||||
#endregion
|
||||
|
||||
#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);
|
||||
|
||||
@@ -237,7 +247,7 @@ namespace TEngine
|
||||
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();
|
||||
if (widget.Create(this, goRoot, visible))
|
||||
@@ -248,6 +258,163 @@ namespace TEngine
|
||||
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
|
||||
}
|
||||
}
|
@@ -131,6 +131,22 @@ namespace TEngine
|
||||
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)
|
||||
{
|
||||
@@ -171,6 +187,9 @@ namespace TEngine
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (gameObject != null)
|
||||
{
|
||||
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
|
||||
for (var index = 0; index < listCanvas.Length; index++)
|
||||
{
|
||||
@@ -178,6 +197,7 @@ namespace TEngine
|
||||
childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIModule.WINDOW_DEEP;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -248,6 +248,9 @@ namespace TEngine
|
||||
if (_isCreate == false)
|
||||
{
|
||||
_isCreate = true;
|
||||
ScriptGenerator();
|
||||
BindMemberProperty();
|
||||
RegisterEvent();
|
||||
OnCreate();
|
||||
}
|
||||
}
|
||||
@@ -334,6 +337,11 @@ namespace TEngine
|
||||
|
||||
RemoveAllUIEvent();
|
||||
|
||||
for (int i = 0; i < ListChild.Count; i++)
|
||||
{
|
||||
ListChild[i].OnDestroy();
|
||||
}
|
||||
|
||||
// 注销回调函数
|
||||
_prepareCallback = null;
|
||||
|
||||
@@ -344,6 +352,12 @@ namespace TEngine
|
||||
Handle = null;
|
||||
}
|
||||
|
||||
if (AssetGroup != null)
|
||||
{
|
||||
AssetGroup.Release(AssetGroup);
|
||||
AssetGroup = null;
|
||||
}
|
||||
|
||||
// 销毁面板对象
|
||||
if (_panel != null)
|
||||
{
|
||||
|
Reference in New Issue
Block a user