Exemplo n.º 1
0
    private void _PlaySound(Sound sound, bool randomPitch)
    {
        SoundSystemDictionaryEntry entry = GetEntry(sound);
        AudioClip c = entry.GetRandomClip();

        if (c == null || !CanPlaySoundAndMarkSoundPlayed(sound))
        {
            return;
        }

        GameObject clip = new GameObject("AudioClip: " + sound.ToString());

        clip.transform.SetParent(audioSourceParent);

        AudioSource audioSource = clip.AddComponent <AudioSource>();

        if (randomPitch)
        {
            audioSource.pitch = Random.Range(0.5f, 1.5f);
        }
        audioSource.volume = entry.volume;

        clip.AddComponent <DestroyAfterTime>().time = c.length;

        audioSource.PlayOneShot(c);
    }
Exemplo n.º 2
0
    private void _ChangeBackgrundMusic(Sound sound, float transitionTime, bool overrideOld)
    {
        SoundSystemDictionaryEntry entry = GetEntry(sound);

        AudioClip clip = entry.GetRandomClip();

        if (!overrideOld && currentBackgroundMusic != null && clip == currentBackgroundMusic.clip)
        {
            return;
        }

        GameObject go = new GameObject("AudioClip: " + sound.ToString());

        go.transform.SetParent(audioSourceParent);
        //DontDestroyOnLoad(go);

        AudioSource audioSource = go.AddComponent <AudioSource>();

        audioSource.volume = entry.volume;
        audioSource.clip   = clip;
        audioSource.loop   = true;
        audioSource.Play();

        if (transitionTime <= 0 || currentBackgroundMusic == null)
        {
            if (currentBackgroundMusic != null)
            {
                currentBackgroundMusic.Stop();
                Destroy(currentBackgroundMusic.gameObject);
            }

            currentBackgroundMusic = audioSource;
        }
        else
        {
            StartCoroutine(TransitionBackgroundMusic(audioSource, transitionTime));
        }
    }