A small but useful csharp script will be attached to this article, and it will be shown how to use it.
The reason for writing the script was that there was a need to set up and test sound effects without starting the project scene. And also in tracking the main replay events.
The script works in the same way as in PlayMode and in EditMode, and allows you to:
1. Play the sound with the necessary delay and track the start of playback.
2. Track the end of the sound, including every moment of the completion of looped playback.
3. Track unplanned end of sound reproduction.
4. Use the event to track and change parameters during playback.
To reproduce sound, static methods are used:
public static SoundTrack PlaySound(AudioClip clip, float volume = 1, float pitch = 1, float loopTime = 0, float delayTime = 0); public static SoundTrack PlaySound(GameObject target, AudioClip clip, float volume = 1, float pitch = 1, float loopTime = 0, float delayTime = 0);
These methods return an instance of the SoundTrack class to which you can later attach the necessary events. The first method creates a GameObject on the scene, the second adds the SoundTrack and AudioSource components to the specified GameObject.
')
The parameters volume and pitch do not need to be presented.
loopTime - can be used to set the time in seconds, which will last the playback cycle. If set to 0, the sound will play only once, if set to float.PositiveInfinity, the sound will play indefinitely.
delayTime is the delay before the sound is played in seconds.
Note:
After loopTime, the first method will delete the GameObject created for the sound, the second method will only remove the components created for the sound.
So, the simplest example of code to play sound in the game.
public AudioClip testSound; void Start () { SoundTrack.PlaySound (testSound); }
And to play sound in the editor.
[MenuItem("MyMenu/TestSound #F1")] static void TestSound(){ AudioClip[] clips=Resources.FindObjectsOfTypeAll<AudioClip>(); if(clips==null||clips.Length==0){ Debug.LogError("No clips in the resources!"); return; } SoundTrack.PlaySound(clips[0]); }
Now you can try to figure out the events. I think for clarity, it is better to immediately take an example for the editor, since in the game mode everything works in a similar way.
This example starts with the SHIFT + F1 key combination, and shows how to use events.
using UnityEngine; using UnityEditor; using System.Collections; public class SoundTrackTest : Editor { [MenuItem("MyMenu/TestSound #F1")] static void TestSound(){ clips=Resources.FindObjectsOfTypeAll<AudioClip>(); Debug.Log("clips " + clips.Length); if(clips==null||clips.Length==0){ Debug.LogError("No clips in the resources!"); return; } rePlayCount = 0; StartNextSound (0); } static AudioClip[] clips; static int rePlayCount=0; static void StartNextSound(float timePosition){ SoundTrack track=SoundTrack.PlaySound(clips[Random.Range(0,clips.Length-1)]); rePlayCount++;
In order for the example to work, this script must be placed in the Editor folder, as well as having at least one sound file in the project resources. If you need a test in a running project, you can simply transfer the code to the Start function from the TestSound function, and if necessary, make all the properties and methods non-static.
void Start(){ clips=Resources.FindObjectsOfTypeAll<AudioClip>(); Debug.Log("clips " + clips.Length); if(clips==null||clips.Length==0){ Debug.LogError("No clips in the resources!"); return; } rePlayCount = 0; StartNextSound (0); }
This event is triggered directly at the beginning of the sound playback.
track.start_action += soundStartEvent;
Those. if delayTime is> 0, it will not work when creating a sound, but when you start playback.
This event can be useful in some cases.
track.processing += soundProcessEvent;
For example, you can set different events for the music and effects, and adjust the volume of all the music in the game with one variable and the other with all the effects. And then add to the effects and music another event, in which you can already adjust the speed of their playback, if the game has a Slow Motion effect.
In this example, the sound should change the volume for each second.
track.complete_action += soundCompleteEvent;
This event can be useful in a situation where loopTime exceeds the length of the sound file, this means that the sound will be played cyclically. In this case, the event will be triggered at the moment when the sound starts anew.
In other cases, the call of this event is not guaranteed.
track.destroy_action += soundDestroyEvent;
This event will work in 2 cases. The first case is when sound playback is completed, after loopTime expires or during one-time playback. The second case is the unrelated sound script from the stage (for example, manually removing an object from the scene hierarchy). You can distinguish one case from another using the atEndOfSound parameter; if it is false, then this is just the second case.
In the second case, we will no longer have access to the GameObject and sound components.
If, for example, you need to consistently play several melodies and create a feeling of continuous sound, you can try to do it as shown in the example in the soundDestroyEvent function.
In addition, the script has parameters that may be needed:
- time_position - the playback position of the audio file, from 0 to the length of the file;
- life_time - time from the beginning of the sound playback;
- playing_time - time spent on sound reproduction, does not take into account pauses;
- loop_time - the time that the sound will play;
- delay_time - time delay before playback;
- startTime - the moment the sound starts playing;
- created_time - the moment of sound creation (startTime-delay_time).
All the time is calculated relative to
Time.realtimeSinceStartup .
Here you can download the script itself and the above example.
If the keyboard shortcut does not work in the example, you can find the TestSound item in the menu.
If Unity does not find the sound in the resources, you just need to select it (see the settings of the audio file).