using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; /*** UI管理类 1. 分成上中下三级, 进入战斗简单关闭下级 2. 得到UI靠unity本身树形结构 3. 如果有模型需要加入Z值, z属于可继承的, 在uiComponent配置需要的Z值 4. 如果有粒子系统需要加入Order, 属于不可继承的, 在uiComponent配置需要的Order值 */ public class UIManager : MonoBehaviour { private static UIManager sInstance; public Camera cameraUI; private UILayer mCurLayer = UILayer.lower; private GameObject[] mAnchors = new GameObject[3]; public bool isPreLoad = false; public static UIManager GetInstance() { return sInstance; } void Awake(){ sInstance = this; DontDestroyOnLoad(gameObject); mAnchors[(int)UILayer.lower] = Gow.c(gameObject).Find("lower"); mAnchors[(int)UILayer.mid] = Gow.c(gameObject).Find("mid"); mAnchors[(int)UILayer.up] = Gow.c(gameObject).Find("up"); cameraUI = Gow.c(gameObject).Find("UICamera"); } public GameObject CreateUI(GameObject preUIObj ){ UIComponent preUI = preUIObj.GetComponent(); GameObject anchor = mAnchors[(int)mCurLayer]; if (preUI.uiLayer!=UILayer.none ){ anchor = mAnchors[(int)preUI.uiLayer]; // SetCurLayer(preUI.uiLayer); } GameObject uiObj = Tools.CreateUI(preUIObj, anchor ); UIComponent uiComponent = Tools.GetOrCreateComponent(uiObj); AddUI(uiComponent ); return uiObj; } public GameObject CreateUI(string path ){ GameObject preUIObj = Resources.Load(path ) as GameObject; return CreateUI(preUIObj); } //主要处理Z值和sortingOrder public void AddUI(UIComponent ui ){ if (isPreLoad ){ ui.isPreLoad = true; return; } GameObject anchor = ui.transform.parent.gameObject; // Debug.LogWarning(ui.gameObject.name + "anchor name is " + anchor.name ); UIComponent lastComponent = GetUI(anchor, 1); if (lastComponent!=null ){ ui.z = lastComponent.zAdd + lastComponent.z; //Debug.LogWarning(lastComponent.gameObject.name ); // Debug.LogWarning(lastComponent.requestOrder + "last order is " + lastComponent.requestOrder ); if (lastComponent.requestOrder > 0&&lastComponent.curOrder==0){ ui.curOrder = lastComponent.requestOrder + anchor.GetComponent().sortingOrder + 1; }else if (lastComponent.curOrder>0){ //上一个有用到order加入order ui.curOrder = lastComponent.requestOrder+lastComponent.curOrder + 1; } } if (lastComponent==null&&ui.requestOrder>0 ){ // //当新UI需要用到order 但是之前没有建立一个新的canvas 并用当前layer的order ui.curOrder = anchor.GetComponent().sortingOrder + 1; } if (ui.isFull ){ bool isStartDisable = false; UIComponent[] uis = anchor.GetComponentsInChildren(true ); for (int i = uis.Length-1; i >=0; i--){ UIComponent peekUI = uis[i]; if (peekUI == null||peekUI.gameObject == null ){ continue; } if (peekUI == ui ){ isStartDisable = true; continue; } if (isStartDisable){ peekUI.gameObject.SetActive(false); } } } ui.SetInit(); } public void SetCurLayer(UILayer layer ){ mCurLayer = layer; } public UILayer GetCurLayer(){ return mCurLayer; } public void SetLayerActive(UILayer layer, bool isActive ){ mAnchors[(int)layer].SetActive(isActive ); } public UIComponent GetLastUI(){ return GetUI(mAnchors[(int)mCurLayer], 1); } public UIComponent GetLowerUI() { return GetUI(mAnchors[(int)UILayer.lower], 0); } public UIComponent GetMidUI() { return GetUI(mAnchors[(int)UILayer.mid], 0); } public UIComponent GetCurUI(){ return GetUI(mAnchors[(int)mCurLayer], 0); } public GameObject GetAnchor(){ return mAnchors[(int)UILayer.up]; } public void DestroyUI(UIComponent ui ){ GameObject anchor = ui.transform.parent.gameObject; Debug.Log("anchor name is " + anchor.gameObject.name ); UIComponent[] uis = anchor.GetComponentsInChildren(true ); if (anchor == mAnchors[(int)UILayer.up]){ if (uis.Length <= 0){ return; } UIComponent peekUI = uis[uis.Length-1]; //TODO for scan if (peekUI.gameObject.activeSelf ){ peekUI.OnResume(); } return; } for (int i = uis.Length-1; i >=0; i--){ UIComponent peekUI = uis[i]; if (peekUI == ui ){ continue; } if (peekUI == null||peekUI.gameObject == null ){ continue; } peekUI.OnResume(); Debug.LogWarning("SetActive now " + peekUI.gameObject.name ); peekUI.gameObject.SetActive(true); if (peekUI.isFull ){ break; } } } public void DestroyUIS(UIComponent ui){ GameObject anchor = ui.transform.parent.gameObject; UIComponent[] uis = anchor.GetComponentsInChildren(true ); int temp = uis.Length; for (int i = 0; i < uis.Length; i++){ UIComponent peekUI = uis[i]; if (ui == peekUI) temp = i; if (temp < i){ Destroy(peekUI.gameObject); //DestroyUI(peekUI); } } } public UIComponent GetUI(GameObject anchor,int lastIndex ){ Transform curAnchorTrans = anchor.transform; UIComponent[] childs = curAnchorTrans.GetComponentsInChildren(true); // Debug.LogWarning("child is " + childs.Length ); // foreach(UIComponent child in childs ){ // Debug.LogWarning("child is " + child.gameObject.name ); // } if (childs.Length <= lastIndex){ return null; } return childs[childs.Length - lastIndex - 1]; } public void TurnToInGame(){ SetLayerActive(UILayer.lower, false); SetCurLayer(UILayer.mid ); } public void TurnBack(){ WJUtility.PurgeChildren(mAnchors[(int)UILayer.mid], false, true); //SetLayerActive(UILayer.mid, false); SetLayerActive(UILayer.lower, true ); SetCurLayer(UILayer.lower ); } public void TurnBackToLogin(){ WJUtility.PurgeChildren(mAnchors[(int)UILayer.mid], false, true); WJUtility.PurgeChildren(mAnchors[(int)UILayer.lower], false, true); WJUtility.PurgeChildren(mAnchors[(int)UILayer.up], false, true); SetLayerActive(UILayer.lower, true ); SetCurLayer(UILayer.lower ); EventNotificationCenter.GetInstance().Broadcast(SysEvents.ON_LOGOUT); } }