Пример #1
0
        void Play(MusicZone zone)
        {
            if (debug)
            {
                Debug.Log("Attempting a play of " + zone.name);
            }

            if (currentTrackSource)
            {
                // Pause the currently playing audio source if it's playing a different track than the requested zone
                if (zone.musicTrack != currentTrackSource.MyTrack)
                {
                    currentTrackSource.Pause(CurrentTrackFadeOutTime());
                }
            }

            TrackAudioSource newTrackAudioSource = null;

            // If an audio source stack has already been built for the
            // music track of this zone, just change zones on it.
            if (trackSources.TryGetValue(zone.musicTrack, out newTrackAudioSource))
            {
                newTrackAudioSource.ChangeZones(zone, zone.fadeInTime);
                newTrackAudioSource.Play(zone.fadeInTime);
                currentTrackSource = newTrackAudioSource;
                return;
            }

            // if we don't have an audio source stack for this track yet,
            // build one and set it as the new current track
            newTrackAudioSource = BuildNewTrackAudioSource(zone);
            currentTrackSource  = newTrackAudioSource;
        }
Пример #2
0
        public void BuildAudioSources(MusicZone newMusicZone, AudioMixerGroup mixerGroup)
        {
            musicZone    = newMusicZone;
            track        = musicZone.musicTrack;
            trackVolumes = trackVolumesOutput = newMusicZone.GenerateVolumes();

            // Create an audio source for each layer
            for (int i = 0; i < musicZone.layers.Count; i++)
            {
                var layer = musicZone.layers[i];

                // instantiate a source
                var newAudioSource = gameObject.AddComponent <AudioSource>();
                newAudioSource.spatialBlend          = 0;
                newAudioSource.clip                  = layer.clip;
                newAudioSource.loop                  = false;
                newAudioSource.volume                = 0;
                newAudioSource.playOnAwake           = false;
                newAudioSource.outputAudioMixerGroup = mixerGroup;

                sources.Add(newAudioSource);
            }

            Play(musicZone.fadeInTime);
        }
Пример #3
0
        void I_ExitZone(MusicZone zone)
        {
            if (activeZones.Remove(zone))
            {
                if (debug)
                {
                    Debug.Log("Music zone " + zone + " was exited.");
                }

                RecalculatePriority();
            }
        }
Пример #4
0
        void I_EnterZone(MusicZone zone)
        {
            if (activeZones.Add(zone))
            {
                if (debug)
                {
                    Debug.Log("Music zone " + zone + " was newly entered.");
                }

                RecalculatePriority();
            }
        }
Пример #5
0
        void Stop(MusicZone zone)
        {
            if (!currentTrackSource)
            {
                return;
            }
            if (currentTrackSource.musicZone != zone)
            {
                return;
            }

            currentTrackSource.Pause(CurrentTrackFadeOutTime());
        }
Пример #6
0
        TrackAudioSource BuildNewTrackAudioSource(MusicZone newZone)
        {
            GameObject newGO = new GameObject();

            newGO.name                    = "track source: " + newZone.musicTrack.name;
            newGO.transform.parent        = transform;
            newGO.transform.localPosition = Vector3.zero;
            TrackAudioSource newTrack = newGO.AddComponent <TrackAudioSource>();

            newTrack.BuildAudioSources(newZone, mixerGroup);

            trackSources.Add(newZone.musicTrack, newTrack);
            return(newTrack);
        }
Пример #7
0
        public void LoopTrack(MusicZone musicZone)
        {
            // remove the old one from the dictionary
            var t = musicZone.musicTrack;

            trackSources.Remove(t);

            // re-create it
            var newTrackSource = BuildNewTrackAudioSource(musicZone);

            // if the looping track was the current playing, carryover that status
            if (currentTrackSource.musicZone == musicZone)
            {
                currentTrackSource = newTrackSource;
            }
        }
Пример #8
0
        /// <summary>
        /// Change the music zone I'm playing for.
        /// This ONLY supports swapping between zones that share the same track!
        /// </summary>
        public void ChangeZones(MusicZone newMusicZone, float newFadeTime = 1)
        {
            if (newMusicZone == musicZone)
            {
                return;
            }

            string oldName = musicZone ? musicZone.name : "null";

            Debug.Log("Track " + MyTrack.name + " is changing zones from " + oldName + " to " + newMusicZone.name);
            if (newMusicZone.musicTrack != MyTrack)
            {
                Debug.LogError(name + " trying to change music zones, but the current and new zones don't " +
                               "have the same tracks.", gameObject);
                return;
            }

            musicZone    = newMusicZone;
            trackVolumes = newMusicZone.GenerateVolumes();
            fadeInTime   = newFadeTime;
        }
Пример #9
0
 public static void ExitZone(MusicZone zone)
 {
     GuaranteeInstance();
     instance.I_ExitZone(zone);
 }