unity TimeLine播放多个音效

在unity中,TimeLine的功能还是非常的强大,可以做动画和视屏。
在项目中遇到一个场景要用到非常多个对话,而且在播放完一个对话时,需要停顿一下。我感觉有点繁琐。
在unity中PlayableDirector是存放TimeLine的,通过这个组件获取TimeLine中的每个片段的全部属性 。
我是要与多个TimeLine进行切换,并播放操作,还是挺简单的。


 public PlayableDirector Director;

    public Dictionary bindingDict = new Dictionary();

    public List wordTimeAsset = new List();
       
    private string bindingDictName= "Audio";

    //变量
    private  int index=0;
    private  double time=0;
    TrackAsset currentTrack;

    //赋值变量
    private bool Limit = false;
    private bool Over = false;

    public int CcurrentIndex=0;

    private void Start()
    {
        CcurrentIndex = (int)PlayerUIButton.seletItem;
        InitAttributes();

        EventCenter.AddListener(SeletType.TimeLinePlay, TimeLinePlay);                          //注册播放TimeLine
        EventCenter.AddListener(SeletType.TimeLineReset, ResetTimeLine);                     //注册重置TimeLine
        EventCenter.AddListener(SeletType.TriggerPlay, TriggerPlay);                                 //注册触发播放音效
        EventCenter.AddListener(SeletType.ReplaceTimeLine, ReplaceTmeLine);               //注册替换TimeLine的方法
    }

    private void Update()
    {
        //if (Input.GetKeyDown(KeyCode.A))
        //{
        //    TimeLinePlay();
        //}
        //if (Input.GetKeyDown(KeyCode.B))
        //{
        //    ClickButtonPlay();
        //}
        //if (Input.GetKeyDown(KeyCode.C))
        //{
        //    ResetTimeLine();
        //}

        //if (Input.GetKeyDown(KeyCode.D))
        //{
        //    PlayerUIButton.seletItem = SeletItem.FallingWater;
        //    ReplaceTmeLine();
        //}
        if (CcurrentIndex != (int)PlayerUIButton.seletItem)
        {
            ReplaceTmeLine();
            CcurrentIndex = (int)PlayerUIButton.seletItem;
        }

        if (!Limit)
        {
            if (Director.time >= time)
            {
                Director.Pause();
                Over = true;
                PlayNextAudio();
                Limit = true;
            }
            else
            {
                Over = false;
            }
        }
    }

    /// 
    /// 替换TimeLine
    /// 
    public void ReplaceTmeLine()
    {
        ResetTimeLine();                                 //保险没有重置TimeLine
        int Index = (int)PlayerUIButton.seletItem;
        Director.playableAsset = wordTimeAsset[Index];
        InitAttributes();
        index = 0;
    }

    /// 
    /// 触发播放 
    /// 
    public void  TriggerPlay()
    {
        ClickButtonPlay();
    }

    /// 
    /// 重置TimeLine
    /// 
    public  void ResetTimeLine()
    {
        Over = false;
        index = -1;   //音频的index 
        time = 0;     //时间
        Director.Stop();
    }

    /// 
    /// 播放音效
    /// 
    public void TimeLinePlay()
    {
        Limit = false;
        Director.Play();
    }
    
    /// 
    /// 点击Button播放音效
    /// 
    public void ClickButtonPlay()
    {
        Limit = false;
        if (!Over)
        {
            Director.time = time;
            PlayNextAudio();
        }
        Director.Play();
    }

    /// 
    /// 初始化
    /// 
    private void InitAttributes()
    {
        bindingDict.Clear();
        foreach (PlayableBinding pb in Director.playableAsset.outputs)
        {
            if (!bindingDict.ContainsKey(pb.streamName))
            {
                bindingDict.Add(pb.streamName, pb);
            }
        }
        currentTrack = bindingDict["Audio0"].sourceObject as TrackAsset;
        time = currentTrack.duration;
    }

    /// 
    /// 播放下一个音效
    /// 默认是从第二个Bindings开始播放
    /// 
    public void PlayNextAudio()
    {
        index =(int)Mathf.Repeat((float)++index, (float)bindingDict.Count);
        string name = bindingDictName + index;
        var track = bindingDict[name].sourceObject as TrackAsset;
        time += track.duration;
    }

你可能感兴趣的:(unity TimeLine播放多个音效)