D
DapperSpy
Guest
Hello! I'm new to C# and game development and need help. I'm trying to program a Visual Novel in C# using Unity. I have one issue when I load my game though. The dialogue does not show up. My Scripts are shown below...
DialogueParser:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class DialogueParser : MonoBehaviour {
struct DialogueLine { // The data of each line of dialogue
public string name;
public string content;
public int pose;
public string position;
public string[] options;
public DialogueLine(string Name, string Content, int Pose, string Position) {
name = Name;
content = Content;
pose = Pose;
position = Position;
options = new string[0];
}
}
List<DialogueLine> lines; // Storing the lines in a specific order
// Use this for initialization
void Start () {
string file = "Assets/Data/Dialogue";
string sceneNum = EditorApplication.currentScene; // Gets the current Scene saved name aka Scene1
// Regex is a way to manipulate strings
sceneNum = Regex.Replace (sceneNum, "[^0-9]", ""); //Replace everything except numbers in the string with ""
file += sceneNum;
file += ".txt";
lines = new List<DialogueLine>();
LoadDialogue (file);
}
// Update is called once per frame
void Update () {
}
void LoadDialogue(string filename) {
string line;
StreamReader r = new StreamReader (filename);
using (r) {
do {
line = r.ReadLine();
if (line != null) {
string[] lineData = line.Split(';');
if (lineData[0] == "Player") {
DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, "");
lineEntry.options = new string[lineData.Length-1];
for (int i = 1; i < lineData.Length; i++) {
Debug.Log(lineData);
lineEntry.options[i-1] = lineData;
}
lines.Add(lineEntry);
} else {
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), lineData[3]);
lines.Add(lineEntry);
}
}
}
while (line != null);
r.Close();
}
}
public string GetPosition(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].position;
}
return "";
}
public string GetName(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].name;
}
return "";
}
public string GetContent(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].content;
}
return "";
}
public int GetPose(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].pose;
}
return 0;
}
public string[] GetOptions(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].options;
}
return new string[0];
}
}
DialogueManager:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class DialogueManager : MonoBehaviour {
DialogueParser parser;
public string dialogue, characterName;
public int lineNum;
int pose;
string position;
string[] options;
public bool playerTalking;
List<Button> buttons = new List<Button> ();
public Text dialogueBox;
public Text nameBox;
public GameObject choiceBox;
// Use this for initialization
void Start () {
dialogue = "";
characterName = "";
pose = 0;
position = "L";
playerTalking = false;
parser = GameObject.Find("DialogueParser").GetComponent<DialogueParser>();
lineNum = 0;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) && playerTalking == false) {
ShowDialogue();
lineNum++;
}
UpdateUI ();
}
public void ShowDialogue() {
ResetImages ();
ParseLine ();
}
void UpdateUI() {
if (!playerTalking) {
ClearButtons();
}
dialogueBox.text = dialogue;
nameBox.text = characterName;
}
void ClearButtons() {
for (int i = 0; i < buttons.Count; i++) {
print ("Clearing buttons");
Button b = buttons;
buttons.Remove(b);
Destroy(b.gameObject);
}
}
void ParseLine() {
if (parser.GetName (lineNum) != "Player") {
Debug.Log("Parsing line of " + parser.GetName(lineNum));
playerTalking = false;
characterName = parser.GetName (lineNum);
dialogue = parser.GetContent (lineNum);
pose = parser.GetPose (lineNum);
position = parser.GetPosition (lineNum);
DisplayImages();
} else {
Debug.Log("Parsing player line.");
playerTalking = true;
characterName = "";
dialogue = "";
pose = 0;
position = "";
options = parser.GetOptions(lineNum);
CreateButtons();
}
}
void CreateButtons() {
for (int i = 0; i < options.Length; i++) {
GameObject button = (GameObject)Instantiate(choiceBox);
Button b = button.GetComponent<Button>();
ChoiceButton cb = button.GetComponent<ChoiceButton>();
cb.SetText(options.Split(':')[0]);
cb.option = options.Split(':')[1];
cb.box = this;
b.transform.parent = this.transform;
b.transform.localPosition = new Vector3(0,-25 + (i*50));
b.transform.localScale = new Vector3(1, 1, 1);
buttons.Add (b);
}
}
void ResetImages() {
if (characterName != "") {
GameObject character = GameObject.Find (characterName);
SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
currSprite.sprite = null;
}
}
void DisplayImages() {
if (characterName != "") {
GameObject character = GameObject.Find(characterName);
SetSpritePositions(character);
SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
currSprite.sprite = character.GetComponent<Character>().characterPoses[pose];
}
}
void SetSpritePositions(GameObject spriteObj) {
if (position == "L") {
spriteObj.transform.position = new Vector3 (-6, 0);
} else if (position == "R") {
spriteObj.transform.position = new Vector3 (6, 0);
}
spriteObj.transform.position = new Vector3 (spriteObj.transform.position.x, spriteObj.transform.position.y, 0);
}
}
Can someone please try and help me out?
Continue reading...
DialogueParser:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class DialogueParser : MonoBehaviour {
struct DialogueLine { // The data of each line of dialogue
public string name;
public string content;
public int pose;
public string position;
public string[] options;
public DialogueLine(string Name, string Content, int Pose, string Position) {
name = Name;
content = Content;
pose = Pose;
position = Position;
options = new string[0];
}
}
List<DialogueLine> lines; // Storing the lines in a specific order
// Use this for initialization
void Start () {
string file = "Assets/Data/Dialogue";
string sceneNum = EditorApplication.currentScene; // Gets the current Scene saved name aka Scene1
// Regex is a way to manipulate strings
sceneNum = Regex.Replace (sceneNum, "[^0-9]", ""); //Replace everything except numbers in the string with ""
file += sceneNum;
file += ".txt";
lines = new List<DialogueLine>();
LoadDialogue (file);
}
// Update is called once per frame
void Update () {
}
void LoadDialogue(string filename) {
string line;
StreamReader r = new StreamReader (filename);
using (r) {
do {
line = r.ReadLine();
if (line != null) {
string[] lineData = line.Split(';');
if (lineData[0] == "Player") {
DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, "");
lineEntry.options = new string[lineData.Length-1];
for (int i = 1; i < lineData.Length; i++) {
Debug.Log(lineData);
lineEntry.options[i-1] = lineData;
}
lines.Add(lineEntry);
} else {
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), lineData[3]);
lines.Add(lineEntry);
}
}
}
while (line != null);
r.Close();
}
}
public string GetPosition(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].position;
}
return "";
}
public string GetName(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].name;
}
return "";
}
public string GetContent(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].content;
}
return "";
}
public int GetPose(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].pose;
}
return 0;
}
public string[] GetOptions(int lineNumber) {
if (lineNumber < lines.Count) {
return lines[lineNumber].options;
}
return new string[0];
}
}
DialogueManager:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class DialogueManager : MonoBehaviour {
DialogueParser parser;
public string dialogue, characterName;
public int lineNum;
int pose;
string position;
string[] options;
public bool playerTalking;
List<Button> buttons = new List<Button> ();
public Text dialogueBox;
public Text nameBox;
public GameObject choiceBox;
// Use this for initialization
void Start () {
dialogue = "";
characterName = "";
pose = 0;
position = "L";
playerTalking = false;
parser = GameObject.Find("DialogueParser").GetComponent<DialogueParser>();
lineNum = 0;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) && playerTalking == false) {
ShowDialogue();
lineNum++;
}
UpdateUI ();
}
public void ShowDialogue() {
ResetImages ();
ParseLine ();
}
void UpdateUI() {
if (!playerTalking) {
ClearButtons();
}
dialogueBox.text = dialogue;
nameBox.text = characterName;
}
void ClearButtons() {
for (int i = 0; i < buttons.Count; i++) {
print ("Clearing buttons");
Button b = buttons;
buttons.Remove(b);
Destroy(b.gameObject);
}
}
void ParseLine() {
if (parser.GetName (lineNum) != "Player") {
Debug.Log("Parsing line of " + parser.GetName(lineNum));
playerTalking = false;
characterName = parser.GetName (lineNum);
dialogue = parser.GetContent (lineNum);
pose = parser.GetPose (lineNum);
position = parser.GetPosition (lineNum);
DisplayImages();
} else {
Debug.Log("Parsing player line.");
playerTalking = true;
characterName = "";
dialogue = "";
pose = 0;
position = "";
options = parser.GetOptions(lineNum);
CreateButtons();
}
}
void CreateButtons() {
for (int i = 0; i < options.Length; i++) {
GameObject button = (GameObject)Instantiate(choiceBox);
Button b = button.GetComponent<Button>();
ChoiceButton cb = button.GetComponent<ChoiceButton>();
cb.SetText(options.Split(':')[0]);
cb.option = options.Split(':')[1];
cb.box = this;
b.transform.parent = this.transform;
b.transform.localPosition = new Vector3(0,-25 + (i*50));
b.transform.localScale = new Vector3(1, 1, 1);
buttons.Add (b);
}
}
void ResetImages() {
if (characterName != "") {
GameObject character = GameObject.Find (characterName);
SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
currSprite.sprite = null;
}
}
void DisplayImages() {
if (characterName != "") {
GameObject character = GameObject.Find(characterName);
SetSpritePositions(character);
SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
currSprite.sprite = character.GetComponent<Character>().characterPoses[pose];
}
}
void SetSpritePositions(GameObject spriteObj) {
if (position == "L") {
spriteObj.transform.position = new Vector3 (-6, 0);
} else if (position == "R") {
spriteObj.transform.position = new Vector3 (6, 0);
}
spriteObj.transform.position = new Vector3 (spriteObj.transform.position.x, spriteObj.transform.position.y, 0);
}
}
Can someone please try and help me out?
Continue reading...