Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using Unity.VisualScripting;
- using UnityEngine;
- public class MainMenuNavigation : MonoBehaviour
- {
- [SerializeField] private TextMeshProUGUI[] menuText;
- private int totalMenuOptions;
- private int currentIndex = 0;
- private KeyCode selectionUp = KeyCode.UpArrow;
- private KeyCode selectionDown = KeyCode.DownArrow;
- private void Start()
- {
- totalMenuOptions = menuText.Length;
- UpdateSelectedMenuItem(0, 0);
- }
- private void UpdateSelectedMenuItem(int pastSelected, int newSelected)
- {
- TextMeshProUGUI pastSelectedText = menuText[pastSelected];
- TextMeshProUGUI currentSelectedText = menuText[newSelected];
- if (pastSelectedText.text[0] == '>')
- {
- pastSelectedText.text = pastSelectedText.text.Substring(2);
- }
- currentSelectedText.text = "> " + currentSelectedText.text;
- }
- private void Update()
- {
- if (Input.anyKey)
- {
- int previousSelection = currentIndex;
- if (Input.GetKeyDown(selectionUp))
- {
- currentIndex = (currentIndex - 1 < 0) ? totalMenuOptions - 1 : currentIndex - 1;
- }
- if (Input.GetKeyDown(selectionDown))
- {
- currentIndex = (currentIndex + 1 > totalMenuOptions - 1) ? 0 : currentIndex + 1;
- }
- UpdateSelectedMenuItem(previousSelection, currentIndex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement