using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIControl : MonoBehaviour { public Text score, high, messageText, failedMessageText, speed, gapSize, wallWidth; public bool resetting = false; public static UIControl Instance; private int currentScore, currentHigh; void Start() { Instance = this; currentHigh = Manager.playerStatistics.highScore; ResetScore(); } void Update() { RunningTrack track = RunningTrack.Instance; speed.text = string.Format("{0:N1}m/s", track.WallSpeed); gapSize.text = string.Format("{0:N1}m", track.WallSpawnDistance); wallWidth.text = string.Format("{0:N1}m", track.MaxWallWidth); } public void AddScore(int value) { if (resetting) return; currentScore += value; if (currentScore > currentHigh) { currentHigh = currentScore; Manager.playerStatistics.highScore = currentHigh; Manager.Save(Manager.playerStatistics); } score.text = currentScore.ToString(); high.text = currentHigh.ToString(); } public void ResetScore() { currentScore = 0; AddScore(0); } public void NewMessage(string message) { messageText.text = message; StartCoroutine(FadeMessage(messageText, 0.02f)); } public void FailedMessage() { StartCoroutine(FadeMessage(failedMessageText, 0.1f)); } IEnumerator FadeMessage(Text text, float fadeSpeed) { Color old = text.color; float r = old.r, g = old.g, b = old.b; for (float a = 0; a <= 1; a += fadeSpeed) { text.color = new Color(r, g, b, a); yield return null; } text.color = new Color(r, g, b, 1); yield return new WaitForSeconds(1f); for (float a = 1; a >= 0; a -= fadeSpeed) { text.color = new Color(r, g, b, a); yield return null; } text.color = new Color(r, g, b, 0); } }