How to play a specific sound before every dialogue?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Ondatr
Posts: 10
Joined: Wed Dec 07, 2016 7:54 am

How to play a specific sound before every dialogue?

Post by Ondatr »

Before the start of any dialogue I need a particular sound to be played, depending on which conversant starts the dialogue.
For now I wrote AudioWait (mySound) in the Sequence field of Start node for each dialogue. How to code it for all issues at once?
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to play a specific sound before every dialogue?

Post by Tony Li »

Hi,

Here are some suggestions:

Suggestion 1:
1. Add an Audio Source component to each NPC. Assign the sound that you would like to play. Untick Play On Awake and Loop.
2. Add a Dialogue System Events component to the NPC. Set the OnConversationStart event to play the Audio Source.

Suggestion 2:
1. Name the sounds according to the NPC names (e.g., "Adam", "Bob", "Charlie") and put them in a Resources folder.
2. Add this script (which uses script messages) to the Dialogue Manager:

PlayConversantGreeting.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class PlayConversantGreeting : MonoBehaviour {
    void OnConversationLine(Subtitle subtitle) {
        if (subtitle.dialogueEntry.id == 0) { // Add AudioWait(npcName) to START
            subtitle.sequence += "; AudioWait(" + subtitle.listenerInfo.Name + ")";
        }
    }
} 
Ondatr
Posts: 10
Joined: Wed Dec 07, 2016 7:54 am

Re: How to play a specific sound before every dialogue?

Post by Ondatr »

Hi,

Going "suggestion 1" route, the sound plays simultaneously with the start of dialogue, not before.

Didn't try the "suggestion 2" yet, is there a way to control volume and other parameters of an audio clip with AudioWait?
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to play a specific sound before every dialogue?

Post by Tony Li »

Hi,

If you want it to play before the start of the dialogue, suggestion 2 is the way to go. AudioWait() doesn't provide parameters for volume control, but you can set them on the NPC's Audio Source or write a custom sequencer command to do so. They're actually quite simple to write, and the Dialogue System comes with a starter template.
Ondatr
Posts: 10
Joined: Wed Dec 07, 2016 7:54 am

Re: How to play a specific sound before every dialogue?

Post by Ondatr »

Thank you for the great asset and excellent support!
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to play a specific sound before every dialogue?

Post by Tony Li »

Happy to help! :)
Post Reply