1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
namespace IL {
public enum UILevel { Bottom, Common, Top, }
public class UIMgr : ASingleton<UIMgr> {
private Dictionary<string, PanelView> mAllPanel = new Dictionary<string, PanelView>();
private Transform mBottomTrans; private Transform mCommonTrans; private Transform mTopTrans;
private Canvas mCanvas; private CanvasScaler mCanvasScaler; private GraphicRaycaster mGraphicRaycaster; private RectTransform mRectTransform;
private EventSystem mEventSystem;
private Transform UIRoot;
public void Init() { UIRoot = GameObject.Instantiate(ResMgr.Ins.Load<GameObject>(AssetBundleName.UI,"uiroot")).transform; GameObject.DontDestroyOnLoad(UIRoot);
mBottomTrans = UIRoot.Find("Bottom"); mCommonTrans = UIRoot.Find("Common"); mTopTrans = UIRoot.Find("Top");
mCanvas = UIRoot.GetComponent<Canvas>(); mCanvasScaler = UIRoot.GetComponent<CanvasScaler>(); mGraphicRaycaster = UIRoot.GetComponent<GraphicRaycaster>(); mRectTransform = UIRoot.GetComponent<RectTransform>();
mEventSystem = UIRoot.Find("EventSystem").GetComponent<EventSystem>();
}
public RectTransform RectTransform { get { return mRectTransform; } }
public GraphicRaycaster GraphicRaycaster { get { return mGraphicRaycaster; } }
public Canvas RootCanvas { get { return mCanvas; } } public void SetResolution(int width, int height) { mCanvasScaler.referenceResolution = new Vector2(width, height); }
private T OpenUI<T>(string PanelName, UILevel uiLevel) where T:PanelView { if (!mAllPanel.ContainsKey(PanelName)) { CreateUI(typeof(T), PanelName, uiLevel); } mAllPanel[PanelName].SetActive(true); return mAllPanel[PanelName]as T;
}
private void OpenUIAsync<T>(string PanelName, Action<UnityEngine.Object> onLoaded = null) where T : PanelView, new() { if (!mAllPanel.ContainsKey(PanelName)) { CreateUIAsync<T>(PanelName,onLoaded); } onLoaded(null);
}
private void CreateUIAsync<T>(string panelName, Action<UnityEngine.Object> onLoaded) where T : PanelView, new() { ResMgr.Ins.LoadAsync(AssetBundleName.UI, panelName,onLoaded); }
private void CreateUI(Type type, string panelName, UILevel uiLevel) { PanelView panelView = Activator.CreateInstance(type) as PanelView;
Transform parent;
switch (uiLevel) { case UILevel.Bottom: parent = mBottomTrans; break; case UILevel.Common: parent = mCommonTrans; break; case UILevel.Top: parent = mTopTrans; break; default: parent = mCommonTrans; break; } GameObject ui = GameObject.Instantiate(ResMgr.Ins.Load<GameObject>(AssetBundleName.UI, panelName), parent); ui.name = panelName; panelView.SetGameObject(ui); panelView.Init(); mAllPanel.Add(panelName, panelView);
}
public ViewType Open<ViewType>(UILevel uiLevel = UILevel.Common) where ViewType : PanelView { return OpenUI<ViewType>( GetName<ViewType>(), uiLevel); }
public void OpenAsync<T>( Action<UnityEngine.Object> onLoaded = null) { OpenUIAsync<PanelView>(GetName<T>(), onLoaded); }
private string GetName<T>() { if (Runtime.Ins.IsHotResProject) { string name = typeof(T).ToString(); return name.Replace("Type : ", ""); } else { string name = typeof(T).ToString(); string[] nameSplits = name.Split('.'); return nameSplits[nameSplits.Length - 1]; }
} } }
|