using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class CharacterSelectionUI : MonoBehaviour { [SerializeField] private List characters; // List of characters to display [SerializeField] private Button leftButton; // Button to scroll left [SerializeField] private Button rightButton; // Button to scroll right [SerializeField] private Button nextButton; // Button to confirm and save the selection private int currentIndex = 0; // Tracks the currently active character index void Start() { // Initialize the buttons' onClick events leftButton.onClick.AddListener(ScrollLeft); rightButton.onClick.AddListener(ScrollRight); nextButton.onClick.AddListener(SaveSelection); // Show only the first character at the start UpdateCharacterDisplay(); } // Scroll to the left character void ScrollLeft() { if (characters.Count == 0) return; // Decrease the index and wrap around if necessary currentIndex = (currentIndex - 1 + characters.Count) % characters.Count; UpdateCharacterDisplay(); } // Scroll to the right character void ScrollRight() { if (characters.Count == 0) return; // Increase the index and wrap around if necessary currentIndex = (currentIndex + 1) % characters.Count; UpdateCharacterDisplay(); } // Saves the currently selected character index to PlayerPrefs void SaveSelection() { PlayerPrefs.SetInt(Constants.PlayerSelectionKey, currentIndex); PlayerPrefs.Save(); Debug.Log("Character " + currentIndex + " selected and saved."); // SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); } // Updates which character is displayed based on the current index void UpdateCharacterDisplay() { // Loop through each character and set it active or inactive for (int i = 0; i < characters.Count; i++) { characters[i].SetActive(i == currentIndex); } } }