Ejemplo n.º 1
0
    /// <summary>
    /// Plays a 2D audio clip with the given alias
    /// </summary>
    public GameObject Play2D(AudioClip clip, AudioType audioType, AudioSourceData2D audioSourceData = null, float delayInSeconds = 0, Action onStart = null, Action onComplete = null)
    {
        AudioSourceData3D audioSourceData3D = new AudioSourceData3D();

        if (audioSourceData != null)
        {
            audioSourceData3D.loop             = audioSourceData.loop;
            audioSourceData3D.randomPitchRange = audioSourceData.randomPitchRange;
            audioSourceData3D.pitchOverride    = audioSourceData.pitchOverride;
            audioSourceData3D.volume           = audioSourceData.volume;
            audioSourceData3D.parent           = audioSourceData.parent;
        }

        audioSourceData3D.maxDistance      = 999;
        audioSourceData3D.minDistance      = 999;
        audioSourceData3D.relativeVelocity = Vector3.zero;
        audioSourceData3D.spatialBlend     = 0;

        return(Play3D(clip, Camera.main.transform.position, audioType, audioSourceData3D, delayInSeconds, onStart, onComplete));
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Plays a 3D sound at the given position
    /// An GameObject with an AudioSource is spawned at the given position and is removed once the audio finishes
    /// </summary>
    /// <returns>Reference to the AudioSource that was spawned</returns>
    public GameObject Play3D(AudioClip clip, Vector3 position, AudioType audioType, AudioSourceData3D audioSourceData, float delayInSeconds = 0, Action onStart = null, Action onComplete = null)
    {
        if (clip == null)
        {
            return(null);
        }

        if (audioSourceData == null)
        {
            audioSourceData = new AudioSourceData3D();
        }

        GameObject obj = new GameObject((audioSourceData.spatialBlend == 0) ? "[2D_AudioSource] - " + clip.name : "[3D_AudioSource] - " + clip.name);

        obj.transform.parent   = ((audioSourceData.parent != null) ? audioSourceData.parent : transform);
        obj.transform.position = position;

        const float minMagnitude = 0;
        const float maxMagnitude = 5;

        AudioSource spawnedAudioSource = obj.AddComponent <AudioSource>();

        spawnedAudioSource.clip         = clip;
        spawnedAudioSource.volume       = audioSourceData.volume + (Mathf.Clamp(audioSourceData.relativeVelocity.magnitude, minMagnitude, maxMagnitude) / maxMagnitude); // Higher velocity = louder;
        spawnedAudioSource.spatialBlend = audioSourceData.spatialBlend;
        spawnedAudioSource.minDistance  = audioSourceData.minDistance;
        spawnedAudioSource.maxDistance  = audioSourceData.maxDistance;

        if (audioSourceData.pitchOverride != 0)
        {
            spawnedAudioSource.pitch = audioSourceData.pitchOverride;
        }
        else
        {
            spawnedAudioSource.pitch = 1 + UnityEngine.Random.Range(-Mathf.Abs(audioSourceData.randomPitchRange), Mathf.Abs(audioSourceData.randomPitchRange));
        }

        spawnedAudioSource.rolloffMode = AudioRolloffMode.Logarithmic;
        spawnedAudioSource.loop        = audioSourceData.loop;

        switch (audioType)
        {
        case AudioType.Queue:
            m_AudioQueue.Enqueue(new AudioQueueInfo
            {
                is3D           = (audioSourceData.spatialBlend == 0) ? false : true,
                audioSource    = spawnedAudioSource,
                audioClip      = clip,
                delayInSeconds = delayInSeconds,
                onComplete     = onComplete,
                onStart        = onStart
            });

            if ((m_3DAudioSource == null || (m_3DAudioSource != null && m_3DAudioSource.isPlaying == false)))
            {
                PlayNext();
            }
            break;

        case AudioType.Additive:
            spawnedAudioSource.PlayDelayed(delayInSeconds);

            if (!audioSourceData.loop)
            {
                StartCoroutine(PlayAdditiveRoutine(spawnedAudioSource.gameObject, (spawnedAudioSource.clip.length / spawnedAudioSource.pitch) + delayInSeconds, onStart, onComplete));
            }
            break;

        default:
            Debug.LogError("Unhandled Audio type");
            break;
        }

        return(obj);
    }