| | | 1 | | using UnityEngine; |
| | | 2 | | using UnityEngine.UI; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System; |
| | | 5 | | using UnityEngine.SceneManagement; |
| | | 6 | | |
| | | 7 | | class ShowMessage : MonoBehaviour { |
| | | 8 | | private Text messageBox; |
| | | 9 | | private CanvasGroup canvasGroup; |
| | | 10 | | |
| | | 11 | | // Get necessary components at start |
| | 1 | 12 | | void Start() { |
| | 1 | 13 | | messageBox = gameObject.GetComponent<Text>(); |
| | 1 | 14 | | canvasGroup = gameObject.GetComponentInParent<CanvasGroup>(); |
| | 1 | 15 | | } |
| | | 16 | | |
| | 7 | 17 | | public void RunMessage(String message) { |
| | 7 | 18 | | StartCoroutine(ShowText(message)); |
| | 7 | 19 | | } |
| | | 20 | | |
| | 7 | 21 | | private IEnumerator ShowText(String message) { |
| | | 22 | | // Set message |
| | 7 | 23 | | messageBox.text = message; |
| | | 24 | | |
| | | 25 | | // Fade in message box if not visible |
| | 3738 | 26 | | while(canvasGroup.alpha < 1){ |
| | 1866 | 27 | | canvasGroup.alpha += Time.deltaTime * 1f; |
| | 1866 | 28 | | yield return null; |
| | 1865 | 29 | | } |
| | | 30 | | |
| | | 31 | | // Wait 5 seconds before fading out |
| | 6 | 32 | | yield return new WaitForSeconds(5f); |
| | | 33 | | |
| | | 34 | | // Fade out message box if visible |
| | 3649 | 35 | | while(canvasGroup.alpha > 0){ |
| | 1822 | 36 | | canvasGroup.alpha -= Time.deltaTime * 1f; |
| | 1822 | 37 | | yield return null; |
| | 1821 | 38 | | } |
| | 5 | 39 | | } |
| | | 40 | | } |