Created
February 14, 2016 18:07
-
-
Save moon-goon/5260b50925a3ae0a16c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class TextFileHandler : MonoBehaviour { | |
public GameObject diaglogBox; | |
public Text dialogText; | |
public TextAsset textFile; | |
public string[] textLines; | |
public int currentLine; | |
public int endLine; | |
private bool isActive; | |
public Transform player; | |
public Transform target; | |
void Start () { | |
// disable dialogbox at start | |
diaglogBox.gameObject.SetActive(false); | |
if (textFile != null) { | |
textLines = textFile.text.Split('\n'); | |
} | |
// If line ending is not manually set to certain number | |
// set line ending to the size of array -1 because array's index starts with 0 | |
if (endLine == 0) { | |
endLine = textLines.Length - 1; | |
} | |
}// end Start() | |
void Update() { | |
dialogText.text = textLines[currentLine]; | |
if (Input.GetKeyDown("e")) { | |
currentLine++; | |
} | |
if (target) { | |
float dist = Vector3.Distance(target.position, player.position); | |
if (dist < 2.0f) { | |
isActive = true; | |
} | |
else { | |
isActive = false; | |
} | |
} | |
if (isActive) { | |
diaglogBox.gameObject.SetActive(true); | |
if (currentLine == endLine) { | |
diaglogBox.gameObject.SetActive(false); | |
} | |
} | |
else { | |
diaglogBox.gameObject.SetActive(false); | |
} | |
}//end Update() | |
}//end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment