You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Scripts/Dev/CharacterSelectionUI.cs

69 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using D2D.Databases;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CharacterSelectionUI : MonoBehaviour
{
[SerializeField] private List<GameObject> 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
[SerializeField] private GameProgressionDatabase GameProgressionDatabase;
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.");
// GameProgressionDatabase.ResetLevelsPerSession();
// 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);
}
}
}