public void OnEnable() { GameObject CV = GameObject.Find("Canvas"); //寻找或创建Canvas,然后设置它 if (CV == null) { ResMgr.I().LoadAsync <GameObject>("UI/Canvas", SetCanvas); } else { SetCanvas(CV); } GameObject ES = GameObject.Find("EventSystem"); //寻找或创建EventSystem,然后设置它 if (ES == null) { //创建EventSystem,让其过场的时候不被移除 ResMgr.I().LoadAsync <GameObject>("UI/EventSystem", o => { o.name = "EventSystem"; DontDestroyOnLoad(o); }); } else { ES.name = "EventSystem"; DontDestroyOnLoad(ES); } }
/// <summary> /// 播放背景音乐 /// </summary> /// <param name="name"></param> public void PlayBackMusic(string name) { if (BGMusic == null) { GameObject obj = new GameObject(); obj.name = "BGMusic"; BGMusic = obj.AddComponent <AudioSource>(); } //异步加载背景音乐并播放 ResMgr.I().LoadAsync <AudioClip>(name, (Clip) => { BGMusic.clip = Clip; BGMusic.volume = BGMVolume; BGMusic.loop = true; BGMusic.Play(); }); }
public void GetObject(string name, UnityAction <GameObject> callback = null) { if (PoolDic.ContainsKey(name) && PoolDic[name].PoolList.Count > 0)//判断存在该类缓存池 并且缓存池中有物体 { callback?.Invoke(PoolDic[name].Get()); } else//以上条件任意一点不满足都可以直接实例化新物体 { //通过异步加载资源 ResMgr.I().Load <GameObject>(name, o => { o.name = name; callback?.Invoke(o); }); } }
/// <summary> /// 播放音效 /// </summary> /// <param name="name">路径</param> /// <param name="callback">回调函数</param> /// <param name="volume">音量</param> /// <param name="IsLoop">是否循环</param> public void PlayAudio(string name, float v, bool IsLoop, UnityAction <AudioSource> callback) { if (SoundObj == null) { SoundObj = new GameObject("Sound"); } //异步加载音效并播放 ResMgr.I().LoadAsync <AudioClip>(name, (Clip) => { AudioSource source = SoundObj.AddComponent <AudioSource>(); source.clip = Clip; source.volume = v; BGMusic.loop = IsLoop; source.Play(); SoundList.Add(source); callback?.Invoke(source); }); }
/// <summary> /// 显示面板 /// </summary> /// <typeparam name="T">面板类型</typeparam> /// <param name="PanelName">面板名</param> /// <param name="layer">面板应该显示在哪一层</param> /// <param name="callBack">面板创建成功后要执行的内容</param> public void ShowPanel <T>(string PanelName, UI_Layer layer = UI_Layer.Bottom, UnityAction <T> callBack = null) where T : BasePanel { switch (layer) { case UI_Layer.Bottom: if (CurrentBottomPanel != null) { HidePanel(CurrentBottomPanel.gameObject.name); CurrentBottomPanel = null; } break; case UI_Layer.Middle: if (CurrentMiddlePanel != null) { HidePanel(CurrentMiddlePanel.gameObject.name); CurrentMiddlePanel = null; } break; case UI_Layer.Top: if (CurrentTopPanel != null) { HidePanel(CurrentTopPanel.gameObject.name); CurrentTopPanel = null; } break; case UI_Layer.System: if (CurrentSystemPanel != null) { HidePanel(CurrentSystemPanel.name); CurrentSystemPanel = null; } break; default: break; } if (panelDic.ContainsKey(PanelName)) { panelDic[PanelName].ShowPanel(); callBack?.Invoke(panelDic[PanelName] as T); return; } else { ResMgr.I().LoadAsync <GameObject>("UI/" + PanelName, (obj) => { //将其移到Canvas中的各个层级之下 Transform father = Bottom; switch (layer) { case UI_Layer.Bottom: CurrentBottomPanel = obj; break; case UI_Layer.Middle: CurrentMiddlePanel = obj; father = Middle; break; case UI_Layer.Top: CurrentTopPanel = obj; father = Top; break; case UI_Layer.System: CurrentSystemPanel = obj; father = System; break; } //设置父对象 obj.transform.SetParent(father); //设置其相对位置 obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; (obj.transform as RectTransform).offsetMax = Vector2.zero; (obj.transform as RectTransform).offsetMin = Vector2.zero; //得到预设体身上的BasePanel脚本 T panel = obj.GetComponent <T>(); panel.name = PanelName; panel.ShowPanel(); //将其添加到PanelDic中 panelDic.Add(PanelName, panel); //处理面板创建完成后,要执行的方法 callBack?.Invoke(panel); }); } }