Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Assets/Prefabs/PlaceholderObj/TextButton.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Prefabs/PlaceholderObj/TextButton.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Scenes/TestScene.unity
Binary file not shown.
120 changes: 120 additions & 0 deletions Assets/Scripts/TextBoxButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TextBoxButton : MonoBehaviour
{
public GameObject textBox;
public GameObject TextButton;

public Text theText;

public TextAsset textFile;
public string[] textLines;


public int currentLine = 0;
public int endAtLine;

GameObject player;
bool playerContact = false;

private bool isTyping = false;
private bool cancelTyping = false;
public float typeSpeed;

// !for text file, create an empty line for the first line and last line to prevent an array error!
void Start ()
{
textBox.SetActive (false);

if (textFile != null) //making sure there's text
{
textLines = (textFile.text.Split('\n')); //splits text whenever there is a new line
}

if (endAtLine == 0) // create empty lines at the end so Array error does not occur
{
endAtLine = textLines.Length - 1; // last line of text, one less because of array
}
}

void OnTriggerStay2D(Collider2D c)
{
if(playerContact == true && Input.GetKeyDown(KeyCode.E))
{
textBox.SetActive (true); // make text box appear
}
}

void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
playerContact = true;
}
}

void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "Player")
{
playerContact = false;
}
}

// Update is called once per frame
void Update ()
{
//theText.text = textLines [currentLine];//gets text from file

if (playerContact == true && Input.GetKeyDown(KeyCode.E)) //press E for next line
{
if (currentLine >= endAtLine)
{
Destroy(TextButton, 0.0F);//make text button disappear prevents array error
}

if (!isTyping)
{
currentLine += 1; // play next line

if (currentLine > endAtLine) //after last line
{
textBox.SetActive (false); // makes text box disappear
}
else
{
StartCoroutine (TextScroll (textLines [currentLine]));
}
}
else if (isTyping && !cancelTyping)
{
cancelTyping = true;
}


}


}

private IEnumerator TextScroll (string lineOfText) //making words appear word by word
{
int letter = 0;
theText.text = ""; //display nothing on textbox
isTyping = true;
cancelTyping = false;
while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
{
theText.text += lineOfText[letter];
letter += 1; // next letter
yield return new WaitForSeconds(typeSpeed); // waiting for text to appear
}
theText.text = lineOfText; //print whole line of text on screen

isTyping = false;
cancelTyping = false;
}

}
12 changes: 12 additions & 0 deletions Assets/Scripts/TextBoxButton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Assets/TextBox/TestText.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

This is a Text Press E to continue
Press E again to make this disappear
Are you even trying, Press E again
Ok one more time
Ok ok last one this should be the last time
8 changes: 8 additions & 0 deletions Assets/TextBox/TestText.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.