using UnityEngine; using System.Collections; using System; using System.Linq; using UnityEngine.UI; public class HighscoreLettersManager : MonoBehaviour { private DateTime lastInput; public int InputDelay = 150; public GameObject[] Letters; public Text ResultText; public string XAxis; public int rank; public TimeSpan RunTime = new TimeSpan(0,0,1,30,500); private int _selectionIndex = 0; private bool selectionChanging; bool IsSpawned { get { return lastInput == null || (DateTime.Now - lastInput).Milliseconds < InputDelay; } } // Use this for initialization void Start() { if (Letters != null && Letters.Any()) Letters[0].GetComponent().IsActive = true; } // Update is called once per frame void Update() { if (ResultText != null) ResultText.text = UpdateText(); if (!IsSpawned || selectionChanging) { if (!string.IsNullOrEmpty(XAxis)) { var axis = Input.GetAxis(XAxis); if (Math.Abs(axis) == 1) ChangeSelection(axis == 1); } } } private void ChangeSelection(bool toRight) { selectionChanging = true; lastInput = DateTime.Now; var oldSelection = _selectionIndex; if (toRight) _selectionIndex++; else _selectionIndex--; if (_selectionIndex < 0) _selectionIndex = Letters.Length - 1; else if (_selectionIndex >= Letters.Length) _selectionIndex = 0; Letters[oldSelection].GetComponent().IsActive = false; Letters[_selectionIndex].GetComponent().IsActive = true; selectionChanging = false; } private string UpdateText() { var pseudo = string.Empty; foreach (var letter in Letters) pseudo += letter.GetComponentInChildren().text; return string.Format("{0}/ - \t{1}\t - {2}:{3}:{4}", rank, pseudo, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds); } }