Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Last active November 2, 2015 06:41
Show Gist options
  • Select an option

  • Save grimmdev/4b031d7585fc0043d80a to your computer and use it in GitHub Desktop.

Select an option

Save grimmdev/4b031d7585fc0043d80a to your computer and use it in GitHub Desktop.
Simple Dialogues and Conversations in Unity. πŸ‘₯ πŸ’¬
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class Conversation
{
// The ID of the current Conversation.
public string ID = string.Empty;
// The Name of the person talking.
public string Name = string.Empty;
// The actual text the person says!
public string ConversationText = string.Empty;
// Do we want to respond back?
public List<Response> Responses = new List<Response>();
// Fallback ID for next Conversation when there is no Responses.
public string DefaultNextID = string.Empty;
}
// πŸ‘₯ πŸ’¬
// Simple example for conversations/dialogue.
// Sean Loper
// I hope you can learn from this and make something special of your own!
// Just attach the conversation manager to any gameobject in your scene and start adding conversations and responses!
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class ConversationManager : MonoBehaviour
{
// This holds all our conversations and responses.
[SerializeField]
private List<Conversation> Conversations = new List<Conversation>();
// This variable is just for storing the current chosen or started conversation.
private Conversation Current;
// When the scene loads.
private void Awake ()
{
// Did we add conversations like we where suppose to?
if (Conversations.Count > 0) {
// Then pick the very first in the Conversation List.
Current = Conversations[0];
} else {
// or.. complain about it.
Debug.Log ("Please add some Conversations.");
}
}
// This function is a must have, it sorts through the list given a matching string or in this case ConversationID.
private Conversation GetConversationByID(string conversationID)
{
return Conversations.FirstOrDefault(conversation => conversation.ID == conversationID);
}
// We use old unity UI, only for demonstration purposes.
private void OnGUI()
{
// Check to see if our current conversation isn't empty for good measure.
if (Current != null) {
// Display the conversation name holder.
GUILayout.Label(Current.Name);
// Display the conversation text
GUILayout.Label(Current.ConversationText);
// If you added responses for this conversation
if(Current.Responses.Count > 0) {
// go through each response stored.
foreach(Response r in Current.Responses) {
// now display the response text and then change the conversation on click.
if(GUILayout.Button(r.ResponseText)){
Current = GetConversationByID(r.ConversationID);
}
}
} else {
// Here we continue with the conversation if no responses.
if(GUILayout.Button("Continue")){
Current = GetConversationByID(Current.DefaultNextID);
}
}
}
}
}
using UnityEngine;
using System;
using System.Collections;
[Serializable]
public class Response
{
// Text we want to respond back with.
public string ResponseText;
// Next conversation when you respond.
public string ConversationID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment