diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBase.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBase.cs index 1ecfcda1..df187891 100644 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBase.cs +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBase.cs @@ -23,7 +23,7 @@ namespace TEngine.Runtime.UIModule Widget, } - public class UIWindowBase : UIBase + public partial class UIWindowBase : UIBase { /// /// 所属的window diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs new file mode 100644 index 00000000..dff36009 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs @@ -0,0 +1,206 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TEngine.Runtime.UIModule +{ + partial class UIWindowBase + { + public T CreateWidget(string goPath, bool visible = true) where T : UIWindowWidget, new() + { + var goRootTrans = FindChild(goPath); + if (goRootTrans != null) + { + return CreateWidget(goRootTrans.gameObject, visible); + } + + Log.Error("CreateWidget failed, path: {0}, widget type: {1}", goPath, typeof(T).FullName); + return null; + } + + public T CreateWidget(Transform parent,string goPath, bool visible = true) where T : UIWindowWidget, new() + { + var goRootTrans = FindChild(parent,goPath); + if (goRootTrans != null) + { + return CreateWidget(goRootTrans.gameObject, visible); + } + + return null; + } + + public T[] CreateWidgets(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(child.gameObject); + } + return array; + } + + public T[] CreateWidgets(string path) where T : UIWindowWidget, new() + { + var parent = FindChild(path); + return CreateWidgets(parent); + } + + public T CreateWidget(GameObject goRoot, bool visible = true) where T : UIWindowWidget,new() + { + var widget = new T(); + if (!widget.Create(this, goRoot, visible)) + { + return null; + } + + return widget; + } + + public T CreateWidgetByPrefab(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(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(resPath,goRootTrans,visible); + } + return null; + } + + public T CreateWidgetByType(string goPath, bool visible = true) where T : UIWindowWidget, new() + { + return CreateWidgetByType(transform, goPath, visible); + } + + public T CreateWidgetByType(Transform parent, bool visible = true) where T : UIWindowWidget, new() + { + string resPath = string.Format("UI/{0}", typeof(T).Name); + return CreateWidgetByResPath(resPath,parent,visible); + } + + public T CreateWidgetByResPath(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; + } + + public void AdjustIconNum(List listIcon, int tarNum, Transform parent,GameObject prefab = null) where T : UIWindowWidget, new() + { + if(listIcon == null) + { + Log.Fatal("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(parent); + } + else + { + tmpT = CreateWidgetByPrefab(prefab, parent); + } + listIcon.Add(tmpT); + } + } + else if (listIcon.Count > tarNum) // 多则删除 + { + RemoveUnuseItem(listIcon, tarNum); + } + } + + public void AsyncAdjustIconNum(string name,List listIcon, int tarNum, Transform parent, int maxNumPerFrame = 5, + Action updateAction = null,GameObject prefab = null) where T : UIWindowWidget, new() + { + StartCoroutine(AsyncAdjustIconNumIE(listIcon,tarNum,parent,maxNumPerFrame,updateAction,prefab)); + } + + /// + /// 异步创建接口,maxNumPerFrame单帧最多的创建数量 + /// 注意disable的对象无法运行协程 + /// + public IEnumerator AsyncAdjustIconNumIE(List listIcon, int tarNum, Transform parent, int maxNumPerFrame ,Action updateAction,GameObject prefab) where T : UIWindowWidget, new() + { + if (listIcon == null) + { + Log.Fatal("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(parent); + } + else + { + tmpT = CreateWidgetByPrefab(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(List 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; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs.meta new file mode 100644 index 00000000..dc803655 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindowBaseExtension.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 13cbb1a12b1048249c6d1c09baad56a6 +timeCreated: 1663919294 \ No newline at end of file