/// <summary> /// 执行相应的事件 /// </summary> /// <param name="index"></param> /// <param name="state"></param> static void excuteEvent(GameObject go, AS_State state) { if (go && go.GetComponent <AudioSource>()) { AudioSource _as = go.GetComponent <AudioSource>(); excuteEvent(_as, state); } }
/// <summary> /// 执行相应的事件 /// </summary> /// <param name="index"></param> /// <param name="state"></param> static void excuteEvent(int index, AS_State state) { //更新音频组件的状态 _asStateList[index] = state; //执行对应状态的回调函数 if (_funcBodyList[index]._eventType == state) { _funcBodyList[index]._func(); } }
/// <summary> /// 执行相应的事件 /// </summary> /// <param name="index"></param> /// <param name="state"></param> static void excuteEvent(AudioSource _as, AS_State state) { if (_as) { for (int i = 0; i < _asList.Count; i++) { if (_as.Equals(_asList[i])) { //更新音频组件的状态 _asStateList[i] = state; //执行对应状态的回调函数 if (_funcBodyList[i]._eventType == state) { _funcBodyList[i]._func(); } } } } }
/// <summary> /// 操作所有的音频组件(暂停、取消暂停、停止播放) /// </summary> /// <param name="state"></param> public static void OperateAll(AS_State state) { if (_asList.Count > 0) { //1.先操作 for (int i = 0; i < _asList.Count; i++) { switch (state) { case AS_State.Pause: _asList[i].Pause(); break; case AS_State.UnPause: _asList[i].UnPause(); break; case AS_State.Stop: _asList[i].Stop(); break; case AS_State.Mute: _asList[i].mute = true; break; case AS_State.UnMute: _asList[i].mute = false; break; } //2.操作完后执行对应回调 excuteEvent(i, state); } //3.如果执行的操作是停止,则清楚所有音频组件和对应回调 if (state == AS_State.Stop) { _asList.Clear(); _funcBodyList.Clear(); } } }
public FuncBody(AS_State et, Action func) { _eventType = et; _func = func; }
public static AudioSource Play(AudioSource _as, AudioClip clip, Action callback = null, AS_State et = AS_State.Done) { CreateDontDestroyGO(); if (_as) { Play(_as, clip); } if (callback != null) { FuncBody funcBody = new FuncBody(et, callback); if (!_asList.Contains(_as)) { _asList.Add(_as); _asStateList.Add(AS_State.Playing); _funcBodyList.Add(funcBody); } else { Debug.Log("已包含改音频组件..."); excuteEvent(_as, AS_State.Playing); } } return(_as); }