예제 #1
0
    IEnumerator PlayRandomSound()
    {
        playSound = false;
        StartCoroutine(TimeRandom());
        int             clipIndex          = Random.Range(0, soundClips.Length);
        RandomAudioClip selectedClip       = soundClips[clipIndex];
        float           distanceFromPlayer = Random.Range(selectedClip.distMin, selectedClip.distMax);
        float           heightFromSource   = Random.Range(selectedClip.heightMin, selectedClip.heightMax);
        // --Polar coordinates--
        // The axes
        // pos x = +z
        // pos y = -x
        float       angleFromSource = Random.Range(selectedClip.angleMin, selectedClip.angleMax);
        Vector3     newDir          = PolarToCartesian(angleFromSource, distanceFromPlayer, heightFromSource);
        GameObject  newAudio        = Instantiate(randomAudioSource, newDir, transform.rotation);
        AudioSource randomSource    = newAudio.GetComponent <AudioSource>();

        randomSource.clip        = selectedClip.audioClip;
        randomSource.volume      = selectedClip.volume;
        randomSource.pitch       = Random.Range(selectedClip.pitchMin, selectedClip.pitchMax);
        randomSource.maxDistance = selectedClip.radius;
        randomSource.Play();
        yield return(new WaitForSeconds(selectedClip.audioClip.length));

        Destroy(newAudio);
    }
예제 #2
0
    public static AudioClip GetRandomClipFromDef(SoundDef def)
    {
        if (def == null)
        {
            return(null);
        }
        if (def.m_RandomClips == null)
        {
            return(null);
        }
        if (def.m_RandomClips.Count == 0)
        {
            return(null);
        }
        float max = 0f;

        foreach (RandomAudioClip clip in def.m_RandomClips)
        {
            max += clip.m_Weight;
        }
        float num2 = UnityEngine.Random.Range(0f, max);
        float num3 = 0f;
        int   num4 = def.m_RandomClips.Count - 1;

        for (int i = 0; i < num4; i++)
        {
            RandomAudioClip clip2 = def.m_RandomClips[i];
            num3 += clip2.m_Weight;
            if (num2 <= num3)
            {
                return(clip2.m_Clip);
            }
        }
        return(def.m_RandomClips[num4].m_Clip);
    }
예제 #3
0
    void BuildChanceArray()
    {
        List <int> newChanceArray = new List <int>();

        for (int i = 0; i < sounds.Length; i++)
        {
            RandomAudioClip clip = sounds[i];


            if (clip.chance == 0)
            {
                continue;                   // skip if zero chance
            }
            // Multiply the amount of available clips by the chance, so a clip with 0.5 chance in a selection of 10 clips
            // leads to 5 of that index in the chance array, and 0.1 chance leads to 1 of that index
            // we also make sure it doesn't go below 1 so there will always be one clip in the array
            for (int x = 0; x < Mathf.Max(sounds.Length * clip.chance, 1); x++)
            {
                newChanceArray.Add(i);
            }
        }


        _chanceArray = newChanceArray.ToArray();
    }
예제 #4
0
 public static bool IsVOClip(RandomAudioClip randomClip)
 {
     if (randomClip == null)
     {
         return(false);
     }
     return(IsVOClip(randomClip.m_Clip));
 }
예제 #5
0
    public void PlayAudio(AudioType audioType)
    {
        RandomAudioClip audioPlayer = ReturnMatchingAudio(audioType);

        if (audioPlayer == null)
        {
            return;
        }

        audioPlayer.PlayRandomAudioClip();
    }