How to modify typewriter sound per character effect in-game?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
sleepyBear
Posts: 15
Joined: Wed Nov 07, 2018 10:27 pm

How to modify typewriter sound per character effect in-game?

Post by sleepyBear »

Good day,

I'm currently using the textMeshPro typewriter effect. The audio per character is working well. What I want to do is for example I have two NPCs: One male, one female... The typewritter effect will play the male dialogue 8-bit sound effect audio if conversing with the male NPC and on the other hand it will play the female dialogue 8-bit sound effect when speaking to the female NPC. I can't seem to figure out how to do it.

Thanks,
forum pics.png
forum pics.png (19.24 KiB) Viewed 1568 times
sleepyBear
Posts: 15
Joined: Wed Nov 07, 2018 10:27 pm

Re: How to modify typewriter sound per character effect in-game?

Post by sleepyBear »

Also is there a way I can modify it here in the conversation tab?
forum pic 2.png
forum pic 2.png (140.85 KiB) Viewed 1565 times
It would be really useful especially if there are three or more people in one conversation
User avatar
Tony Li
Posts: 20642
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to modify typewriter sound per character effect in-game?

Post by Tony Li »

Hi,

There are two ways you can do this.

1. Make a copy of the subtitle panel for each character, and assign them to the Standard Dialogue UI's list of subtitle panels. Add a Dialogue Actor component to each character, and select the subtitle panel number to use (e.g., 0 for male, 2 for female, etc.). Then customize that subtitle panel's typewriter effect.

2. Or, if you want to use one subtitle panel, assign a C# method to the typewriter effect's OnBegin() method. In this method, set the typewriter effect's audio clip. If you put the audio clips in a Resources folder, you can add a custom field to each actor that specifies the audio clip name. In your C# method, look up that field, load the corresponding audio clip, and assign it. Something like:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SetTypewriterForActor : MonoBehaviour
{
    public void OnBeginTypewriter() // Assign to typewriter's OnBegin() event.
    {
        // Look up character's audio clip:
        var actorName = DialogueManager.currentConversationState.subtitle.speakerInfo.nameInDatabase;
        var clipName = DialogueManager.masterDatabase.GetActor(actorName).LookupValue("AudioClip");
        var clip = Resources.Load<AudioClip>(clipName);

        // Assign to typewriter:
        var typewriter = GetComponent<AbstractTypewriterEffect>();
        typewriter.audioClip = clip;
        typewriter.audioSource.clip = clip;
    }
}
This is just an example to demonstrate the idea. It might have typos; I typed it directly here. Your code may be different.

(EDIT: Script updated.)
sleepyBear
Posts: 15
Joined: Wed Nov 07, 2018 10:27 pm

Re: How to modify typewriter sound per character effect in-game?

Post by sleepyBear »

I did method #2 and it works like a charm! :) Thank you very much!
User avatar
Tony Li
Posts: 20642
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to modify typewriter sound per character effect in-game?

Post by Tony Li »

Glad to help!
User avatar
Tony Li
Posts: 20642
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to modify typewriter sound per character effect in-game?

Post by Tony Li »

Another user asked for a little more clarification on method #2. I figured I'd add an example scene as well:

ModifyTypewriterExample_2019-09-11.unitypackage

(Exported from Unity 2019.1.)

Let's say you have an audio clip for each actor. Put them in a folder named Resources:

Image

The Resources folder can be anywhere in your project, and you can have as many Resources folders in your project as you want.

Inspect each actor in the Dialogue Editor. Add a custom field named AudioClip, and set its value to the name of the audio clip:

Image

The field name "AudioClip" is arbitrary; it's just what I chose to use in the script.

Add the script above (named SetTypewriterForActor.cs) to the subtitle text GameObject. Also add an Audio Source so you can configure it exactly the way you want it to sound (e.g., volume, etc.). Assign them both to the typewriter effect as shown below:

https://www.pixelcrushers.com/dialogue_ ... riter3.png

When the typewriter effect begins, it will run SetTypewriterForActor's OnBeginTypewriter method. This method looks up the current speaker's AudioClip field and assigns the corresponding audio clip to the typewriter.
Post Reply