diff --git a/Assets/TEngine/Runtime/UI/IUIController.cs b/Assets/TEngine/Runtime/UI/IUIController.cs index 24c9d965..ea64f810 100644 --- a/Assets/TEngine/Runtime/UI/IUIController.cs +++ b/Assets/TEngine/Runtime/UI/IUIController.cs @@ -2,6 +2,6 @@ { interface IUIController { - void ResigterUIEvent(); + void RegisterUIEvent(); } } \ No newline at end of file diff --git a/Assets/TEngine/Runtime/UI/UIManager.cs b/Assets/TEngine/Runtime/UI/UIManager.cs index c29dc6bb..07123ffc 100644 --- a/Assets/TEngine/Runtime/UI/UIManager.cs +++ b/Assets/TEngine/Runtime/UI/UIManager.cs @@ -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() diff --git a/Assets/TEngine/Runtime/UI/UISys.cs b/Assets/TEngine/Runtime/UI/UISys.cs index 1e2cb29c..f9a42cd9 100644 --- a/Assets/TEngine/Runtime/UI/UISys.cs +++ b/Assets/TEngine/Runtime/UI/UISys.cs @@ -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(); + //AddController(); } private void AddController() 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); } + /// /// 调整 RectTransform 组件中的 Left、Top 属性 /// @@ -129,6 +124,7 @@ namespace UI rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x); rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y); } + /// /// 调整 RectTransform 组件中的 Right、Bottom 属性 /// @@ -141,6 +137,7 @@ namespace UI rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x); rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y); } + /// /// 调整 RectTransform 组件中的 Right、Top 属性 /// @@ -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); } } - -} \ No newline at end of file +} diff --git a/Assets/TEngine/Runtime/UI/UIWindowBase.cs b/Assets/TEngine/Runtime/UI/UIWindowBase.cs index b6b3cee8..1a9cac37 100644 --- a/Assets/TEngine/Runtime/UI/UIWindowBase.cs +++ b/Assets/TEngine/Runtime/UI/UIWindowBase.cs @@ -5,7 +5,7 @@ using UnityEngine; namespace UI { - public class UIWindowBase : UIBase + partial class UIWindowBase : UIBase { /// /// 所属的window diff --git a/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs b/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs new file mode 100644 index 00000000..c51ba77e --- /dev/null +++ b/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs @@ -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(string goPath, bool visible = true) where T : UIWindowWidget, new() + { + var goRootTrans = FindChild(goPath); + if (goRootTrans != null) + { + return CreateWidget(goRootTrans.gameObject, visible); + } + + TLogger.LogError("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 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(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 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(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) + { + 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(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) + { + 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(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; + } + } + } + + public void SafeDestroyDelay(GameObject go, float time) + { + if (go != null) + { + AutoDestroyBehaviour w = + CreateWidget(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(); + } + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs.meta b/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs.meta new file mode 100644 index 00000000..86132f15 --- /dev/null +++ b/Assets/TEngine/Runtime/UI/UIWindowBaseExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 071828a80bfbbe94c8f17ccd66e3a6d1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: