while (story.canContinue) { // Continue gets the next line of the story string text = story.Continue (); // This removes any white space from the text. text = text.Trim(); // Display the text on screen! CreateContentView(text); }
// Display all the choices, if there are any! if(story.currentChoices.Count > 0) { for (int i = 0; i < story.currentChoices.Count; i++) { Choice choice = story.currentChoices [i]; Button button = CreateChoiceView (choice.text.Trim ()); // Tell the button what to do when we press it button.onClick.AddListener (delegate { OnClickChoiceButton (choice); }); } } // If we've read all the content and there's no choices, the story is finished! else { Button choice = CreateChoiceView("End of story.\nRestart?"); choice.onClick.AddListener(delegate{ StartStory(); }); }
voidCreateContentView (string text) { Text storyText = Instantiate (textPrefab) as Text; storyText.text = text; storyText.transform.SetParent (canvas.transform, false); }
// Creates a button showing the choice text Button CreateChoiceView (string text) { // Creates the button from a prefab Button choice = Instantiate (buttonPrefab) as Button; choice.transform.SetParent (canvas.transform, false);
// Gets the text from the button prefab Text choiceText = choice.GetComponentInChildren<Text> (); choiceText.text = text;
// Make the button expand to fit the text HorizontalLayoutGroup layoutGroup = choice.GetComponent <HorizontalLayoutGroup> (); layoutGroup.childForceExpandHeight = false;