Esempio n. 1
0
        // ********************************************************************
        public static AudioObject Play(AudioInfo _info)
        {
            if (_info.clip == null)
            {
                Debug.LogWarning("Attempt to play null audio clip");
                return(null);
            }

            if (_info.overrideChannelLimit == AudioChannelOverride.NONE && !ChannelAvailable(_info.category))
            {
                return(null);
            }

            // Check duplicate limit
            List <AudioObject> categoryAudio = GetActiveAudioForCategory(_info.category);
            int numActiveClips = 0;

            for (int i = 0; i < categoryAudio.Count; ++i)
            {
                if (categoryAudio[i].audioClip == _info.clip)
                {
                    ++numActiveClips;
                }
            }
            if (_info.allowedDuplicates != 0 && _info.allowedDuplicates <= numActiveClips)
            {
                return(null);
            }

            bool replacing = !ChannelAvailable(_info.category) && _info.overrideChannelLimit == AudioChannelOverride.REPLACE;

            if (replacing)
            {
                AudioObject oldObject = (instance as AudioManager).m_audioObjectPools[_info.category].FirstActive.GetComponent <AudioObject>();

                // If we're trying to replace it with the same thing, don't.
                if (oldObject.audioClip == _info.clip)
                {
                    if (oldObject.fading == true)
                    {
                        oldObject.StopFade();
                        oldObject.Fade(true); // turn it back on if it was fading out
                    }
                    return(oldObject);
                }
                else
                {
                    oldObject.Fade(false);
                }
            }

            GameObject audioGameObject = (instance as AudioManager).m_audioObjectPools[_info.category].RequestObject();

            if (_info.parent != null)
            {
                audioGameObject.transform.SetParent(_info.parent);
            }
            else
            {
                audioGameObject.transform.SetParent(instance.transform);
            }
            audioGameObject.name = "AudioObject - " + _info.clip.name;
            AudioObject audioObject = audioGameObject.GetComponent <AudioObject>();

            audioObject.audioInfo = _info;

            audioObject.Apply();
            audioObject.audioSource.Play();

            if (_info.fadeDuration != 0)
            {
                audioObject.audioSource.volume = 0;
                audioObject.Fade(true);
            }

            return(audioObject);
        }