eastbayeff

Untitled

Jul 15th, 2022
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class TextDisplayer : MonoBehaviour
  7. {
  8.     [SerializeField]private TextScene currentScene;
  9.    
  10.     [SerializeField]private List<Button> buttons;
  11.  
  12.     private TextNode currentNode;
  13.     private Text content;
  14.  
  15.     private void Start()
  16.     {
  17.         content = GameObject.Find("TextNodeContent").GetComponent<Text>();
  18.         SetCurrentNode(currentScene.startingNode);
  19.     }
  20.  
  21.     // make this public so we can assign it as a listener for the buttons
  22.     // allow it to take a node so we know how to move thru buttons
  23.     public void SetCurrentNode(TextNode node)
  24.     {
  25.         Debug.Log("setting node");
  26.         currentNode = currentScene.startingNode;
  27.         RunText();
  28.     }
  29.  
  30.     public void RunText()
  31.     {
  32.         //write text on screen
  33.         content.text = currentNode.content;
  34.         RunBranches();
  35.     }
  36.  
  37.     public void RunBranches()
  38.     {
  39.         // if there are branches for this node, let's make buttons
  40.         if (currentNode.branches.Count > 0)
  41.         {
  42.             // first turn off all the buttons
  43.             foreach (var button in buttons)
  44.                 button.gameObject.SetActive(false);
  45.            
  46.             // then, enable and populate each button based on the branch data
  47.             var buttonIndex = 0;
  48.             foreach (var branch in currentNode.branches)
  49.             {
  50.                 buttons[buttonIndex].gameObject.SetActive(true);
  51.                 buttons[buttonIndex].GetComponentInChildren<Text>().text = branch.buttonContent;
  52.                 buttons[buttonIndex].onClick.AddListener(() => SetCurrentNode(branch.node));
  53.                 buttonIndex++;
  54.             }
  55.         }
  56.     }
  57.  
  58.  
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment