-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScript.cs
75 lines (60 loc) · 2.06 KB
/
Script.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using UnityEngine;
using System.Collections;
using Ink.Runtime;
public class Script : MonoBehaviour {
[SerializeField]
private TextAsset inkAsset;
private Story _inkStory;
private bool storyNeeded;
[SerializeField]
private Canvas canvas;
[SerializeField]
private float elementPadding;
/* UI Prefabs */
[SerializeField]
private UnityEngine.UI.Text text;
[SerializeField]
private UnityEngine.UI.Button button;
void Awake () {
_inkStory = new Story (inkAsset.text);
storyNeeded = true;
}
// Update is called once per frame
void Update () {
if (storyNeeded == true) {
RemoveChildren ();
float offset = 0;
while (_inkStory.canContinue) {
UnityEngine.UI.Text storyText = Instantiate (text) as UnityEngine.UI.Text;
storyText.text = _inkStory.Continue ();
storyText.transform.SetParent (canvas.transform, false);
storyText.transform.Translate (new Vector2 (0, offset));
offset -= (storyText.fontSize + elementPadding);
}
if(_inkStory.currentChoices.Count > 0) {
for (int ii = 0; ii < _inkStory.currentChoices.Count; ++ii) {
UnityEngine.UI.Button choice = Instantiate (button) as UnityEngine.UI.Button;
choice.transform.SetParent (canvas.transform, false);
choice.transform.Translate (new Vector2 (0, offset));
UnityEngine.UI.Text choiceText = choice.GetComponentInChildren<UnityEngine.UI.Text> ();
choiceText.text = _inkStory.currentChoices [ii].text;
UnityEngine.UI.HorizontalLayoutGroup layoutGroup = choice.GetComponent <UnityEngine.UI.HorizontalLayoutGroup> ();
int choiceId = ii;
choice.onClick.AddListener(delegate{ChoiceSelected(choiceId);});
offset -= (choiceText.fontSize + layoutGroup.padding.top + layoutGroup.padding.bottom + elementPadding);
}
}
storyNeeded = false;
}
}
void RemoveChildren () {
int childCount = canvas.transform.childCount;
for (int i = childCount - 1; i >= 0; --i) {
GameObject.Destroy (canvas.transform.GetChild (i).gameObject);
}
}
public void ChoiceSelected (int id) {
_inkStory.ChooseChoiceIndex (id);
storyNeeded = true;
}
}