示例#1
0
        public SoundHandler Play(ISound sound)
        {
            if (sound == null)
            {
                return(null);
            }

            var mixerGroup    = sound.GetMixedGroup();
            var soundGroup    = sound.GetGroup();
            var soundsInGroup = 0;

            if (soundGroup != null)
            {
                var groupId = soundGroup.GetId();
                if (_groupCounter.ContainsKey(groupId))
                {
                    soundsInGroup = _groupCounter[groupId];
                    if (soundsInGroup >= soundGroup.GetMaxConcurrentSounds())
                    {
                        //Debug.Log(string.Format("[SoundManager] Too many sounds for group {0}", sound.MixerGroup));
                        return(null);
                    }
                }
            }

            var pitch = sound.GetPitch();
            var delay = sound.GetDelay();
            var clip  = sound.GetAudioClip();

            var handler = Play(
                clip,
                sound.GetVolumeModifier(),
                sound.IsOnLoop(),
                pitch,
                sound.ShouldIgnoreListenerPause(),
                delay,
                mixerGroup,
                sound.GetGroup()
                );

            if (soundGroup != null && handler != null)
            {
                var groupId = soundGroup.GetId();
                if (_groupCounter.ContainsKey(groupId))
                {
                    // There are sounds in group so increment by one
                    _groupCounter[groupId] = soundsInGroup + 1;
                }
                else
                {
                    // First sound in group
                    _groupCounter.Add(groupId, 1);
                }
            }

            return(handler);
        }