Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class TextDisplayer : MonoBehaviour
- {
- [SerializeField]private TextScene currentScene;
- [SerializeField]private List<Button> buttons;
- private TextNode currentNode;
- private Text content;
- private void Start()
- {
- content = GameObject.Find("TextNodeContent").GetComponent<Text>();
- SetCurrentNode(currentScene.startingNode);
- }
- // make this public so we can assign it as a listener for the buttons
- // allow it to take a node so we know how to move thru buttons
- public void SetCurrentNode(TextNode node)
- {
- Debug.Log("setting node");
- currentNode = currentScene.startingNode;
- RunText();
- }
- public void RunText()
- {
- //write text on screen
- content.text = currentNode.content;
- RunBranches();
- }
- public void RunBranches()
- {
- // if there are branches for this node, let's make buttons
- if (currentNode.branches.Count > 0)
- {
- // first turn off all the buttons
- foreach (var button in buttons)
- button.gameObject.SetActive(false);
- // then, enable and populate each button based on the branch data
- var buttonIndex = 0;
- foreach (var branch in currentNode.branches)
- {
- buttons[buttonIndex].gameObject.SetActive(true);
- buttons[buttonIndex].GetComponentInChildren<Text>().text = branch.buttonContent;
- buttons[buttonIndex].onClick.AddListener(() => SetCurrentNode(branch.node));
- buttonIndex++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment