• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by erradical64 · Jul 18, 2022 at 09:55 AM · dialogue

How do I make a flexible dialogue system?

I'm thinking about expanding my dialogue systems to include things such as options and conditions, such as toggling between yes or no options that trigger specific dialogue and triggering an animation. I also want the dialogue systems to remember whether the player has previously communicated the dialogue triggering object.

Here are my scripts of code:

Dialogue System

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

namespace DialogueSystem { public class DialogueLine : DialogueBaseClass { private Text textHolder;

 [Header ("Text Options")]
 [SerializeField] private string input;
 [SerializeField] private Color textColor;
 [SerializeField] private Font textFont;

 [Header ("Time parameters")]
 [SerializeField] private float delay;
 [SerializeField] private float delayBetweenLines;
 
 [Header ("Sound")]
 [SerializeField] private AudioClip sound;

 [Header ("Character Icon")]
 [SerializeField] private Sprite characterSprite;
 [SerializeField] private Image holdImage;
 
 private IEnumerator lineAppear;    
 
 private void Awake()
 {

     holdImage.sprite = characterSprite;
     holdImage.preserveAspect = true;
 }
 private void OnEnable()
 {
     ResetLine();
     lineAppear = WriteText(input, textHolder, textColor, textFont, delay, sound, delayBetweenLines);
     StartCoroutine(lineAppear);
 }

 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.C))
     {
     if (textHolder.text != input)
     {
         StopCoroutine(lineAppear);
         textHolder.text = input;
     }
     else
         finished = true;
     }
 }
 private void ResetLine()
 {
     textHolder = GetComponent<Text>();
     textHolder.text = "";
     finished = false;
 }
 }

}

Sound Manager

using UnityEngine;

public class SoundManager : MonoBehaviour { public static SoundManager instance { get; private set; }

 private AudioSource source;

 private void Awake()
 {
     instance = this;

 source = GetComponent<AudioSource>();
 }
 public void PlaySound(AudioClip sound)
 {
 source.PlayOneShot(sound);
 }

}

NPC Controller

using UnityEngine;

public class NPC_Controller : MonoBehaviour { [SerializeField] private GameObject dialogue;

 public void ActivateDialogue()
 {
 dialogue.SetActive(true);
 }

 public bool DialogueActive()
 {
 return dialogue.activeInHierarchy;
 }

}

Dialogue Holder

using System.Collections; using UnityEngine;

namespace DialogueSystem {

 public class DialogueHolder : MonoBehaviour
 {
 private IEnumerator dialogueSeq;
 private bool dialogueFinished;
 public int Afterwards;
 
 private void OnEnable()
 {
     dialogueSeq = dialogueSequence();
     StartCoroutine(dialogueSeq);
 }

 private void Update()
 {
     if (Input.GetKey(KeyCode.Escape))
     {
     Deactivate();
     gameObject.SetActive(false);
     StopCoroutine(dialogueSeq);
     }
 }

 private IEnumerator dialogueSequence()
 {
     if (!dialogueFinished)
     {
     //for (int i = 0; i < transform.childCount - 1; i++)
     for (int i = 0; i < transform.childCount - Afterwards; i++)
     {
          Deactivate();
          transform.GetChild(i).gameObject.SetActive(true);
          yield return new WaitUntil(() => transform.GetChild(i).GetComponent<DialogueLine>().finished);
     }
     }

     else
     {
     //int index = transform.childCount - 1;
     int index = transform.childCount - Afterwards;
     Deactivate();
     transform.GetChild(index).gameObject.SetActive(true);
     yield return new WaitUntil(() => transform.GetChild(index).GetComponent<DialogueLine>().finished);
     
     }
     dialogueFinished = true;
     gameObject.SetActive(false);
 }

 private void Deactivate()
     {
     for (int i = 0; i < transform.childCount; i++)
     {
     transform.GetChild(i).gameObject.SetActive(false);
     }
     }
 }

}

Dialogue Base Class

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

namespace DialogueSystem {

 public class DialogueBaseClass : MonoBehaviour
 {
 
 public bool finished { get; protected set;}

 protected IEnumerator WriteText(string input, Text textHolder, Color textColor, Font textFont, float delay, AudioClip sound, float delayBetweenLines)
 {
     textHolder.color = textColor;
     textHolder.font = textFont;

     for (int i = 0; i < input.Length; i++)
     {
     textHolder.text += input[i];

     SoundManager.instance.PlaySound(sound);

     yield return new WaitForSeconds(delay);
     }

     //yield return new WaitForSeconds(delayBetweenLines);
     yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
     finished = true;
 }
 }

}

Player

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : MonoBehaviour { [SerializeField] private float playerSpeed; private Rigidbody2D body; private NPC_Controller npc;

 public Animator animator;
 public PlayerState currentState;
   

 Vector2 movement;


 private void Awake()
 {
     body = GetComponent<Rigidbody2D>();
 
 }

 private void Update()
 {
 if (!inDialogue())
 {
     movement = Vector2.zero;
     movement.x = Input.GetAxisRaw("Horizontal");
     movement.y = Input.GetAxisRaw("Vertical");

     UpdateAnimationAndMove();
     
     
 }
 }
 private void FixedUpdate()
 {
 body.MovePosition(body.position + movement * playerSpeed * Time.fixedDeltaTime);
 }

 private bool inDialogue()
 {
     if(npc != null)
    return npc.DialogueActive();
 else
    return false;

 }

 private void OnTriggerStay2D(Collider2D collision)
 {
 if (collision.gameObject.tag == "NPC")
 {
     npc = collision.gameObject.GetComponent<NPC_Controller>();

     if (Input.GetKey(KeyCode.Space))
     npc.ActivateDialogue();
 }
 }

 private void OnTriggerExit2D(Collider2D collision)
 {
 npc = null;
 }

 private void UpdateAnimationAndMove()
 {
 if (movement != Vector2.zero)
 {
    FixedUpdate();
    animator.SetFloat("Horizontal", movement.x);
    animator.SetFloat("Vertical", movement.y);
    animator.SetBool("IsMoving", true);
 }
 else
 {

     animator.SetBool("IsMoving", false);
 }
 }


}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make dialogue in Unity3D? 2 Answers

Looking for Dialogue tutorials that teaches developers to read XML files to the game. 1 Answer

How to display a characters "dialogue" when entering a collider? 1 Answer

Dialogue related. Can't get this code to work. Any suggestions ? 0 Answers

Dialogue system with index, children, and parent. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges