Welcome, guest! Login / Register - Why register?
[email protected] webmail now available. Want one? Go here.

Paste

Pasted as C# by Ondrej ( 1 year ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Playables;

/// <summary>
/// Playable graph for audio crossfading.
/// </summary>
public class AudioCrossfadeGraph
{
    PlayableGraph m_graph;
    AudioClipPlayable[] m_clipPlayables;
    AudioMixerPlayable m_mixerPlayable;
    AudioPlayableOutput m_output;
    AudioCrossfadePlayable m_crossfadeBehaviour;
    ScriptPlayable<AudioCrossfadePlayable> m_crossfadePlayable;

    public bool IsPlaying
    {
        get { return m_graph.IsValid() && m_graph.IsPlaying(); }
    }

    public double Time
    {
        get { return m_crossfadePlayable.GetTime(); }
    }

    public PlayableGraph Graph
    {
        get { return m_graph; }
    }

    public AudioMixerPlayable Mixer
    {
        get { return m_mixerPlayable; }
    }

    public ArrayReader<AudioClipPlayable> ClipPlayables
    {
        get { return m_clipPlayables; }
    }

    public ScriptPlayable<AudioCrossfadePlayable> CallbackPlayable
    {
        get { return m_crossfadePlayable; }
    }

    public int CurrentClipIndex
    {
        get { return m_crossfadeBehaviour.CurrentIndex; }
    }

    public PlayableOutput Output
    {
        get { return m_output; }
    }

    public void Create(AudioSource audioSource, AudioClip[] clips)
    {
        Destroy();

        m_graph = PlayableGraph.Create("AudioCrossfadeGraph");
        m_graph.SetTimeUpdateMode(DirectorUpdateMode.DSPClock);
        m_graph.Stop();

        m_mixerPlayable = AudioMixerPlayable.Create(m_graph, clips.Length);

        m_clipPlayables = new AudioClipPlayable[clips.Length];
        for (int i = 0; i < clips.Length; i++)
        {
            m_clipPlayables[i] = AudioClipPlayable.Create(m_graph, clips[i], true);
            m_mixerPlayable.ConnectInput(i, m_clipPlayables[i], 0, i == 0 ? 1 : 0);
        }

        m_crossfadePlayable = AudioCrossfadePlayable.Create(m_graph, m_mixerPlayable);
        m_crossfadeBehaviour = m_crossfadePlayable.GetBehaviour();

        m_output = AudioPlayableOutput.Create(m_graph, "AudioCrossfade", audioSource);
        m_output.SetSourcePlayable(m_crossfadePlayable);
    }

    public void Destroy()
    {
        if (m_graph.IsValid())
        {
            m_graph.Destroy();
        }
    }

    public void Crossfade(int clipIndex, float fadeDuration)
    {
        if (clipIndex >= 0 && !m_graph.IsPlaying())
        {
            m_graph.Play();
        }
        m_crossfadeBehaviour.Crossfade(clipIndex, fadeDuration);
    }

    public void Switch(int clipIndex)
    {
        if (clipIndex >= 0 && !m_graph.IsPlaying())
        {
            m_graph.Play();
        }
        m_crossfadeBehaviour.Switch(clipIndex);
    }

    /// <summary>
    /// Plays or continues playing the audio.
    /// </summary>
    public void Play()
    {
        if (!m_graph.IsPlaying())
        {
            m_graph.Play();
        }
    }

    /// <summary>
    /// Pauses the audio.
    /// </summary>
    public void Pause()
    {
        if (m_graph.IsPlaying())
        {
            m_graph.Stop();
        }
    }
}

 

Revise this Paste

Your Name: Code Language: