Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Need Help Figuring Out Something

Discussion in 'Audio & Video' started by Lakey-12, Apr 16, 2023.

  1. Lakey-12

    Lakey-12

    Joined:
    Apr 16, 2023
    Posts:
    2
    Hello Humans on the Unity Fourms!
    I have a question, so i want to build a game, and i want to have multiple voice options for the player to choose from. but the problem is i want it to play the specific voice at one part of the game, but then if the player wants to change the voice, i dont want it to play the original voice and i want to play the selected voice
    Thank u for ur time!
    Raine/Luna!
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    I think you should elaborate your dialogue system in a way that each line has more than once voice-sound. Then when you have to play the line, you could check which index is referred to by your character settings.

    Does that make sense for your project?
     
  3. Lakey-12

    Lakey-12

    Joined:
    Apr 16, 2023
    Posts:
    2
    so lets say if i were someone named toby, i want it to play the voice lines of toby, and when i change the character to someone like angel, i want it to play the voice lines of angel, not toby, im kinda a beginner on using unity.
    Raine/Luna
     
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    Here's an overview of how you could manage dialogue code. This compiles, but lacks error checking and a bunch of cool features, but it should get you going:
    Code (CSharp):
    1. // A way to identify clearly the voice used
    2. public enum Voice
    3. {
    4.     Invalid, // consider implementing a default voice or warning line
    5.     Alice,
    6.     Bob,
    7.     Carol,
    8.     Dave,
    9.     Eve
    10. }
    11.  
    12. // A way to store all the variations of a line
    13. // depending on who's saying it
    14. public class DialogueLine
    15. {
    16.     public Dictionary<Voice, AudioClip> voices;
    17. }
    18.  
    19. // A way to bundle all the lines of a conversation together
    20. public class Dialogue
    21. {
    22.     public List<DialogueLine> lines;
    23. }
    24.  
    25. // A way to play the line and do a bunch of verifications
    26. [RequireComponent(typeof(AudioSource))]
    27. public class DialoguePlayer : MonoBehaviour
    28. {
    29.     // You should have some kind of placeholder to highlight
    30.     // when a line is missing
    31.     private AudioClip warningAudioClip;
    32.  
    33.     public bool  PlayLine(DialogueLine line, Voice voice)
    34.     {
    35.         if (line != null)
    36.         {
    37.            AudioSource audioSource = GetComponent<AudioSource>();
    38.            if (line.voices.ContainsKey(voice))
    39.            {
    40.                audioSource.PlayOneShot(line.voices[voice]);
    41.                return true;
    42.            }
    43.         }
    44.         audioSource.PlayOneShot(warningAudioClip);
    45.         return false;
    46.     }
    47. }
    48.  
    49. [RequireComponent(typeof(DialoguePlayer))]
    50. public class DialogueExample : MonoBehaviour
    51. {
    52.     // All the lines that could be said by everyone
    53.     public AudioClip
    54.         aliceLine0, bobLine0,
    55.         carolLine1, daveLine1, eveLine1,
    56.         aliceLine2, bobLine2,
    57.         carolLine3, daveLine3, eveLine3;
    58.  
    59.     public void Test()
    60.     {
    61.         // Build a dialogue
    62.         // You should probably store your dialogues as ScriptableObjects
    63.         // to save/edit/manage them better
    64.         Dialogue dialogue = new Dialogue
    65.         {
    66.             lines = new List<DialogueLine>
    67.             {
    68.                 new DialogueLine
    69.                 {
    70.                     voices =
    71.                     {
    72.                         { Voice.Alice, aliceLine0 },
    73.                         { Voice.Bob, bobLine0 },
    74.                     }
    75.                 },
    76.                 new DialogueLine
    77.                 {
    78.                     voices =
    79.                     {
    80.                         { Voice.Carol, carolLine1 },
    81.                         { Voice.Dave, daveLine1 },
    82.                         { Voice.Eve, eveLine1 }
    83.                     }
    84.                 },
    85.                 new DialogueLine
    86.                 {
    87.                     voices =
    88.                     {
    89.                         { Voice.Alice, aliceLine2 },
    90.                         { Voice.Bob, bobLine2 },
    91.                     }
    92.                 },
    93.                 new DialogueLine
    94.                 {
    95.                     voices =
    96.                     {
    97.                         { Voice.Carol, carolLine3 },
    98.                         { Voice.Dave, daveLine3 },
    99.                         { Voice.Eve, eveLine3 }
    100.                     }
    101.                 }
    102.             }
    103.         };
    104.  
    105.         DialoguePlayer player = GetComponent<DialoguePlayer>();
    106.  
    107.         Voice playerVoice = Voice.Alice; // you could load this from your own PlayerSettings
    108.         Voice npcVoice = Voice.Dave;
    109.  
    110.         // Play the dialogue lines with the relevant voices
    111.         player.PlayLine(dialogue.lines[0], playerVoice);
    112.         // ...later
    113.         player.PlayLine(dialogue.lines[1], npcVoice);
    114.         // ...later
    115.         player.PlayLine(dialogue.lines[2], playerVoice);
    116.         // ...later
    117.         player.PlayLine(dialogue.lines[3], npcVoice);
    118.     }
    119. }
     
    Last edited: Apr 17, 2023