Created
May 30, 2015 12:41
-
-
Save kleberandrade/25ff45520d0334449c91 to your computer and use it in GitHub Desktop.
Código para gerenciar dialogos d personagens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
using System.Collections.Generic; | |
public struct DialogMessage | |
{ | |
public string name; | |
public string message; | |
public DialogMessage(string name, string message) | |
{ | |
this.name = name; | |
this.message = message; | |
} | |
} | |
public class Dialog : MonoBehaviour | |
{ | |
public Text nameText; | |
public Text messageText; | |
public float timeToWriteALetter = 0.01f; | |
private string name; | |
private string message; | |
private List<DialogMessage> dialogs = new List<DialogMessage>(); | |
private DialogMessage currentDialog; | |
private bool writing; | |
private bool endMessage; | |
private int indexOfLetter; | |
private int indexOfMessage; | |
public void Show(string name, string message) | |
{ | |
Show(new DialogMessage(name, message)); | |
} | |
public void Show(DialogMessage dialog) | |
{ | |
this.currentDialog = dialog; | |
this.name = currentDialog.name; | |
this.message = string.Empty; | |
this.writing = true; | |
this.indexOfLetter = 0; | |
this.endMessage = false; | |
InvokeRepeating("Write", 0.0f, timeToWriteALetter); | |
} | |
public void Show(List<DialogMessage> dialogs) | |
{ | |
this.dialogs = dialogs; | |
DialogMessage dialog = this.dialogs[0]; | |
dialogs.RemoveAt(0); | |
Show(dialog); | |
} | |
void WriteAll() | |
{ | |
CancelInvoke("Write"); | |
message = currentDialog.message; | |
writing = false; | |
endMessage = true; | |
} | |
void Update() | |
{ | |
if (writing) | |
{ | |
if (Input.GetKeyDown(KeyCode.E)) | |
WriteAll(); | |
nameText.text = name; | |
messageText.text = message; | |
} | |
if (endMessage) | |
{ | |
if (Input.GetKeyDown(KeyCode.E)) | |
{ | |
if (dialogs.Count > 0) | |
Show(dialogs); | |
} | |
} | |
} | |
void Write() | |
{ | |
if (indexOfLetter > currentDialog.message.Length - 1) | |
{ | |
WriteAll(); | |
return; | |
} | |
message += currentDialog.message[indexOfLetter++]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment