UIManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. /***
  6. UI管理类
  7. 1. 分成上中下三级, 进入战斗简单关闭下级
  8. 2. 得到UI靠unity本身树形结构
  9. 3. 如果有模型需要加入Z值, z属于可继承的, 在uiComponent配置需要的Z值
  10. 4. 如果有粒子系统需要加入Order, 属于不可继承的, 在uiComponent配置需要的Order值
  11. */
  12. public class UIManager : MonoBehaviour {
  13. private static UIManager sInstance;
  14. public Camera cameraUI;
  15. private UILayer mCurLayer = UILayer.lower;
  16. private GameObject[] mAnchors = new GameObject[3];
  17. public bool isPreLoad = false;
  18. public static UIManager GetInstance()
  19. {
  20. return sInstance;
  21. }
  22. void Awake(){
  23. sInstance = this;
  24. DontDestroyOnLoad(gameObject);
  25. mAnchors[(int)UILayer.lower] = Gow.c(gameObject).Find("lower");
  26. mAnchors[(int)UILayer.mid] = Gow.c(gameObject).Find("mid");
  27. mAnchors[(int)UILayer.up] = Gow.c(gameObject).Find("up");
  28. cameraUI = Gow.c(gameObject).Find<Camera>("UICamera");
  29. }
  30. public GameObject CreateUI(GameObject preUIObj ){
  31. UIComponent preUI = preUIObj.GetComponent<UIComponent>();
  32. GameObject anchor = mAnchors[(int)mCurLayer];
  33. if (preUI.uiLayer!=UILayer.none ){
  34. anchor = mAnchors[(int)preUI.uiLayer];
  35. // SetCurLayer(preUI.uiLayer);
  36. }
  37. GameObject uiObj = Tools.CreateUI(preUIObj, anchor );
  38. UIComponent uiComponent = Tools.GetOrCreateComponent<UIComponent>(uiObj);
  39. AddUI(uiComponent );
  40. return uiObj;
  41. }
  42. public GameObject CreateUI(string path ){
  43. GameObject preUIObj = Resources.Load(path ) as GameObject;
  44. return CreateUI(preUIObj);
  45. }
  46. //主要处理Z值和sortingOrder
  47. public void AddUI(UIComponent ui ){
  48. if (isPreLoad ){
  49. ui.isPreLoad = true;
  50. return;
  51. }
  52. GameObject anchor = ui.transform.parent.gameObject;
  53. // Debug.LogWarning(ui.gameObject.name + "anchor name is " + anchor.name );
  54. UIComponent lastComponent = GetUI(anchor, 1);
  55. if (lastComponent!=null ){
  56. ui.z = lastComponent.zAdd + lastComponent.z;
  57. //Debug.LogWarning(lastComponent.gameObject.name );
  58. // Debug.LogWarning(lastComponent.requestOrder + "last order is " + lastComponent.requestOrder );
  59. if (lastComponent.requestOrder > 0&&lastComponent.curOrder==0){
  60. ui.curOrder = lastComponent.requestOrder + anchor.GetComponent<Canvas>().sortingOrder + 1;
  61. }else if (lastComponent.curOrder>0){
  62. //上一个有用到order加入order
  63. ui.curOrder = lastComponent.requestOrder+lastComponent.curOrder + 1;
  64. }
  65. }
  66. if (lastComponent==null&&ui.requestOrder>0 ){
  67. // //当新UI需要用到order 但是之前没有建立一个新的canvas 并用当前layer的order
  68. ui.curOrder = anchor.GetComponent<Canvas>().sortingOrder + 1;
  69. }
  70. if (ui.isFull ){
  71. bool isStartDisable = false;
  72. UIComponent[] uis = anchor.GetComponentsInChildren<UIComponent>(true );
  73. for (int i = uis.Length-1; i >=0; i--){
  74. UIComponent peekUI = uis[i];
  75. if (peekUI == null||peekUI.gameObject == null ){
  76. continue;
  77. }
  78. if (peekUI == ui ){
  79. isStartDisable = true;
  80. continue;
  81. }
  82. if (isStartDisable){
  83. peekUI.gameObject.SetActive(false);
  84. }
  85. }
  86. }
  87. ui.SetInit();
  88. }
  89. public void SetCurLayer(UILayer layer ){
  90. mCurLayer = layer;
  91. }
  92. public UILayer GetCurLayer(){
  93. return mCurLayer;
  94. }
  95. public void SetLayerActive(UILayer layer, bool isActive ){
  96. mAnchors[(int)layer].SetActive(isActive );
  97. }
  98. public UIComponent GetLastUI(){
  99. return GetUI(mAnchors[(int)mCurLayer], 1);
  100. }
  101. public UIComponent GetLowerUI()
  102. {
  103. return GetUI(mAnchors[(int)UILayer.lower], 0);
  104. }
  105. public UIComponent GetMidUI()
  106. {
  107. return GetUI(mAnchors[(int)UILayer.mid], 0);
  108. }
  109. public UIComponent GetCurUI(){
  110. return GetUI(mAnchors[(int)mCurLayer], 0);
  111. }
  112. public GameObject GetAnchor(){
  113. return mAnchors[(int)UILayer.up];
  114. }
  115. public void DestroyUI(UIComponent ui ){
  116. GameObject anchor = ui.transform.parent.gameObject;
  117. Debug.Log("anchor name is " + anchor.gameObject.name );
  118. UIComponent[] uis = anchor.GetComponentsInChildren<UIComponent>(true );
  119. if (anchor == mAnchors[(int)UILayer.up]){
  120. if (uis.Length <= 0){
  121. return;
  122. }
  123. UIComponent peekUI = uis[uis.Length-1];
  124. //TODO for scan
  125. if (peekUI.gameObject.activeSelf ){
  126. peekUI.OnResume();
  127. }
  128. return;
  129. }
  130. for (int i = uis.Length-1; i >=0; i--){
  131. UIComponent peekUI = uis[i];
  132. if (peekUI == ui ){
  133. continue;
  134. }
  135. if (peekUI == null||peekUI.gameObject == null ){
  136. continue;
  137. }
  138. peekUI.OnResume();
  139. Debug.LogWarning("SetActive now " + peekUI.gameObject.name );
  140. peekUI.gameObject.SetActive(true);
  141. if (peekUI.isFull ){
  142. break;
  143. }
  144. }
  145. }
  146. public void DestroyUIS(UIComponent ui){
  147. GameObject anchor = ui.transform.parent.gameObject;
  148. UIComponent[] uis = anchor.GetComponentsInChildren<UIComponent>(true );
  149. int temp = uis.Length;
  150. for (int i = 0; i < uis.Length; i++){
  151. UIComponent peekUI = uis[i];
  152. if (ui == peekUI)
  153. temp = i;
  154. if (temp < i){
  155. Destroy(peekUI.gameObject);
  156. //DestroyUI(peekUI);
  157. }
  158. }
  159. }
  160. public UIComponent GetUI(GameObject anchor,int lastIndex ){
  161. Transform curAnchorTrans = anchor.transform;
  162. UIComponent[] childs = curAnchorTrans.GetComponentsInChildren<UIComponent>(true);
  163. // Debug.LogWarning("child is " + childs.Length );
  164. // foreach(UIComponent child in childs ){
  165. // Debug.LogWarning("child is " + child.gameObject.name );
  166. // }
  167. if (childs.Length <= lastIndex){
  168. return null;
  169. }
  170. return childs[childs.Length - lastIndex - 1];
  171. }
  172. public void TurnToInGame(){
  173. SetLayerActive(UILayer.lower, false);
  174. SetCurLayer(UILayer.mid );
  175. }
  176. public void TurnBack(){
  177. WJUtility.PurgeChildren(mAnchors[(int)UILayer.mid], false, true);
  178. //SetLayerActive(UILayer.mid, false);
  179. SetLayerActive(UILayer.lower, true );
  180. SetCurLayer(UILayer.lower );
  181. }
  182. public void TurnBackToLogin(){
  183. WJUtility.PurgeChildren(mAnchors[(int)UILayer.mid], false, true);
  184. WJUtility.PurgeChildren(mAnchors[(int)UILayer.lower], false, true);
  185. WJUtility.PurgeChildren(mAnchors[(int)UILayer.up], false, true);
  186. SetLayerActive(UILayer.lower, true );
  187. SetCurLayer(UILayer.lower );
  188. EventNotificationCenter.GetInstance().Broadcast(SysEvents.ON_LOGOUT);
  189. }
  190. }