工艺仿真实现又一思路

这里写图片描述


层与层之间的沟通可以通过传统的事件和委托的方式,也可以借助事件委托插件,最终实现层与层之间的无缝对接


    public static SoundManager instance;

    public TextMeshProUGUI Note;

    public static Action<string, bool, float> textBack;

    public bool isDisplay = false;  //true表示声音文本已经显示
    public float timee = 5;  //5秒后,自动关闭文本提示框

    public delegate void textPass(string content);
    public static event textPass wenBenChuanDi;  //文本传递
    /// <summary>
    /// 此脚本,专门用于负责全局音频的播放,以及播放完后干什么
    /// 作者:徐海涛(hunk Xu)
    /// </summary>

    private List<AudioClip> listt = new List<AudioClip>();  //声音list列表

    /// <summary>
    /// 单例的
    /// </summary>
    void Awake()
    {

        if (instance == null)
        {
            instance = this;
        }
        if (instance != this)
        {
            Destroy(this.gameObject);
        }

        //载入所有的Sound文本数据
        TextAsset aaa = (TextAsset)Resources.Load("utils/ttt", typeof(TextAsset));//从表中加载数据
        string json = aaa.text;
        JsonData jarr = JsonMapper.ToObject(json);
        TotalData.resetSoundData(jarr);

        //载入所有的声音文件
        AudioClip[] bbb = Resources.LoadAll<AudioClip>("Audio");
        for (int iii = 0; iii < bbb.Length; iii++)
        {
            listt.Add(bbb[iii]);
        }
    }

    public void OnEnable()
    {
        EventCenter.AddListener(FrameEvent.OnStateOver, tingZhiBoFang);
    }

    public void OnDisable()
    {
        EventCenter.RemoveListenrer(FrameEvent.OnStateOver, tingZhiBoFang);
    }

    void Update()
    {
        if (isDisplay)
        {
            timee -= Time.deltaTime;
            if (timee <= 0)
            {
                isDisplay = false;

            }
        }

    }

    /// <summary>
    /// /
    /// </summary>
    /// <param name="clipNo">剪辑编号</param>
    /// <param name="dp">true表示显示声音的UI</param>
    /// <param name="mm">回调方法</param>
    /// <param name="pp">几秒后回调</param>
    //播放指定名字的音频剪辑
    public void boFangShengYing(string clipNo, bool dp, Action mm, float pp)
    {
        AudioClip kk = getClip(clipNo);

        if (kk == null)
        {
            //print("没有找到指定名称的音频");
        }
        else
        {
            GetComponent<AudioSource>().clip = kk;
            GetComponent<AudioSource>().Play();  //播放音频
            if (mm != null)
            {
                item jj = new item(kk, mm, pp);
                StartCoroutine("startYingPingJiShi", jj);
            }

        }
        if (dp)
        {
            //依据编号,查找文本
            SoundDataBean beann = TotalData.soundData.getSoundInfoForID(clipNo);
            if (beann == null)
            {
                print("没有找到对应的文本内容");
            }
            else
            {
                if (wenBenChuanDi != null)
                {
                    //Debug.LogError(11111111);
                    wenBenChuanDi(beann.content);
                }

            }
        }
    }


    public void boFangShengYing(string clipNo, bool dp, Action mm)
    {
        AudioClip kk = getClip(clipNo);
        float len = kk.length;
        if (kk == null)
        {
            //print("没有找到指定名称的音频");
        }
        else
        {
            GetComponent<AudioSource>().clip = kk;
            GetComponent<AudioSource>().Play();  //播放音频
            if (mm != null)
            {
                item jj = new item(kk, mm, len);
                StartCoroutine("startYingPingJiShi", jj);
            }

        }
        if (dp)
        {
            //依据编号,查找文本
            SoundDataBean beann = TotalData.soundData.getSoundInfoForID(clipNo);
            if (beann == null)
            {
                print("没有找到对应的文本内容");
            }
            else
            {
                if (wenBenChuanDi != null)
                {
                    //Debug.LogError(11111111);
                    wenBenChuanDi(beann.content);
                }

            }
        }
    }

    public void zhiXingDelay(Action mm, float pp)
    {

        if (mm != null)
        {
            item jj = new item(null, mm, pp);
            StartCoroutine("startYingPingJiShi", jj);
        }



    }
    #region Jack

    public SongEventHandle curHandle;

    public St_Script_SoundBase soundBase;

    public static Dictionary<System.Object, St_Script_SoundBase> tempObj = new Dictionary<object, St_Script_SoundBase>();


    public St_Script_SoundBase PlaySoundFormInspector(AudioClip clip, bool isLoop = false,float value=1)
    {
        St_Script_SoundBase o=null;
        if (clip)
        {
            o = Instantiate(soundBase, transform);
            //tempObj.Add(clip.GetHashCode(), o);
            o.Play(clip, isLoop, value);
        }
        return o;
    }

    public St_Script_SoundBase PlaySoundFromResources(string str, bool isLoop = false, float value = 1)
    {
        var clip = Resources.Load<AudioClip>(string.Format("Sound/{0}", str));
        St_Script_SoundBase o=null;

        if (clip)
        {
            o = Instantiate(soundBase, transform);
            //tempObj.Add(clip.GetHashCode(), o);
            o.Play(clip, isLoop, value);
        }

        return o;
    }


    public SoundManager PlaySound(string clipNo, bool showUI = false, float DelayPlay = 0, float offsetAction = 0, float showUILength = 3)
    {
        Refresh();

        AudioClip clip = getClip(clipNo);

        var length = clip.length;

        var off = length + offsetAction;

        off = off <= 0 ? 0 : off;

        if (clip != null) StartCoroutine(PlaySoundBegin(clip, off, length, DelayPlay));

        showUILength += length;
        var beann = TotalData.soundData.getSoundInfoForID(clipNo);
        if (beann != null)
            //StartCoroutine(ShowUIPanel(showUILength, beann));
            if (textBack != null)
            {
                textBack(beann.content, showUI, showUILength);
            }
            else { Debug.Log("No LoadText!"); }

        return this;
    }
    public SoundManager OnComplete(Action action)
    {
        curHandle.Complete = action;
        return this;
    }

    public SoundManager OnStart(Action action)
    {
        curHandle.StartOn = action;
        return this;
    }

    public SoundManager OnOffset(Action action)
    {
        curHandle.Offset = action;
        return this;
    }

    public SoundManager OnDelay(Action action)
    {
        curHandle.Delay = action;
        return this;
    }
    IEnumerator PlaySoundOver(float length)
    {
        yield return new WaitForSeconds(length);
        if (curHandle.Offset != null)
        {
            curHandle.Offset.Invoke();
        }

    }


    IEnumerator PlaySoundBegin(AudioClip clip, float offsetLength, float AllLength, float Delay)
    {

        var audio = GetComponent<AudioSource>();

        audio.clip = clip;

        yield return null;
        //Do start handle

        if (curHandle.StartOn != null)
        {
            curHandle.StartOn.Invoke();
        }

        yield return new WaitForSeconds(Delay);


        if (curHandle.Delay != null)
        {
            curHandle.Delay.Invoke();
        }


        audio.Play();
        //offset handle
        StartCoroutine(PlaySoundOver(offsetLength));
        //complete handle
        yield return new WaitForSeconds(AllLength);

        if (curHandle.Complete != null)
        {
            curHandle.Complete.Invoke();
        }
    }

    IEnumerator ShowUIPanel(float time, SoundDataBean bean)
    {

        isDisplay = true;
        Note.text = bean.content;
        yield return new WaitForSeconds(time);

        isDisplay = false;

    }

    public void Refresh()
    {
        StopAllCoroutines();

        curHandle = new SongEventHandle();
    }

    #endregion


    public void tingZhiBoFang()
    {
        GetComponent<AudioSource>().Stop();
        StopAllCoroutines();
        if (curHandle != null)
            curHandle.HandleClear();
        curHandle = null;
    }
    //处理声音完后的回调
    IEnumerator startYingPingJiShi(item ff)
    {
        yield return new WaitForSeconds(ff.shiJian);
        ff.actionn();    //声音播放完后回调
    }



    //根据音频剪辑的名称快速获取音频
    private AudioClip getClip(string clipName)
    {
        AudioClip pp = null;
        foreach (AudioClip ac in listt)
        {
            if (ac.name.Contains(clipName))
            {
                pp = ac;
                return pp;
            }
        }
        return pp;
    }

    #region ZW
    public void ClearAudioSource()
    {

        GetComponent<AudioSource>().clip = null;

    }
    #endregion

    public class SongEventHandle
    {
        public Action Complete;
        public Action StartOn;
        public Action Delay;
        public Action Offset;

        public void HandleClear()
        {
            ClearActon(Complete);
            ClearActon(StartOn);
            ClearActon(Delay);
            ClearActon(Offset);
        }

        private void ClearActon(Action action)
        {
            if (action == null) return;
            var list = action.GetInvocationList();
            if (list != null && list.Length > 0)
                foreach (var item in list)
                {
                    action -= (Action)item;
                }
        }
    }

    //信息封装
    class item
    {
        public AudioClip clip;
        public Action actionn;
        public float shiJian;
        public item(AudioClip kk, Action nn, float yy)
        {
            clip = kk;
            actionn = nn;
            shiJian = yy;
        }

        public item(AudioClip kk, float yy)
        {
            clip = kk;
            shiJian = yy;
        }


    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;



public enum FrameEvent
{
    /// <summary>
    /// 状态执行结束时的事件
    /// </summary>
    OnStateOver,
}

public delegate void Callback();
public delegate void Callback<T>(T t);
public delegate void Callback<T, U>(T t, U u);
public delegate void Callback<T, U, V>(T t, U u, V v);
public delegate void Callback<T, U, V, X>(T t, U u, V v, X x);

public class EventCenter
{

#pragma warning disable 0414

#pragma warning restore 0414

    public static Dictionary<FrameEvent, Delegate> mEventTable = new Dictionary<FrameEvent, Delegate>();

    public static List<FrameEvent> mPermanenMessage = new List<FrameEvent>();

    public static void MarkAsPermanent(FrameEvent eventType)
    {
#if Log_All_MESSAGES
        Debug.log("Message MarkAsApermanent \t\""+eventType+"\"");
#endif
        mPermanenMessage.Add(eventType);
    }

    public static void Cleanup()
    {
#if Log_All_MESSAGES
        Debug.log("Message CleanUp \t\""+eventType+"\"");
#endif

        List<FrameEvent> messageToRemove = new List<FrameEvent>();

        foreach (KeyValuePair<FrameEvent, Delegate> pair in mEventTable)
        {
            bool wasFound = false;
            foreach (FrameEvent message in mPermanenMessage)
            {
                if (pair.Key == message)
                {
                    wasFound = true;
                    break;
                }
            }
            if (!wasFound)
            {
                messageToRemove.Add(pair.Key);
            }
        }

        foreach (FrameEvent message in messageToRemove)
        {
            mEventTable.Remove(message);
        }
    }

    public static void PrEgameEventEventTable()
    {

        foreach (KeyValuePair<FrameEvent, Delegate> pair in mEventTable)
        {
            Debug.Log(pair.Key + "\t" + pair.Value);
        }
    }

    public static void OnListenerAdding(FrameEvent eventType, Delegate ListenerBeingAdded)
    {

        if (!mEventTable.ContainsKey(eventType))
        {
            mEventTable.Add(eventType, null);
        }

        Delegate d = mEventTable[eventType];

        if (d != null && d.GetType() != ListenerBeingAdded.GetType())
        {
            throw new ListenerException("Current type is diffrent!");
        }

    }

    public static void OnListenerRemoveing(FrameEvent eventType, Delegate ListenerBeingAdded)
    {
        if (mEventTable.ContainsKey(eventType))
        {
            Delegate d = mEventTable[eventType];
            if (d == null)
            {
                throw new ListenerException("Current listenrer is null \t" + eventType);
            }
            else if (d.GetType() != ListenerBeingAdded.GetType())
            {
                throw new ListenerException(string.Format("{ 0},\t{1 },\t type is diffrent", d.GetType(), eventType));
            }
        }
        else
        {
            throw new ListenerException("Can't know about this eventType \t" + eventType);
        }
    }

    public static void OnListenerRemoved(FrameEvent eventType)
    {
        if (mEventTable[eventType] == null)
        {
            mEventTable.Remove(eventType);
        }
    }

    public static void OnBroadcasting(FrameEvent eventType)
    {

    }

    public class BroadcastException : Exception
    {
        public BroadcastException(string message) : base(message) { }
    }

    public class ListenerException : Exception
    {
        public ListenerException(string message) : base(message) { }
    }

    public static void AddListener(FrameEvent eventType, Callback handle)
    {
        OnListenerAdding(eventType, handle);
        mEventTable[eventType] = (Callback)mEventTable[eventType] + handle;
    }

    public static void AddListener<T>(FrameEvent eventType, Callback<T> handle)
    {
        OnListenerAdding(eventType, handle);
        mEventTable[eventType] = (Callback<T>)mEventTable[eventType] + handle;
    }

    public static void AddListener<T, U>(FrameEvent eventType, Callback<T, U> handle)
    {
        OnListenerAdding(eventType, handle);
        mEventTable[eventType] = (Callback<T, U>)mEventTable[eventType] + handle;
    }

    public static void AddListener<T, U, V>(FrameEvent eventType, Callback<T, U, V> handle)
    {
        OnListenerAdding(eventType, handle);
        mEventTable[eventType] = (Callback<T, U, V>)mEventTable[eventType] + handle;
    }

    public static void AddListener<T, U, V, X>(FrameEvent eventType, Callback<T, U, V, X> handle)
    {
        OnListenerAdding(eventType, handle);
        mEventTable[eventType] = (Callback<T, U, V, X>)mEventTable[eventType] + handle;
    }

    public static void RemoveListenrer(FrameEvent eventType, Callback handle)
    {

        OnListenerRemoveing(eventType, handle);

        mEventTable[eventType] = (Callback)mEventTable[eventType] - handle;

        OnListenerRemoved(eventType);

    }

    public static void RemoveListenrer<T>(FrameEvent eventType, Callback<T> handle)
    {
        OnListenerRemoveing(eventType, handle);

        mEventTable[eventType] = (Callback<T>)mEventTable[eventType] - handle;

        OnListenerRemoved(eventType);
    }

    public static void RemoveListenrer<T, U>(FrameEvent eventType, Callback<T, U> handle)
    {
        OnListenerRemoveing(eventType, handle);

        mEventTable[eventType] = (Callback<T, U>)mEventTable[eventType] - handle;

        OnListenerRemoved(eventType);
    }

    public static void RemoveListenrer<T, U, V>(FrameEvent eventType, Callback<T, U, V> handle)
    {
        OnListenerRemoveing(eventType, handle);

        mEventTable[eventType] = (Callback<T, U, V>)mEventTable[eventType] - handle;

        OnListenerRemoved(eventType);
    }

    public static void RemvoeListenrer<T, U, V, X>(FrameEvent eventType, Callback<T, U, V, X> handle)
    {
        OnListenerRemoveing(eventType, handle);

        mEventTable[eventType] = (Callback<T, U, V, X>)mEventTable[eventType] - handle;

        OnListenerRemoved(eventType);
    }
    /// <summary>
    /// Clear All EventAndData
    /// </summary>
    public static void RemoveAllLIstener()
    {
        List<Delegate> temp = new List<Delegate>(mEventTable.Values);

        for (int i = 0; i < temp.Count; i++)
        {
            //Delegate.Remove( temp[i], (Delegate)temp[i].Target);
            temp[i] = null;
        }

        mPermanenMessage = new List<FrameEvent>();

        mEventTable = new Dictionary<FrameEvent, Delegate>();
    }

    public static void Broadcast(FrameEvent eventType)
    {
        OnBroadcasting(eventType);

        Delegate d;

        if (mEventTable.TryGetValue(eventType, out d))
        {
            Callback callback = d as Callback;
            if (callback != null)
            {
                callback();
            }
            else
            {
                throw new BroadcastException(eventType.ToString());
            }
        }
    }

    public static void Broadcast<T>(FrameEvent eventType, T t)
    {
        OnBroadcasting(eventType);

        Delegate d;

        if (mEventTable.TryGetValue(eventType, out d))
        {
            Callback<T> callback = d as Callback<T>;
            if (callback != null)
            {
                callback(t);
            }
            else
            {
                throw new BroadcastException(eventType.ToString());
            }
        }

    }

    public static void Broadcast<T, U>(FrameEvent eventType, T t, U u)
    {
        OnBroadcasting(eventType);

        Delegate d;

        if (mEventTable.TryGetValue(eventType, out d))
        {
            Callback<T, U> callback = d as Callback<T, U>;
            if (callback != null)
            {
                callback(t, u);
            }
            else
            {
                throw new BroadcastException(eventType.ToString());
            }
        }
    }

    public static void Broadcast<T, U, V>(FrameEvent eventType, T t, U u, V v)
    {
        OnBroadcasting(eventType);

        Delegate d;

        if (mEventTable.TryGetValue(eventType, out d))
        {
            Callback<T, U, V> callback = d as Callback<T, U, V>;
            if (callback != null)
            {
                callback(t, u, v);
            }
            else
            {
                throw new BroadcastException(eventType.ToString());
            }
        }
    }

    public static void Broadcast<T, U, V, X>(FrameEvent eventType, T t, U u, V v, X x)
    {
        OnBroadcasting(eventType);

        Delegate d;

        if (mEventTable.TryGetValue(eventType, out d))
        {
            Callback<T, U, V, X> callback = d as Callback<T, U, V, X>;
            if (callback != null)
            {
                callback(t, u, v, x);
            }
            else
            {
                throw new BroadcastException(eventType.ToString());
            }
        }
    }
}



FR:徐海涛(hunk Xu) QQ技术交流群:386476712

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值