Пример #1
0
        /// <summary>
        /// This method will create a new bus with the name you specify.
        /// </summary>
        /// <param name="busName">The name of the new bus.</param>
        /// <param name="errorOnExisting">Whether to log an error if the bus already exists (same name).</param>
        public static bool CreateBus(string busName, bool errorOnExisting = true) {
            var match = GroupBuses.FindAll(delegate(GroupBus obj) {
                return obj.busName == busName;
            });

            if (match.Count > 0) {
                if (errorOnExisting) {
                    LogError("You already have a bus named '" + busName + "'. Not creating a second one.");
                }
                return false;
            }

            var newBus = new GroupBus { busName = busName };

            var busVol = PersistentAudioSettings.GetBusVolume(busName);
            GroupBuses.Add(newBus);

            if (busVol.HasValue) {
                SetBusVolumeByName(busName, busVol.Value);
            }

            return true;
        }
Пример #2
0
        private static void SetBusVolume(GroupBus bus, float newVolume) {
            bus.volume = newVolume;

            // ReSharper disable TooWideLocalVariableScope
            AudioInfo aInfo;
            AudioSource aSource;
            AudioGroupInfo aGroup;
            SoundGroupVariation aVar;
            // ReSharper restore TooWideLocalVariableScope

            foreach (var key in AudioSourcesBySoundType.Keys) {
                aGroup = AudioSourcesBySoundType[key];
                var groupBus = GetBusByIndex(aGroup.Group.busIndex);

                if (groupBus == null || groupBus.busName != bus.busName) {
                    continue;
                }

                // ReSharper disable once ForCanBeConvertedToForeach
                for (var i = 0; i < aGroup.Sources.Count; i++) {
                    aInfo = aGroup.Sources[i];
                    aSource = aInfo.Source;

                    if (!aInfo.Variation.IsPlaying) {
                        continue;
                    }

                    var grpVol = aGroup.Group.groupMasterVolume * bus.volume * Instance._masterAudioVolume;
                    var newVol = (aInfo.OriginalVolume * aInfo.LastPercentageVolume * grpVol) + aInfo.LastRandomVolume;
                    aSource.volume = newVol;

                    aVar = aSource.GetComponent<SoundGroupVariation>();
                    aVar.SetGroupVolume = grpVol;
                }
            }
        }
Пример #3
0
        private static void StopOldestSoundOnBus(GroupBus bus) {
            var busIndex = GetBusIndex(bus.busName, true);

            if (busIndex < 0) {
                return;
            }

            var sources = AudioSourcesBySoundType.GetEnumerator();

            // ReSharper disable TooWideLocalVariableScope
            MasterAudioGroup aGroup;
            AudioGroupInfo aInfo;
            // ReSharper restore TooWideLocalVariableScope
            SoundGroupVariation oldestVar = null;
            var oldestVarPlayTime = -1f;

            while (sources.MoveNext()) {
                aInfo = sources.Current.Value;
                aGroup = aInfo.Group;
                if (aGroup.busIndex != busIndex) {
                    continue;
                }

                // group has same bus, check for time played.
                if (aGroup.ActiveVoices == 0) {
                    continue; // nothing playing, look in next group
                }

                // ReSharper disable once ForCanBeConvertedToForeach
                for (var i = 0; i < aInfo.Sources.Count; i++) {
                    var aVar = aInfo.Sources[i].Variation;
                    if (!aVar.PlaySoundParm.IsPlaying) {
                        continue;
                    }

                    if (oldestVar == null) {
                        oldestVar = aVar;
                        oldestVarPlayTime = aVar.LastTimePlayed;
                    } else if (aVar.LastTimePlayed < oldestVarPlayTime) {
                        oldestVar = aVar;
                        oldestVarPlayTime = aVar.LastTimePlayed;
                    }
                }
            }

            if (oldestVar != null) {
                oldestVar.Stop();
            }
        }