Simple Music Controller for Unity providing smooth audio transitions without gain loss and desync issues
Unity Music Flow Controller is an simple music control system for Unity that provides smooth transitions between musical tracks without unwanted volume drop. The default "Equal Power Crossfade" mode maintain consistent volume during track changes.
- Open Window > Package Manager in Unity
- Click + and select Add package from git URL
- Paste:
https://github.com/TheStami/Unity-MusicFlowController.git
Add MusicController component to object.
You can add as many music tracks as you want. Name is optional. Add AutioClip to it.
Volume is is the target volume after playing the song, you can control maximum volume here or add Audio Mixer Group.
When you got component (GetComponent<MusicController>()), you can control tracks by index or by giving MusicTrack list.
// Play single track
musicController.PlayTrack(0); // Play first track
// Play with stop others
musicController.PlayTrack(1, true); // Play second, stop all others
// Stop all tracks
musicController.StopAllTracks();
// Control multiple tracks simultaneously
musicController.PlayTracks(
new int[] { 0, 2 }, // Tracks to start
new int[] { 1 } // Tracks to stop
);
List<MusicTrack> allTracks = musicController.MusicTracks;
musicController.PlayTracks(
allTracks.Where(x => x.name == "happy").ToArray();
allTracks.Where(x => x.name == "sad").ToArray();
);| Parameter | Type | Description | Default Value |
|---|---|---|---|
musicTracks |
List | List of music tracks | - |
volume |
float (0-1) | Destination volume | 1.0 |
transitionSpeed |
float | Transition speed | 2.0 |
transitionMode |
MusicTransitionType | Transition type | EqualPowerCrossfade |
audioMixerGroup |
AudioMixerGroup | Audio Mixer Group | - |
- Benefits: Maintains consistent volume during transitions
- Algorithm: Uses sine/cosine functions for smooth transitions
- Benefits: Simple, predictable transitions, with audible transitions between tracks
- Algorithm: Linear volume interpolation
- Benefits: Smooth transitions with acceleration
- Algorithm: Lerp interpolation with curve
This project is licensed under the MIT License. See the LICENSE file for details.
