示例#1
0
    public void PlayCustomSound(string clip, Vector3 soundPos, string entityToFollow, float volume, bool useGroup)
    {
        // Get correct audio clip to play
        AudioClip clipToPlay = null;

        if (useGroup)
        {
            clipToPlay = soundLibrary.GetGroupClip(clip);
        }
        else
        {
            clipToPlay = soundLibrary.GetClip(clip);
        }

        if (clipToPlay == null)
        {
            return;
        }

        // Setup sound game object
        GameObject  newSFXSource   = new GameObject("SFX2D source");
        AudioSource newAudioSource = newSFXSource.AddComponent <AudioSource> ();

        newSFXSource.transform.parent = transform;
        // Sound volume settings
        newAudioSource.maxDistance  = 30;
        newAudioSource.spatialBlend = .96f;
        // Play audio and destroy it after X amount of time
        newAudioSource.PlayOneShot(clipToPlay, masterVolume * sfxVolume * volume * volume);
        StartCoroutine(DestroyCustomSFXSource(newSFXSource));
    }