Sound

Built-in

By default is used for all sounds in the project (if FMOD is not installed).

How To Use

  1. Create Sound Data in the Unity project.

    _images/SoundDataCreation.png
  2. Assign your desired AudioClip.

    _images/SoundDataBuiltin.png

    Note

    Sound ID is created automatically.

  3. Press Add To Container button to add sound to the Sound container.

    _images/FMOD-SoundServiceAddedExample.png
  4. Now, we can trigger the sound using the code examples below.

Code Examples

MonoBehaviour

public class ExampleSoundBehaviour : MonoBehaviour
{
    [SerializeField] private SoundData exampleSoundData;

            private void PlaySound()
            {
                    SoundManager.Instance.PlayOneShot(exampleSoundData, transform.position);
            }
    }

Jobs

If you want to create sound in Job, read here.

FMOD

FMOD is used for all sounds in the project (if FMOD installed).

Installation

  1. Download the FMOD plugin from the Asset Store.

  2. Add the FMOD scripting define to the project.

  3. Select the project FMOD path in the window that appears on the Linking tab (by default DotsCity/FMOD/fmodproject/fmodproject.fspro).

    _images/FMOD-setup.png
  4. Sign up & download the FMOD Studio software from the official site (official guide) [optional step, required if you want to add & edit sounds].

    _images/FMODstudio-download.png
  5. Open FMOD settings.

    _images/FMOD-toolbar-settings.png
  6. By default, project path: FMOD project path DotsCity/FMOD/fmodproject/fmodproject.fspro, make sure that the FMOD project path is set correctly.

    _images/FMOD-settings.png

How To Use

  1. Open FMOD Studio installed on your computer.

    _images/FMOD-Studio-mainwindow.png
  2. Open Bank bookmark.

    _images/FMOD-Studio-bankwindow.png
  3. If you do not have an existing bank or need to create a new one, right-click in the window and press New Bank.

  4. In the bookmark Events - Create or open exist folder.

    _images/FMOD-Studio-eventswindow.png
  5. Right-click on the created folder and press Create Event, rename created event.

    _images/FMOD-Studio-NewEventExample.png
  6. Right-click on Add Timeline Sheet in the created event.

    _images/FMOD-Studio-NewTimelineExample.png _images/FMOD-Studio-NewTimelineExample2.png
  7. Drag and drop the desired sound into the timeline.

    _images/FMOD-Studio-DragNDropTimelineExample.png
  8. Customize your sound.

  9. Assign the selected FMOD event to the Bank.

    _images/FMOD-Studio-AssignToBankExample.png
  10. Build FMOD project.

    _images/FMOD-Studio-BuildExample.png
  11. Create Sound Data in the Unity project.

    _images/SoundDataCreation.png
  12. Enter trigger name event:/Vehicle/TestExample.

    _images/SoundDataExample2.png

    Note

    Sound ID is created automatically.

  13. Press Add To Container button to add sound to the Sound container.

    _images/FMOD-SoundServiceAddedExample.png
  14. Now, we can trigger the sound from the code.

Data

Sound Data

Contains data about the sound.

How To Create

Select from the project context menu:

Spirit604/City/Sound/Sound Data

_images/SoundDataCreation.png

Settings

Built-in
_images/SoundDataBuiltin.png
Id : immutable ID, by which sounds are triggered in DOTS traffic city (ID is created automatically).
Loop : on/off sound looping.
Clip volume : volume of the audio clip.
Random sound : on/off random sound.
Audio clip : reference to AudioClip .
FMOD
_images/SoundDataExample.png
Id : immutable ID, by which sounds are triggered in DOTS traffic city (ID is created automatically).
Name : event name of the sound.
Parameters : event parameters .

Sound Data Container

Contains data on all sounds in the Unity project.

_images/FMOD-SoundServiceExample.png

Warning

If you do not add sound to the service, it cannot be activated from the code.

Code Examples

Sound Types

  • Default : default sound entity.

  • One Shot : entity played once & destroyed afterwards.

  • Tracking : entity tracks target entity.

  • Tracking Vehicle : entity tracks target vehicle entity.

  • Tracking And Loop : entity tracks target entity & loop playback.

How To Create

EntityManager methods

SoundExtension.CreateSoundEntity(ref this EntityManager entityManager, int soundId, float volume = 1f)
// Creating a default sound entity.
SoundExtension.CreateTrackedSoundEntity(ref this EntityManager entityManager, int soundId, Entity parentEntity, float volume = 1f)
// Creation of a sound entity that follows a given entity.
SoundExtension.CreateChildSoundEntity(ref this EntityManager entityManager, int soundId, Entity parentEntity, float volume = 1f)
// Creation of a sound entity that will be a child of a given entity.

CommandBuffer methods

Burst compatible methods.

SoundExtension.CreateSoundEntity(ref this EntityCommandBuffer commandBuffer, Entity soundPrefabEntity, int soundId, float volume = 1f)
// Creating a default sound entity.
SoundExtension.CreateSoundEntity(ref this EntityCommandBuffer commandBuffer, Entity soundPrefabEntity, int soundId, float3 position, float volume = 1f)
// Create a sound entity at a specific position.

Create prefab query method

SoundExtension.GetSoundQuery(EntityManager entityManager, SoundType soundType)
// Get `EntityQuery` with the selected `Sound type`.

Create sound example

public partial class ExampleSoundSystem : SystemBase
{
    private EntityQuery soundPrefabQuery;

    protected override void OnCreate()
    {
        soundPrefabQuery = SoundExtension.GetSoundQuery(EntityManager, SoundType.Default);
    }

            protected override void OnUpdate()
            {
                    var commandBuffer = ...
                    var soundPrefabEntity = soundPrefabQuery.GetSingletonEntity();

                    // Pass 'commandBuffer' & 'soundPrefabEntity' into the IJobEntity or Entities.ForEach
                    // commandBuffer.CreateSoundEntity(soundPrefabEntity, soundId, position);
                    // 'soundId' can be taken from 'SoundData'
            }

Params

  • soundId : id of sound taken from sound data.

  • soundPrefabEntity : sound prefab entity taken from EntityQuery.

  • position : initial position of the sound.

  • volume : volume of the sound (0..1 range).

How To Play

public partial class PlayAndStopSoundExampleSystem : SystemBase
{
protected override void OnUpdate()
{

// Get world sounds
var sounds = GetComponentLookup<SoundEventComponent>(false);

Entities
.WithBurst()
.ForEach((
        Entity entity
        in SoundHolder soundHolder) =>
{
        // Some play condition
        bool shouldPlay = true;

        // Some sound entity container component
        Entity soundEntity = soundHolder.Entity

        SoundEventComponent soundEvent = sounds[soundEntity];

        if (shouldPlay)
        {
                soundEvent.SetEvent(SoundEventType.Play);
        }
        else
        {
                soundEvent.SetEvent(SoundEventType.StopFadeout);
        }

        sounds[soundEntity] = soundEvent;

}).Schedule();
}
}

How To Destroy

Enable the PooledEventTag component in the sound entity.

How To Loop

  1. Create a Sound entity.

  2. Add a LoopSoundData component (assign a Duration value).