Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / Demos / Basic Demo / Scripts / BasicInkExample.cs
  1. using System;
  2. using Ink.Runtime;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. // This is a super bare bones example of how to play and display a ink story in Unity.
  6. public class BasicInkExample : MonoBehaviour {
  7. public static event Action<Story> OnCreateStory;
  8. void Awake () {
  9. // Remove the default message
  10. RemoveChildren();
  11. StartStory();
  12. }
  13. // Creates a new Story object with the compiled story which we can then play!
  14. void StartStory () {
  15. story = new Story (inkJSONAsset.text);
  16. if(OnCreateStory != null) OnCreateStory(story);
  17. RefreshView();
  18. }
  19. // This is the main function called every time the story changes. It does a few things:
  20. // Destroys all the old content and choices.
  21. // Continues over all the lines of text, then displays all the choices. If there are no choices, the story is finished!
  22. void RefreshView () {
  23. // Remove all the UI on screen
  24. RemoveChildren ();
  25. // Read all the content until we can't continue any more
  26. while (story.canContinue) {
  27. // Continue gets the next line of the story
  28. string text = story.Continue ();
  29. // This removes any white space from the text.
  30. text = text.Trim();
  31. // Display the text on screen!
  32. CreateContentView(text);
  33. }
  34. // Display all the choices, if there are any!
  35. if(story.currentChoices.Count > 0) {
  36. for (int i = 0; i < story.currentChoices.Count; i++) {
  37. Choice choice = story.currentChoices [i];
  38. Button button = CreateChoiceView (choice.text.Trim ());
  39. // Tell the button what to do when we press it
  40. button.onClick.AddListener (delegate {
  41. OnClickChoiceButton (choice);
  42. });
  43. }
  44. }
  45. // If we've read all the content and there's no choices, the story is finished!
  46. else {
  47. Button choice = CreateChoiceView("End of story.\nRestart?");
  48. choice.onClick.AddListener(delegate{
  49. StartStory();
  50. });
  51. }
  52. }
  53. // When we click the choice button, tell the story to choose that choice!
  54. void OnClickChoiceButton (Choice choice) {
  55. story.ChooseChoiceIndex (choice.index);
  56. RefreshView();
  57. }
  58. // Creates a textbox showing the the line of text
  59. void CreateContentView (string text) {
  60. Text storyText = Instantiate (textPrefab) as Text;
  61. storyText.text = text;
  62. storyText.transform.SetParent (canvas.transform, false);
  63. }
  64. // Creates a button showing the choice text
  65. Button CreateChoiceView (string text) {
  66. // Creates the button from a prefab
  67. Button choice = Instantiate (buttonPrefab) as Button;
  68. choice.transform.SetParent (canvas.transform, false);
  69. // Gets the text from the button prefab
  70. Text choiceText = choice.GetComponentInChildren<Text> ();
  71. choiceText.text = text;
  72. // Make the button expand to fit the text
  73. HorizontalLayoutGroup layoutGroup = choice.GetComponent <HorizontalLayoutGroup> ();
  74. layoutGroup.childForceExpandHeight = false;
  75. return choice;
  76. }
  77. // Destroys all the children of this gameobject (all the UI)
  78. void RemoveChildren () {
  79. int childCount = canvas.transform.childCount;
  80. for (int i = childCount - 1; i >= 0; --i) {
  81. Destroy (canvas.transform.GetChild (i).gameObject);
  82. }
  83. }
  84. [SerializeField]
  85. private TextAsset inkJSONAsset = null;
  86. public Story story;
  87. [SerializeField]
  88. private Canvas canvas = null;
  89. // UI Prefabs
  90. [SerializeField]
  91. private Text textPrefab = null;
  92. [SerializeField]
  93. private Button buttonPrefab = null;
  94. }