Custom AudioSource Settings
The playback prefab is how Dissonance plays the audio signal from each player. A copy of the prefab is instantiated for each player and then moved into the correct position for positional audio to work. Creating your own playback prefab allows you to customise the AudioSource settings used for voice or attach your own script to the prefab. To use a custom prefab drag the prefab into the
Playback Prefab field on the Dissonance Comms component inspector.
If no prefab is set Dissonance will automatically use a default prefab.
The playback prefab must include a
VoicePlayback component (part of Dissonance).
You may also attach a Unity
AudioSource component, in which case you can adjust some of the settings to change how voice will be played back. However, the following settings will be overwritten by Dissonance:
- Play On Awake
When writing your own scripts to attach to the playback prefab it is important to remember that the lifetime is managed entirely by Dissonance. Prefab instances are recycled to reduce the amount of garbage created. This means that your custom script attached to the prefab must be able to handle being re-assigned from one player to another.
When there are no instances available to use, a new one is created:
- Prefab instantiated
- Default components added
When the player for an instance leaves the prefab is recycled:
- Stored in a pool of inactive instances
When another player joins an instance is retrieved and re-used:
- Retrieved from pool
To handle this in your script simply use the normal Unity lifecycle events:
// This only runs once. Use this to perform one-time setup.
// e.g. Find some Dissonance components
_playbackComponent = GetComponent<VoicePlayback>();
_dissonanceComms = FindObjectOfType<DissonanceComms>();
// This runs every time the script is activated. Use this to perform per-player setup
// e.g. find information about this player
_playerState = _dissonanceComms.FindPlayer(_playbackComponent.PlayerName);
// This will run every frame while the script is active
// This runs every time the script is deactivated. Use this to perform per-player cleanup
// e.g. Remove the things which were initialised in OnEnable
_playerState = null;