mirror of
https://github.com/Ratstail91/Mementos.git
synced 2025-11-29 10:34:27 +11:00
Committed everything
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using Ink.Runtime;
|
||||
|
||||
// This is a super bare bones example of how to play and display a ink story in Unity.
|
||||
public class BasicInkExample : MonoBehaviour {
|
||||
public static event Action<Story> OnCreateStory;
|
||||
|
||||
void Awake () {
|
||||
// Remove the default message
|
||||
RemoveChildren();
|
||||
StartStory();
|
||||
}
|
||||
|
||||
// Creates a new Story object with the compiled story which we can then play!
|
||||
void StartStory () {
|
||||
story = new Story (inkJSONAsset.text);
|
||||
if(OnCreateStory != null) OnCreateStory(story);
|
||||
RefreshView();
|
||||
}
|
||||
|
||||
// This is the main function called every time the story changes. It does a few things:
|
||||
// Destroys all the old content and choices.
|
||||
// Continues over all the lines of text, then displays all the choices. If there are no choices, the story is finished!
|
||||
void RefreshView () {
|
||||
// Remove all the UI on screen
|
||||
RemoveChildren ();
|
||||
|
||||
// Read all the content until we can't continue any more
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// When we click the choice button, tell the story to choose that choice!
|
||||
void OnClickChoiceButton (Choice choice) {
|
||||
story.ChooseChoiceIndex (choice.index);
|
||||
RefreshView();
|
||||
}
|
||||
|
||||
// Creates a textbox showing the the line of text
|
||||
void CreateContentView (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;
|
||||
|
||||
return choice;
|
||||
}
|
||||
|
||||
// Destroys all the children of this gameobject (all the UI)
|
||||
void RemoveChildren () {
|
||||
int childCount = canvas.transform.childCount;
|
||||
for (int i = childCount - 1; i >= 0; --i) {
|
||||
GameObject.Destroy (canvas.transform.GetChild (i).gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private TextAsset inkJSONAsset = null;
|
||||
public Story story;
|
||||
|
||||
[SerializeField]
|
||||
private Canvas canvas = null;
|
||||
|
||||
// UI Prefabs
|
||||
[SerializeField]
|
||||
private Text textPrefab = null;
|
||||
[SerializeField]
|
||||
private Button buttonPrefab = null;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66e957d58a4584a8894329531ec0b096
|
||||
timeCreated: 1473758854
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9afc9fe3049ac409e886991c340e1a33
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Ink.UnityIntegration;
|
||||
using Ink.Runtime;
|
||||
|
||||
[CustomEditor(typeof(BasicInkExample))]
|
||||
[InitializeOnLoad]
|
||||
public class BasicInkExampleEditor : Editor {
|
||||
|
||||
static BasicInkExampleEditor () {
|
||||
BasicInkExample.OnCreateStory += OnCreateStory;
|
||||
}
|
||||
|
||||
static void OnCreateStory (Story story) {
|
||||
// If you'd like NOT to automatically show the window and attach (your teammates may appreciate it!) then replace "true" with "false" here.
|
||||
InkPlayerWindow window = InkPlayerWindow.GetWindow(true);
|
||||
if(window != null) InkPlayerWindow.Attach(story);
|
||||
}
|
||||
public override void OnInspectorGUI () {
|
||||
Repaint();
|
||||
base.OnInspectorGUI ();
|
||||
var realTarget = target as BasicInkExample;
|
||||
var story = realTarget.story;
|
||||
InkPlayerWindow.DrawStoryPropertyField(story, new GUIContent("Story"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c04f2595983345ab86d4ef0eea06dea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class QuitGameOnKeypress : MonoBehaviour {
|
||||
|
||||
public KeyCode key = KeyCode.Escape;
|
||||
|
||||
void Update () {
|
||||
if(Input.GetKeyDown(key)) Application.Quit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ce0d8573e1b2ff42af74cb80f3e5ece
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user