我是一名学生,我正在为我们的论文统一创作一部视觉小说。大部分代码来自them,但我对其进行了研究并进行了一些修改以使其成为我的
。我的问题是,一切都在统一编辑器中进行,对话正在改变,我可以通过单击按钮来选择一个决定,但是当我尝试构建它时,除了背景之外什么都没有。
This is what it looks like in Unity Editor.
构建并在Windows中运行它后,start()函数开始运行,但是当我开始按任意键时,它将不会更新,如下所示:
This what it looks like after I built and run the game.
这是我的start()和update()函数中的代码:
PS:Input.TouchCount也可以使游戏在android中运行,但是我希望游戏首先在Windows中运行。谢谢您的帮助!
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用于管理对话,角色图像,角色名称,并在场景中显示它们。当DialogueParser.cs解析该行时(例如:“ MC |” SOMEONE ...!HELP!HELP ME !!!“ || 0 | 0 | R”)。第一部分是名称,第二部分是对话,两个数字分别是字符图像和背景,第五和最后一部分是字符图像的位置。这五个部分之间用“ |”分隔。换句话说,DialogueParser.cs的工作是将它们分开并提供给DialogueManager.cs。
答案 0 :(得分:1)
问题可能出在您的LoadDialogue函数中。在Unity中,当您进行构建时,资源文件夹已打包,因此“资产/资源/对话框/对话框#.txt”将不存在。
您可以使用Resources.Load读取文件。请注意,使用Resources.Load,您不需要文件扩展名。
TextAsset dialogueAsset = Resources.Load<TextAsset>("Dialogue/Dialogue#");
string dialogue = dialogueAsset.text; // This is your whole dialogue file.