Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class DialogueSystem : MonoBehaviour {
  7.     // Allows us to get to DialogueSystem from other classes via DialogueSystem.Instance
  8.     public static DialogueSystem Instance { get; set; } //attribute accessors
  9.  
  10.     // a list for our lines to be set via inspector
  11.     public List<string> dialogueLines = new List<string>(); //lists are like arrays but "better", in c# you can't dynamicly size arrays
  12.  
  13.     // allows us to drag Dialog Panel into this object via inspector
  14.     public GameObject dialoguePanel;
  15.     public string     npcName; //will probably be set to hidden soon
  16.  
  17.     Button continueButton;         //make a button object
  18.     Text   dialogueText, nameText; //make two text objects
  19.     int    dialogueIndex;          //I belive this is for our dialogue array
  20.  
  21.     // When activated via NPC
  22.     void Awake () {
  23.         //get continue button component through dialogue panel's child 'Continue', same with text and name
  24.         continueButton = dialoguePanel.transform.Find ("Continue").GetComponent<Button> ();
  25.         dialogueText   = dialoguePanel.transform.Find("Text").GetComponent<Text> ();
  26.         nameText       = dialoguePanel.transform.Find("Name").GetChild(0).GetComponent<Text>(); // 0 means get the first child (indexed by 0)
  27.  
  28.         //what happens when the button is clicked
  29.         // Delegates are used to pass methods as arguments to other methods.
  30.         continueButton.onClick.AddListener (delegate{ ContinueDialogue(); } );
  31.         dialoguePanel.SetActive (false);
  32.  
  33.  
  34.         // if Instance is not null and isn't this instance(?)
  35.         if (Instance != null && Instance != this) {
  36.             Destroy (gameObject); //Destroy self
  37.         }
  38.         else
  39.         {
  40.             Instance = this;
  41.         }
  42.     }
  43.  
  44.     // To be called by NPC, who will pass in their lines and name
  45.     public void AddNewDialogue(string[] lines, string npcName)
  46.     {
  47.         dialogueIndex = 0;
  48.         dialogueLines = new List<string> (lines.Length); //set the list to lines.length
  49.         dialogueLines.AddRange (lines); //appends lines to the the list
  50.         this.npcName = npcName; //set this instances name to the name passed
  51.         CreateDialogue();
  52.         Debug.Log(dialogueLines.Count);
  53.     }
  54.  
  55.     public void CreateDialogue()
  56.     {
  57.         dialogueText.text = dialogueLines [dialogueIndex];
  58.         nameText.text = npcName;
  59.         dialoguePanel.SetActive (true);
  60.     }
  61.  
  62.     public void ContinueDialogue()
  63.     {
  64.         if (dialogueIndex < dialogueLines.Count - 1)
  65.         {
  66.             dialogueIndex++;
  67.             dialogueText.text = dialogueLines [dialogueIndex];
  68.         }
  69.         else
  70.         {
  71.             dialoguePanel.SetActive (false);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement