using UnityEngine; using System.Collections; public class FadingMessage : MonoBehaviour { private string _message; // Message to be displayed private Rect _rectMessage; // Where to display the message on screen private Vector3 _location; // Where the message is being translated from in-world private Color _color; // Current color of the message (fades out) private float _timer; // Timer used to count down until our destruction private float _duration; // How long the message should take to fade out private float _height; // How high the message should rise while fading out void Update () { // Decrement the fade timer _timer += Time.deltaTime; if(_timer < _duration) { // Update the color _color.a = 1 - (_timer / _duration); // Update the position Vector3 point = Camera.main.WorldToScreenPoint(_location); _rectMessage = new Rect(point.x - (GUIState.FadingMessageWidth / 2), (Screen.height - point.y) - (_timer * _height / _duration), GUIState.FadingMessageWidth, GUIState.MenuFontSize * 2); } else { // If we have exceeded the duration, it's time to destroy us! Destroy(gameObject); } } void OnGUI() { GUI.skin = GUIState.GuiSkin; // Ensure that this message is drawn over top of everything else GUI.depth = -1; // Update the color (fade out) GUI.color = _color; // Update font size and alignment GUI.skin.label.fontSize = GUIState.MenuFontSize; GUI.skin.label.alignment = TextAnchor.LowerCenter; // Actually draw the message on screen GUI.Label(_rectMessage, _message); // Reset the GUI's color so it doesn't leak into other GUI scripts GUI.color = Color.white; // Reset the label so it doesn't leak into other GUI scripts GUIState.ResetLabel(); } public void NewMessage(string message, Vector3 location, Color color, float duration = 1, float height = 200) { _message = message; _location = location; _color = color; _duration = duration; _height = height; } }