|
1 | 1 | # Documentation |
2 | 2 | It is recommended to view this file in a markdown viewer. |
3 | | -View on [GitHub](https://github.com/DavidF-Dev/Unity-Audio/blob/main/DOCUMENTATION.md). |
| 3 | +View on [GitHub](https://github.com/DavidF-Dev/Unity-Audio/blob/main/DOCUMENTATION.md). |
| 4 | + |
| 5 | +## Usage |
| 6 | +No setup is required. To access the scripts, include the ``DavidFDev.Audio`` namespace at the top of your file. The core functionality of the asset is contained in the ``Audio`` static class, which exposes methods for playing audio clips, sound effects & music. |
| 7 | +- ``Play(AudioClip, Position [optional], AudioMixerGroup [optional])``: play the provided audio clip. |
| 8 | +- ``Play(Path, Position [optional], AudioMixerGroup[optional])``: play an audio clip loaded from the Resources folder. |
| 9 | +- ``PlaySfx(SoundEffect, Position [optional])``: play the provided sound effect. |
| 10 | +- ``PlaySfx(Path, Position [optional])``: play a sound effect loaded from the Resources folder. |
| 11 | +- ``PlayMusic(AudioClip, FadeIn, FadeOut)``: play the provided music track. |
| 12 | +- ``PlayMusic(Path, FadeIn, FadeOut)``: play a music track loaded from the Resources folder. |
| 13 | + |
| 14 | +</br>Calling ``Play()`` or ``PlaySfx()`` will return an instance of Playback which allows control of the Audio Source indirectly. |
| 15 | +E.g. |
| 16 | +```cs |
| 17 | +Playback playback = Audio.PlaySfx(sfx); |
| 18 | +playback.Pause(); |
| 19 | +playback.Volume = 0.4f; |
| 20 | +playback.Loop = true; |
| 21 | +``` |
| 22 | + |
| 23 | +</br>Sound effects are scriptable objects that can be added to your Assets. They are used for setting up specific types of sounds. An instance can be played by calling the ``Play()`` method or by passing it in to ``Audio.PlaySfx()``. |
| 24 | + |
| 25 | +### Examples |
| 26 | +```cs |
| 27 | +Audio.Play("Audio/death_scream", enemy.position); |
| 28 | +``` |
| 29 | +This call will load the `death_scream` Audio Clip from the Resources folder and play it at the enemy's position. |
| 30 | +</br></br> |
| 31 | +```cs |
| 32 | +Audio.PlayMusic(music, fadeIn: 1f, fadeOut: 0.75f); |
| 33 | +Debug.Log($"Current music track: {Audio.CurrentMusic.name}"); |
| 34 | +``` |
| 35 | +Playing a music track will crossfade the new track with the old track, if one was already playing. The currently playing music track can be queried through ``Audio.CurrentMusic``. |
| 36 | +</br></br> |
| 37 | +```cs |
| 38 | +Playback playback = Audio.Play(clip, position: new Vector3(0f, 1f, 0f), output: output); |
| 39 | +playback.ForceFinish(); |
| 40 | +``` |
| 41 | +An Audio Mixer Group can be provided to control where the audio signal is routed. Also, a sound can be stopped prematurely by calling ``ForceFinish()`` on the Playback instance. Note that - unlike with Audio Sources - Playback instances cannot be replayed once the audio has finished playing. |
0 commit comments