diff --git a/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs b/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs new file mode 100644 index 00000000..dffeecac --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs @@ -0,0 +1,300 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngineInternal; +using Object = UnityEngine.Object; + +namespace TEngine.Runtime +{ + /// + /// 封装Unity相关的一些通用接口 + /// + public class UnityUtil + { + [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] + public static Component AddMonoBehaviour(Type type, GameObject go) + { + var comp = go.GetComponent(type); + if (comp == null) + { + comp = go.AddComponent(type); + } + + return comp; + } + + public static T AddMonoBehaviour(Component comp) where T : Component + { + var ret = comp.GetComponent(); + if (ret == null) + { + ret = comp.gameObject.AddComponent(); + } + + return ret; + } + + public static T AddMonoBehaviour(GameObject go) where T : Component + { + var comp = go.GetComponent(); + if (comp == null) + { + comp = go.AddComponent(); + } + + return comp; + } + + [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] + public static void RmvMonoBehaviour(Type type, GameObject go) + { + var comp = go.GetComponent(type); + if (comp != null) + { + UnityEngine.Object.Destroy(comp); + } + } + + public static void RmvMonoBehaviour(GameObject go) where T : Component + { + var comp = go.GetComponent(); + if (comp != null) + { + UnityEngine.Object.Destroy(comp); + } + } + + + public static Transform FindChild(Transform transform, string path) + { + var findTrans = transform.Find(path); + if (findTrans != null) + { + return findTrans; + } + + return null; + } + + public static Transform FindChildByName(Transform transform, string name) + { + if (transform == null) + { + return null; + } + + for (int i = 0; i < transform.childCount; i++) + { + var childTrans = transform.GetChild(i); + if (childTrans.name == name) + { + return childTrans; + } + + var find = FindChildByName(childTrans, name); + if (find != null) + { + return find; + } + } + + return null; + } + + [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] + public static Component FindChildComponent(Type type, Transform transform, string path) + { + var findTrans = transform.Find(path); + if (findTrans != null) + { + return findTrans.gameObject.GetComponent(type); + } + + return null; + } + + public static T FindChildComponent(Transform transform, string path) where T : Component + { + var findTrans = transform.Find(path); + if (findTrans != null) + { + return findTrans.gameObject.GetComponent(); + } + + return null; + } + + public static void SetLayer(GameObject go, int layer) + { + if (go == null) + { + return; + } + + SetLayer(go.transform, layer); + } + + public static void SetLayer(Transform trans, int layer) + { + if (trans == null) + { + return; + } + + trans.gameObject.layer = layer; + for (int i = 0, imax = trans.childCount; i < imax; ++i) + { + Transform child = trans.GetChild(i); + SetLayer(child, layer); + } + } + + public static int RandomRangeInt(int min, int max) + { + return UnityEngine.Random.Range(min, max); + } + + public static float RandomRangeFloat(float min, float max) + { + return UnityEngine.Random.Range(min, max); + } + + public static Vector2 RandomInsideCircle(float radius) + { + return UnityEngine.Random.insideUnitCircle * radius; + } + + [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] + public static Array CreateUnityArray(Type type, int length) + { + return Array.CreateInstance(type, length); + } + + public static T[] CreateUnityArray(int length) + { + return new T[length]; + } + + + public static GameObject Instantiate(GameObject go) + { + if (go != null) + { + return UnityEngine.GameObject.Instantiate(go); + } + + return null; + } + + public static int GetHashCodeByString(string str) + { + return str.GetHashCode(); + } + + public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) + { + return Physics.Raycast(ray, out hitInfo, maxDistance, layerMask); + } + + public static List GetRegexMatchGroups(string pattern, string input) + { + List list = new List(); + var regexLink = new Regex(pattern); + var links = regexLink.Match(input); + for (var i = 0; i < links.Groups.Count; ++i) + { + list.Add(links.Groups[i].Value); + } + + return list; + } + + public static void SetMaterialVector3(Material mat, int nameId, Vector3 val) + { + mat.SetVector(nameId, val); + } + + public static void GetVectorData(Vector3 val, out float x, out float y, out float z) + { + x = val.x; + y = val.y; + z = val.z; + } + + public static void GetVector2Data(Vector2 val, out float x, out float y) + { + x = val.x; + y = val.y; + } + + public static bool GetTouchByFingerId(int fingerId, out Touch findTouch) + { + var finded = false; + var touchCnt = Input.touchCount; + + findTouch = new Touch(); + for (int i = 0; i < touchCnt; i++) + { + var touch = Input.GetTouch(i); + if (touch.fingerId == fingerId) + { + findTouch = touch; + finded = true; + break; + } + } + + return finded; + } + + public static bool SetAnimatorController(GameObject go, string resPath) + { + //RuntimeAnimatorController rac = (RuntimeAnimatorController)ResourcesManager.Instance.Load(resPath); + //if (rac == null) + //{ + // Debug.Log("GetAnimator failed path: " + resPath); + // return false; + //} + + //var ani = go.GetComponentInChildren(true); + + //if (ani != null) + //{ + // ani.runtimeAnimatorController = rac; + // return true; + //} + + return false; + } + + public static void SetGameObjectActive(GameObject go, bool active) + { + if (go != null && go.activeSelf != active) + { + go.SetActive(active); + } + } + + public static T[] GetComponentsInChildren(GameObject go) where T : UIBehaviour + { + if (go != null) + { + return go.GetComponentsInChildren(); + } + + return null; + } + + public static T GetComponent(GameObject go) + { + if (go != null) + { + return go.GetComponent(); + } + + return default(T); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs.meta b/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs.meta new file mode 100644 index 00000000..b4427d6b --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Utility/UnityUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bdc9d542a7834f14881e0b0ea544ca01 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts.meta new file mode 100644 index 00000000..3f2daf29 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4730d132ebcf0b34a94c75287e582f9a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr.meta new file mode 100644 index 00000000..2a2eb77a --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 144caac249909404f9af09b2998611b8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs new file mode 100644 index 00000000..a54614e4 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs @@ -0,0 +1,7 @@ +namespace TEngine.Runtime.UIModule +{ + public interface IUIController + { + void RegisterUIEvent(); + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs.meta new file mode 100644 index 00000000..63a1e063 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0801c753191b47a08c37d1f5dd82e38b +timeCreated: 1661502732 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs new file mode 100644 index 00000000..f64ac417 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs @@ -0,0 +1,533 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace TEngine.Runtime.UIModule +{ + public sealed partial class UIManager : TSingleton + { + private Transform m_canvasTrans; + public Canvas m_canvas; + private Dictionary m_allWindow = new Dictionary(); + + /// + /// 不同显示顺序的的窗口列表 + /// + private UIWindowStack[] m_listWindowStack = new UIWindowStack[(int)WindowStackIndex.StackMax]; + + /// + /// 类型到实例的索引 + /// + private static Dictionary m_typeToInst = new Dictionary(); + + /// + /// 类型和资源的绑定关系 + /// + private Dictionary m_uiType2PrefabPath = new Dictionary(); + + private GameObject m_uiManagerGo; + private Transform m_uiManagerTransform; + private Camera m_uiCamera; + public Camera Camera => m_uiCamera; + + public UIManager() + { + m_uiManagerGo = TResources.Load("UI/UIRoot.prefab"); + Object.DontDestroyOnLoad(m_uiManagerGo); + m_uiManagerTransform = m_uiManagerGo.transform; + m_uiCamera = UnityUtil.FindChildComponent(m_uiManagerTransform, "Camera"); + Canvas canvas = m_uiManagerGo.GetComponentInChildren(); + if (canvas != null) + { + m_canvas = canvas; + m_canvasTrans = canvas.transform; + } + + var windowRoot = m_canvasTrans; + + int baseOrder = 1000; + for (int i = 0; i < (int)WindowStackIndex.StackMax; i++) + { + m_listWindowStack[i] = new UIWindowStack(); + m_listWindowStack[i].m_stackIndex = (WindowStackIndex)i; + m_listWindowStack[i].m_baseOrder = baseOrder; + m_listWindowStack[i].m_parentTrans = windowRoot; + baseOrder += 1000; + } + + CalcCameraRect(); + } + + void CalcCameraRect() + { + CanvasScaler canvasScale = m_canvas.GetComponent(); + if (canvasScale != null) + { + canvasScale.referenceResolution = new Vector2(UISys.DesginWidth, UISys.DesginHeight); + float sceneScale = Screen.width / (float)Screen.height; + float designScale = canvasScale.referenceResolution.x / canvasScale.referenceResolution.y; + canvasScale.matchWidthOrHeight = sceneScale > designScale ? 1 : 0; + } + } + + + public void OnUpdate() + { + var allList = GetAllWindowList(); + for (int i = 0; i < allList.Count; i++) + { + UIWindow window = allList[i]; + if (!window.IsDestroyed) + { + window.Update(); + } + } + } + + + private List m_tmpWindowList = new List(); + private bool m_tmpWindowListDirty = true; + + private List GetAllWindowList() + { + if (m_tmpWindowListDirty) + { + m_tmpWindowList.Clear(); + var itr = m_allWindow.GetEnumerator(); + while (itr.MoveNext()) + { + var kv = itr.Current; + m_tmpWindowList.Add(kv.Value); + } + + m_tmpWindowListDirty = false; + } + + return m_tmpWindowList; + } + + #region Methods + + public T ShowWindow(bool isAsync = false) where T : UIWindow, new() + { + string typeName = GetWindowTypeName(); + + T window = GetUIWindowByType(typeName) as T; + if (window == null) + { + window = new T(); + if (!CreateWindowByType(window, typeName, isAsync)) + { + return null; + } + } + + ShowWindow(window, -1); + return window; + } + + public string GetWindowTypeName() + { + string typeName = typeof(T).Name; + return typeName; + } + + public string GetWindowTypeName(UIWindow window) + { + string typeName = window.GetType().Name; + return typeName; + } + + public void CloseWindow() where T : UIWindow + { + string typeName = GetWindowTypeName(); + CloseWindow(typeName); + } + + public T GetWindow() where T : UIWindow + { + string typeName = GetWindowTypeName(); + UIWindow window = GetUIWindowByType(typeName); + if (window != null) + { + return window as T; + } + + return null; + } + + public UIWindow GetUIWindowByType(string typeName) + { + UIWindow window; + if (m_typeToInst.TryGetValue(typeName, out window)) + { + return window; + } + + return null; + } + + #endregion + + private bool CreateWindowByType(UIWindow window, string typeName, bool async = false) + { + //先判断是否有缓存 + GameObject uiGo = null; + + string resPath = string.Format("{0}.prefab", GetUIResourcePath(typeName)); + if (string.IsNullOrEmpty(resPath)) + { + Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, cant find respath", typeName); + return false; + } + + UIWindowStack windowStack = GetUIWindowStack(window); + + if (async) + { + TResources.LoadAsync(resPath, (obj) => + { + if (obj == null) + { + Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", + typeName, resPath); + } + + if (obj != null && windowStack.m_parentTrans != null) + { + obj.transform.SetParent(windowStack.m_parentTrans); + } + + obj.name = typeName; + + window.AllocWindowId(); + + var rectTrans_ = obj.transform as RectTransform; + if (window.NeedCenterUI()) + { + rectTrans_.SetMax(); //localPosition = new Vector3(0, 0, 0); + } + + rectTrans_.localRotation = Quaternion.identity; + rectTrans_.localScale = Vector3.one; + + if (!window.Create(this, obj)) + { + Debug.LogErrorFormat("window create failed, typeName:{0}", typeName); + if (obj != null) + { + Object.Destroy(obj); + obj = null; + } + } + + m_typeToInst[typeName] = window; + m_allWindow[window.WindowId] = window; + m_tmpWindowListDirty = true; + }); + return true; + } + + uiGo = TResources.Load(resPath, windowStack.m_parentTrans); + if (uiGo == null) + { + Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName, + resPath); + //UISys.Mgr.ShowTipMsg(TextDefine.DOWNLOAD_TIP_UI); + //GameEvent.Get().ShowDownloadUI(); + return false; + } + + uiGo.name = typeName; + + window.AllocWindowId(); + + RectTransform rectTrans = uiGo.transform as RectTransform; + if (window.NeedCenterUI()) + { + rectTrans.SetMax(); //localPosition = new Vector3(0, 0, 0); + } + + rectTrans.localRotation = Quaternion.identity; + rectTrans.localScale = Vector3.one; + + if (!window.Create(this, uiGo)) + { + Debug.LogErrorFormat("window create failed, typeName:{0}", typeName); + if (uiGo != null) + { + Object.Destroy(uiGo); + uiGo = null; + } + + return false; + } + + m_typeToInst[typeName] = window; + m_allWindow[window.WindowId] = window; + m_tmpWindowListDirty = true; + return true; + } + + #region MyRegion + + private string GetUIResourcePath(string typeName) + { + string resPath; + if (m_uiType2PrefabPath.TryGetValue(typeName, out resPath)) + { + return resPath; + } + + string path = string.Format("UI/{0}", typeName); + m_uiType2PrefabPath.Add(typeName, path); + return path; + } + + private void ShowWindow(UIWindow window, int showIndex) + { + UIWindowStack windowStack = GetUIWindowStack(window); + List windowList = windowStack.m_windowList; + int resortIndex = -1; + int findIndex = windowList.IndexOf(window.WindowId); + if (findIndex >= 0) + { + windowList.RemoveAt(findIndex); + resortIndex = findIndex; + } + + windowList.Add(window.WindowId); + + ResortStackUI(windowStack, resortIndex); + ShowTopUI(windowStack); + } + + private void ResortStackUI(UIWindowStack stack, int startIdx) + { + if (stack.m_windowList.Count > 0) + { + startIdx = startIdx < 0 ? (stack.m_windowList.Count - 1) : startIdx; + for (int i = startIdx; i < stack.m_windowList.Count; i++) + { + uint windowId = stack.m_windowList[i]; + UIWindow window = FindWindow(windowId); + if (window != null) + { + int order; + if (window.IsFixedSortingOrder) + { + order = stack.m_baseOrder + window.FixedAdditionalOrder; + } + else + { + order = stack.m_baseOrder + i * UIWindow.MaxCanvasSortingOrder; + } + + window.SortingOrder = order; + } + } + } + } + + private void ShowTopUI(UIWindowStack stack) + { + if (stack.m_windowList.Count > 0) + { + bool hasTop = false; + for (int i = stack.m_windowList.Count - 1; i >= 0; i--) + { + uint windowId = stack.m_windowList[i]; + UIWindow window = FindWindow(windowId); + if (window != null) + { + if (!hasTop) + { + hasTop = window.IsFullScreen; + + window.Show(true); + } + else + { + window.Show(false); + } + } + } + } + + OnWindowVisibleChanged(); + } + + private void OnWindowVisibleChanged() + { + bool isFullScreenMaskscene = false; + for (int i = 0; i < m_listWindowStack.Length && !isFullScreenMaskscene; i++) + { + var stack = m_listWindowStack[i]; + if (stack == null) + { + continue; + } + + var listWindow = stack.m_windowList; + for (int k = 0; k < listWindow.Count; k++) + { + var winId = listWindow[k]; + var win = FindWindow(winId); + if (win == null || !win.Visible) + { + continue; + } + + if (win.IsFullScreenMaskScene) + { + isFullScreenMaskscene = true; + break; + } + } + } + //SceneSys.Instance.CameraMgr.SetSceneCameraEnableByUI(true); + } + + public UIWindow FindWindow(uint windowId) + { + UIWindow window; + if (m_allWindow.TryGetValue(windowId, out window)) + { + return window; + } + + return null; + } + + public void CloseWindow(string typeName) + { + UIWindow window = GetUIWindowByType(typeName); + if (window != null) + { + CloseWindow(window); + } + } + + public void CloseWindow(UIWindow window) + { + if (window.IsDestroyed) + { + return; + } + + //刷新窗口order,保证新创建的窗口不会出现重叠 + UIWindowStack windowStack = GetUIWindowStack(window); + + int findIndex = windowStack.FindIndex(window.WindowId); + + //window.Destroy(); + DestroyWindowObject(window); + + ResortStackUI(windowStack, findIndex); + ShowTopUI(windowStack); + } + + private void DestroyWindowObject(UIWindow window) + { + string typeName = window.GetType().Name; + UIWindow typeWindow = null; + if (m_typeToInst.TryGetValue(typeName, out typeWindow) && typeWindow == window) + { + m_typeToInst.Remove(typeName); + } + + uint windowId = window.WindowId; + m_allWindow.Remove(windowId); + UIWindowStack windowStack = GetUIWindowStack(window); + windowStack.m_windowList.Remove(windowId); + window.Destroy(); + m_tmpWindowListDirty = true; + } + + + private int GetIndexByWindowType(UIWindowType windowType) + { + if (windowType == UIWindowType.WindowTop) + { + return (int)WindowStackIndex.StackTop; + } + + return (int)WindowStackIndex.StackNormal; + } + + public UIWindowStack GetUIWindowStack(UIWindow window) + { + int index = GetIndexByWindowType(window.GetWindowType()); + return m_listWindowStack[index]; + } + + public UIWindow GetWindowById(uint windowId) + { + return FindWindow(windowId); + } + + public UIWindowStack GetUIWindowStack(UIWindowType windowType) + { + int index = GetIndexByWindowType(windowType); + return m_listWindowStack[index]; + } + + #endregion + + #region 拓展 + + /// + /// 给控件添加自定义事件监听 + /// + /// 控件对象 + /// 事件类型 + /// 事件的响应函数 + public static void AddCustomEventListener(UIBehaviour control, EventTriggerType type, + UnityAction callback) + { + EventTrigger trigger = control.GetComponent(); + + if (trigger == null) + { + trigger = control.gameObject.AddComponent(); + } + + EventTrigger.Entry entry = new EventTrigger.Entry(); + entry.eventID = type; + entry.callback.AddListener(callback); + + trigger.triggers.Add(entry); + } + + public bool GetMouseDownUiPos(out Vector3 screenPos) + { + bool hadMouseDown = false; + Vector3 mousePos = Vector3.zero; + +#if UNITY_STANDALONE_WIN || UNITY_EDITOR + mousePos = Input.mousePosition; + hadMouseDown = Input.GetMouseButton(0); +#else + if (Input.touchCount > 0) + { + mousePos = Input.GetTouch(0).position; + hadMouseDown = true; + } + else + { + hadMouseDown = false; + } +#endif + Vector2 pos; + RectTransformUtility.ScreenPointToLocalPointInRectangle(m_canvasTrans as RectTransform, Input.mousePosition, + m_uiCamera, out pos); + screenPos = m_canvasTrans.TransformPoint(pos); + + return hadMouseDown; + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs.meta new file mode 100644 index 00000000..49a69a94 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e74405722cde4618ab9345bfee06bb82 +timeCreated: 1661503069 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs new file mode 100644 index 00000000..6477da79 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; + +namespace TEngine.Runtime.UIModule +{ + public partial class UISys + { + private List m_listController = new List(); + + public void RegisterAllController() + { + //AddController(); + } + + private void AddController() where T : IUIController, new() + { + for (int i = 0; i < m_listController.Count; i++) + { + var type = m_listController[i].GetType(); + + if (type == typeof(T)) + { + Log.Error(Utility.Text.Format("repeat controller type: {0}", typeof(T).Name)); + return; + } + } + + var controller = new T(); + + m_listController.Add(controller); + + controller.RegisterUIEvent(); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs.meta new file mode 100644 index 00000000..3569bcf5 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1a56516dc01941788d6b2444486ba2dc +timeCreated: 1661502668 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs new file mode 100644 index 00000000..04577cfd --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs @@ -0,0 +1,27 @@ +namespace TEngine.Runtime.UIModule +{ + public partial class UISys:BehaviourSingleton + { + public static int DesginWidth => 750; + + public static int DesginHeight => 1624; + + public static UIManager Mgr + { + get { return UIManager.Instance; } + } + + public override void Active() + { + base.Active(); + + RegisterAllController(); + } + + public override void Update() + { + base.Update(); + UIManager.Instance.OnUpdate(); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs.meta new file mode 100644 index 00000000..5dd1e8a2 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7d083df9934145c38db443762bb72060 +timeCreated: 1661502362 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs new file mode 100644 index 00000000..4bc2ad48 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace TEngine.Runtime.UIModule +{ + public enum WindowStackIndex + { + StackNormal = 0, + StackTop = 1, + StackMax + }; + + public class UIWindowStack + { + public WindowStackIndex m_stackIndex; + public int m_baseOrder = 0; + public List m_windowList = new List(); + public Transform m_parentTrans; + + public int FindIndex(uint windowId) + { + for (int i = 0; i < m_windowList.Count; i++) + { + if (m_windowList[i] == windowId) + { + return i; + } + } + + return -1; + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs.meta new file mode 100644 index 00000000..355439a9 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0f7edbd113f347cd879fb95370b9e8f5 +timeCreated: 1661503317 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI.meta new file mode 100644 index 00000000..6bc393a2 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9548eadbef8ca014186047db6722d7c3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs new file mode 100644 index 00000000..be7c3910 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace TEngine.Runtime.UIModule +{ + public class UIBase + { + protected GameObject m_go; + protected RectTransform m_transform; + protected string m_name; + protected bool m_destroyed = true; + + private GameEventMgr m_eventMgr; + + protected GameEventMgr EventMgr + { + get + { + if (m_eventMgr == null) + { + m_eventMgr = GameEventMgr.Instance; + } + + return m_eventMgr; + } + } + + public bool IsDestroyed + { + get { return m_destroyed; } + } + + public bool IsCreated + { + get { return !IsDestroyed; } + } + + public RectTransform transform + { + get { return m_transform; } + } + + public GameObject gameObject + { + get { return m_go; } + } + + public string name + { + get + { + if (string.IsNullOrEmpty(m_name)) + { + m_name = GetType().Name; + } + + return m_name; + } + } + + #region Event + + private Dictionary m_eventTable = new Dictionary(); + + protected void ClearAllRegisterEvent() + { + var element = m_eventTable.GetEnumerator(); + while (element.MoveNext()) + { + var m_event = element.Current.Value; + //GameEventMgr.Instance.RemoveEventListener(element.Current.Key, m_event); + } + + m_eventTable.Clear(); + } + + protected void AddUIEvent(int eventType, Action handler) + { + m_eventTable.Add(eventType, handler); + EventMgr.AddEventListener(eventType, handler); + } + + protected void AddUIEvent(int eventType, Action handler) + { + m_eventTable.Add(eventType, handler); + EventMgr.AddEventListener(eventType, handler); + } + + protected void AddUIEvent(int eventType, Action handler) + { + m_eventTable.Add(eventType, handler); + EventMgr.AddEventListener(eventType, handler); + } + + protected void AddUIEvent(int eventType, Action handler) + { + m_eventTable.Add(eventType, handler); + EventMgr.AddEventListener(eventType, handler); + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs.meta new file mode 100644 index 00000000..8a7c8e31 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fbb040a83c294beab9e514d9c2360841 +timeCreated: 1661503401 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindow.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindow.cs new file mode 100644 index 00000000..fc663066 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/UI/UIWindow.cs @@ -0,0 +1,270 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace TEngine.Runtime.UIModule +{ + public enum UIWindowType + { + /// 普通窗口 + WindowNormal, + + /// 置顶窗口 + WindowTop, + + /// 模态窗口 + WindowModel + } + public enum ModalType + { + /// 普通模态 + NormalType, + + /// 透明模态 + TransparentType, + + /// 普通状态且有关闭功能 + NormalHaveClose, + + /// 透明状态且有关闭功能 + TransparentHaveClose, + + /// 非模态 + NoneType, + } + + public class UIWindow : UIWindowBase + { + #region 属性 + private bool m_isClosed = false; + private bool m_isCreating = false; + private Image m_modalImage; + private float m_modalAlpha = 0.86f; + + /// + /// 是否固定SortingOrder + /// + public virtual bool IsFixedSortingOrder + { + get { return false; } + } + + public virtual bool NeedCenterUI() + { + return true; + } + + /// + /// 窗口Id + /// + private uint m_windowId = 0; + + /// + /// 窗口Id + /// + public uint WindowId + { + get { return m_windowId; } + } + + private static uint m_nextWindowId = 0; + + public virtual bool IsFullScreen + { + get { return false; } + } + + public virtual bool IsFullScreenMaskScene + { + get { return false; } + } + + /// + /// 一个界面中最大sortOrder值 + /// + public const int MaxCanvasSortingOrder = 50; + + /// + /// SortingOrder = stack.m_baseOrder + FixedAdditionalOrder + /// + public virtual int FixedAdditionalOrder + { + get { return UIWindow.MaxCanvasSortingOrder; } + } + + public int SortingOrder + { + get + { + if (m_canvas != null) + { + return m_canvas.sortingOrder; + } + + return 0; + } + + set + { + if (m_canvas != null) + { + int oldOrder = m_canvas.sortingOrder; + if (oldOrder != value) + { + var listCanvas = gameObject.GetComponentsInChildren(true); + for (int i = 0; i < listCanvas.Length; i++) + { + var childCanvas = listCanvas[i]; + childCanvas.sortingOrder = value + (childCanvas.sortingOrder - oldOrder); + } + m_canvas.sortingOrder = value; + _OnSortingOrderChg(); + } + } + } + } + #endregion + + + public void AllocWindowId() + { + if (m_nextWindowId == 0) + { + m_nextWindowId++; + } + + m_windowId = m_nextWindowId++; + } + + #region virtual function + + public virtual UIWindowType GetWindowType() + { + return UIWindowType.WindowNormal; + } + + #endregion + + #region Call From UIManager + public bool Create(UIManager uiMgr, GameObject uiGo) + { + if (IsCreated) + { + return true; + } + + m_isClosed = false; + if (!CreateBase(uiGo, true)) + { + return false; + } + + if (m_canvas == null) + { + Debug.LogErrorFormat("{0} have not a canvas!!", this.ToString()); + Destroy(); + return false; + } + + m_ownUIManager = uiMgr; + m_firstVisible = false; + + if (m_canvas != null) + { + m_canvas.overrideSorting = true; + } + + ScriptGenerator(); + BindMemberProperty(); + RegisterEvent(); + + m_isCreating = true; + OnCreate(); + m_isCreating = false; + + if (m_isClosed) + { + Destroy(); + return false; + } + + SetModalState(GetModalType()); + return true; + } + + public virtual ModalType GetModalType() + { + if (IsFullScreen || GetWindowType() == UIWindowType.WindowTop) + { + return ModalType.TransparentType; + } + return ModalType.NormalType; + } + + private void SetModalState(ModalType type) + { + var canClose = false; + switch (type) + { + case ModalType.NormalType: + { + m_modalAlpha = 0f; + break; + } + case ModalType.NormalHaveClose: + { + canClose = true; + break; + } + case ModalType.TransparentType: + { + m_modalAlpha = 0.01f; + break; + } + case ModalType.TransparentHaveClose: + { + m_modalAlpha = 0.01f; + canClose = true; + break; + } + default: + { + m_modalAlpha = 0f; + break; + } + } + if (m_modalAlpha > 0) + { + string path = "UI/ModalSprite.prefab"; + GameObject modal = TResources.Load(path); + modal.transform.SetParent(transform); + modal.transform.SetAsFirstSibling(); + modal.transform.localScale = Vector3.one; + modal.transform.localPosition = Vector3.zero; + if (canClose) + { + var button = UnityUtil.AddMonoBehaviour