I'm a student and I'm creating a visual novel in unity for our thesis. Mostly the code is from them but I studied it and make some revisions to make it mine.
My problem is that everything is working in unity editor, the dialogues are changing and I can choose a decision by clicking a button, but when I try to build it there's nothing except the background.
This is what it looks like in Unity Editor.
After the build and I run it in windows, The start() function is working, but when I start to press any key, it won't update as you can see below:
This what it looks like after I built and run the game.
This is the code in my start() and update() function:
PS: The Input.TouchCount there is to make the game runnable in android too but I want the game run in windows first. Thank you for you help!
DialogueManager.cs:
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;
public int background;
string position;
string[] options;
public bool playerTalking;
List<Button> buttons = new List<Button>();
public Text dialogueBox;
public Text nameBox;
public GameObject choiceBox;
void Start () {
dialogue = "NO! N-NO! PLEASE NO!";
characterName = "MC";
pose = 0;
background = 0;
position = "MC";
playerTalking = false;
parser = GameObject.Find("DialogueParser").GetComponent<DialogueParser>();
lineNum = 1;
}
void Update () {
if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Began || Input.anyKeyDown && playerTalking == false)
{
ShowDialogue();
lineNum++;
}
UpdateUI();
}
public void ShowDialogue()
{
ResetImages();
ParseLine();
}
void ResetImages()
{
if (characterName != "")
{
GameObject character = GameObject.Find(characterName);
SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
currSprite.sprite = null;
}
}
void ParseLine()
{
if (parser.GetName(lineNum) != "Player")
{
playerTalking = false;
characterName = parser.GetName(lineNum);
dialogue = parser.GetContent(lineNum);
pose = parser.GetPose(lineNum);
background = parser.GetBackground(lineNum);
position = parser.GetPosition(lineNum);
DisplayImages();
DisplayBackground();
}
else
{
playerTalking = true;
characterName = "";
dialogue = "";
pose = 0;
background = 1;
position = "";
options = parser.GetOptions(lineNum);
CreateButtons();
}
}
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(-2, 0);
}
else if (position == "R")
{
spriteObj.transform.position = new Vector3(2, 0);
}
else if (position == "Wolf"){
spriteObj.transform.position = new Vector3(1, 0);
spriteObj.transform.localScale = new Vector3(10, 10);
}
else if (position == "C")
{
spriteObj.transform.position = new Vector3(0, 0);
}
else if (position == "MC")
{
spriteObj.transform.position = new Vector3(-5, -3);
}
spriteObj.transform.position = new Vector3(spriteObj.transform.position.x, spriteObj.transform.position.y, 0);
}
void DisplayBackground()
{
GameObject bgObject = GameObject.Find("Background");
SetBackgroundScale(bgObject);
bgObject.transform.position = new Vector3(0, 0);
SpriteRenderer currSprite = bgObject.GetComponent<SpriteRenderer>();
currSprite.sprite = bgObject.GetComponent<Character>().characterPoses[background];
}
void SetBackgroundScale(GameObject bgObject)
{
if(background == 0)
{
bgObject.transform.localScale = new Vector3(17, 18);
}
else if(background == 1)
{
bgObject.transform.localScale = new Vector3(10, 10, -10);
}
else if(background == 2)
{
bgObject.transform.localScale = new Vector3(20, 25);
}
else if (background == 3)
{
bgObject.transform.localScale = new Vector3(10, 18);
}
else if (background == 4)
{
bgObject.transform.localScale = new Vector3(10, 14);
}
else if (background == 5)
{
bgObject.transform.localScale = new Vector3(15, 18);
}
}
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[i].Split(':')[0]);
cb.option = options[i].Split(':')[1];
cb.box = this;
b.transform.SetParent(this.transform);
b.transform.localPosition = new Vector3(0, -25 + (i * 50));
b.transform.localScale = new Vector3(1, 1, 1);
if (button.GetComponentInChildren<Text>().text == "")
{
b.image.color = new Color(0,0,0,0);
b.transform.localPosition = new Vector3(0, -25 + (i * 50));
b.transform.localScale = new Vector3(20, 20);
}
buttons.Add(b);
}
}
void UpdateUI()
{
if (!playerTalking)
{
ClearButtons();
}
dialogueBox.text = dialogue;
if (characterName == "Wolf")
{
nameBox.text = "";
}
else nameBox.text = characterName;
}
void ClearButtons()
{
for (int i = 0; i < buttons.Count; i++)
{
print("Clearing buttons");
Button b = buttons[i];
buttons.Remove(b);
Destroy(b.gameObject);
}
}
}
DialogueParser.cs:
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 {
List<DialogueLine> lines;
struct DialogueLine
{
public string name;
public string content;
public int pose;
public int background;
public string position;
public string[] options;
public DialogueLine(string Name, string Content, int Pose,int bg, string Position)
{
name = Name;
content = Content;
pose = Pose;
background = bg;
position = Position;
options = new string[0];
}
}
void Start () {
string file = "Assets/Resources/Dialogue/Dialogue";
string sceneNum = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
file += sceneNum;
file += ".txt";
lines = new List<DialogueLine>();
LoadDialogue(file);
}
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,0, "");
lineEntry.options = new string[lineData.Length - 1];
for (int i = 1; i < lineData.Length; i++)
{
lineEntry.options[i - 1] = lineData[i];
}
lines.Add(lineEntry);
}
else
{
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), int.Parse(lineData[3]), lineData[4]);
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 int GetBackground(int lineNumber)
{
if (lineNumber < lines.Count)
{
return lines[lineNumber].background;
}
return 0;
}
public string[] GetOptions(int lineNumber)
{
if (lineNumber < lines.Count)
{
return lines[lineNumber].options;
}
return new string[0];
}
}
PS: DialogueManager.cs is to manage the dialogue, the character images, the characters' name, and showing them in the scene. While the DialogueParser.cs is parsing the line (for example: "MC|"SOMEONE…! HELP! HELP ME!!!"|0|0|R"). The first part is the name, the second is the dialogue, the two numbers are character image and background respectively, and the fifth and last part is the position of a character image. These five parts are separated by "|". In other words, the DialogueParser.cs work is to separate them and give them to DialogueManager.cs.