none
Visual Novel in C# Using Unity RRS feed

  • Question

  • 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[i]);
    lineEntry.options[i-1] = lineData[i];
    }
    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[i];
    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[i].Split(':')[0]);
    cb.option = options[i].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?

    • Moved by CoolDadTx Tuesday, December 3, 2019 5:21 PM Third party product
    Monday, December 2, 2019 11:48 PM

All replies

  • Hi DapperSpy,

    Thank you for posting here.

    Since this thread is related to Unity, it is a third-party product, we don't provide support for that.

    I suggest you can ask this question in the Unity Forum.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 3, 2019 1:53 AM