UILayerManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class UILayerManager : MonoBehaviour
  5. {
  6. public List<UICameraData> UICameraList = new List<UICameraData>();
  7. public void Awake()
  8. {
  9. for (int i = 0; i < UICameraList.Count; i++)
  10. {
  11. UICameraData data = UICameraList[i];
  12. //data.m_root.transform.localPosition = new Vector3(0, 0, i * -2000);
  13. if (data.m_root == null)
  14. {
  15. Debug.LogError("UILayerManager :Root is null! " + " key : " + data.m_key + " index : " + i);
  16. }
  17. if (data.m_camera == null)
  18. {
  19. Debug.LogError("UILayerManager :Camera is null! " + " key : " + data.m_key + " index : " + i);
  20. }
  21. if (data.m_GameUILayerParent == null)
  22. {
  23. Debug.LogError("UILayerManager :GameUILayerParent is null!" + " key : " + data.m_key + " index : " + i);
  24. }
  25. if (data.m_FixedLayerParent == null)
  26. {
  27. Debug.LogError("UILayerManager :FixedLayerParent is null!" + " key : " + data.m_key + " index : " + i);
  28. }
  29. if (data.m_NormalLayerParent == null)
  30. {
  31. Debug.LogError("UILayerManager :NormalLayerParent is null!" + " key : " + data.m_key + " index : " + i);
  32. }
  33. if (data.m_TopbarLayerParent == null)
  34. {
  35. Debug.LogError("UILayerManager :TopbarLayerParent is null!" + " key : " + data.m_key + " index : " + i);
  36. }
  37. if (data.m_PopUpLayerParent == null)
  38. {
  39. Debug.LogError("UILayerManager :popUpLayerParent is null!" + " key : " + data.m_key + " index : " + i);
  40. }
  41. }
  42. }
  43. public void SetLayer(UIWindowBase ui,string cameraKey = null)
  44. {
  45. UICameraData data = GetUICameraDataByKey(cameraKey);
  46. if(cameraKey == null)
  47. {
  48. data = GetUICameraDataByKey(ui.cameraKey);
  49. }
  50. else
  51. {
  52. data = GetUICameraDataByKey(cameraKey);
  53. }
  54. RectTransform rt = ui.GetComponent<RectTransform>();
  55. switch (ui.m_UIType)
  56. {
  57. case UIType.GameUI: ui.transform.SetParent(data.m_GameUILayerParent); break;
  58. case UIType.Fixed: ui.transform.SetParent(data.m_FixedLayerParent); break;
  59. case UIType.Normal:
  60. ui.transform.SetParent(data.m_NormalLayerParent);
  61. break;
  62. case UIType.TopBar: ui.transform.SetParent(data.m_TopbarLayerParent); break;
  63. case UIType.PopUp: ui.transform.SetParent(data.m_PopUpLayerParent); break;
  64. }
  65. rt.localScale = Vector3.one;
  66. rt.sizeDelta = Vector2.zero;
  67. if (ui.m_UIType != UIType.GameUI)
  68. {
  69. rt.anchorMin = Vector2.zero;
  70. rt.anchorMax = Vector3.one;
  71. rt.sizeDelta = Vector2.zero;
  72. rt.transform.localPosition = new Vector3(0, 0, ui.m_PosZ);
  73. rt.anchoredPosition3D = new Vector3(0, 0, ui.m_PosZ);
  74. rt.SetAsLastSibling();
  75. }
  76. else
  77. {
  78. Vector3 lp = rt.transform.localPosition;
  79. lp.z = 0;
  80. rt.transform.localPosition = lp;
  81. }
  82. }
  83. public UICameraData GetUICameraDataByKey(string key)
  84. {
  85. if(key == null || key == "")
  86. {
  87. if(UICameraList.Count > 0)
  88. {
  89. return UICameraList[0];
  90. }
  91. else
  92. {
  93. throw new System.Exception("UICameraList is null ! " + key);
  94. }
  95. }
  96. for (int i = 0; i < UICameraList.Count; i++)
  97. {
  98. if(UICameraList[i].m_key == key)
  99. {
  100. return UICameraList[i];
  101. }
  102. }
  103. throw new System.Exception("Dont Find UILayerData by " + key);
  104. }
  105. //public void RemoveUI(UIWindowBase ui)
  106. //{
  107. // switch (ui.m_UIType)
  108. // {
  109. // case UIType.GameUI: break;
  110. // case UIType.Fixed: break;
  111. // case UIType.Normal:
  112. // normalUIList.Remove(ui);
  113. // break;
  114. // case UIType.TopBar: break;
  115. // case UIType.PopUp: break;
  116. // }
  117. //}
  118. [System.Serializable]
  119. public struct UICameraData
  120. {
  121. public string m_key;
  122. public GameObject m_root;
  123. public Camera m_camera;
  124. public Transform m_GameUILayerParent;
  125. public Transform m_FixedLayerParent;
  126. public Transform m_NormalLayerParent;
  127. public Transform m_TopbarLayerParent;
  128. public Transform m_PopUpLayerParent;
  129. }
  130. }