Posted May 09, 2021 by misterG-gamedev
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Ink.Runtime;
public class InkTestingScript : MonoBehaviour
{
public TextAsset inkJSON;
private Story story;
public Text textPrefab;
public Button buttonPrefab;
private void Start()
{
story = new Story(inkJSON.text);
RefreshUI();
}
public void RefreshUI()
{
EraseUI();
Text storyText = Instantiate(textPrefab) as Text;
storyText.text = LoadStoryChunk();
storyText.transform.SetParent(this.transform, false);
foreach (Choice choice in story.currentChoices)
{
Button choiceButton = Instantiate(buttonPrefab) as Button;
choiceButton.transform.SetParent(this.transform, false);
//Text choiceText = buttonPrefab.GetComponentInChildren<Text>();
Text choiceText = choiceButton.GetComponentInChildren<Text>();
choiceText.text = choice.text;
choiceButton.onClick.AddListener(delegate
{
ChooseStoryChoice(choice);
}
);
}
}
public void EraseUI()
{
for (int i = 0; i < this.transform.childCount; i++)
{
Destroy(this.transform.GetChild(i).gameObject);
}
}
public void ChooseStoryChoice(Choice choice)
{
story.ChooseChoiceIndex(choice.index);
RefreshUI();
}
public string LoadStoryChunk()
{
string text = "";
if (story.canContinue)
{
text = story.ContinueMaximally();
}
return text;
}
}