示例#1
0
        public AudioSource PlayOneShot(AudioClip clip, Vector3 pos, float volume = 1, AudioSource baseSource = null, bool reversed = false)
        {
            if (simultaneousSounds >= MAX_SIMULTANEOUS_DIALOG_SOUNDS)
            {
                for (int i = 0; i < oneshots.Count - MAX_SIMULTANEOUS_DIALOG_SOUNDS - 1; i++)
                {
                    OnOneshotFinish(oneshots[i]);
                }
            }

            GameObject tempGO;

            if (baseSource == null)
            {
                tempGO = new GameObject();                 // create the temp object
            }
            else
            {
                baseSource.playOnAwake = false;
                tempGO = GameObject.Instantiate(baseSource.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
                tempGO.SetActive(true);
            }

            tempGO.name = "_Temp Sound " + clip.name;
            tempGO.transform.SetParent(null);
            tempGO.transform.position = pos;        // set its position

            AudioSource aSource = tempGO.GetComponent <AudioSource>();

            if (aSource == null)
            {
                aSource = tempGO.AddComponent <AudioSource>();                // add an audio source
            }
            aSource.volume     *= volume;
            aSource.clip        = clip; // define the clip
            aSource.playOnAwake = false;
            aSource.loop        = false;
            if (reversed)
            {
                // audio clip must be on Decompress On Load
                aSource.timeSamples = clip.samples - 1;
                aSource.pitch       = -1;
            }
            aSource.Play();        // start the sound
            float lifetime = clip.length;

            if (baseSource != null)
            {
                lifetime = Mathf.Max(12, clip.length * 3);         // ummm hack the lifetime of the clip in case there's effects like reverb
            }

            OneShotSound s = new OneShotSound(tempGO, lifetime);

            oneshots.Add(s);
            simultaneousSounds++;

            return(aSource);       // return the AudioSource reference
        }
示例#2
0
        internal void OnOneshotFinish(OneShotSound Sound)
        {
            if (Sound.markForRemoval)
            {
                return;
            }

            if (Sound.target != null)
            {
                Destroy(Sound.target);
            }
            Sound.markForRemoval = true;
            simultaneousSounds--;
        }