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/CharactersScript.cs

111 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CharactersScript : MonoBehaviour
{
public GameObject[] characters; // Array of character models or portraits
private int currentIndex = 0;
public Button nextButton;
public Button prevButton;
public Button selectButton; // Separate "Select" button for current character
void Start()
{
nextButton.onClick.AddListener(ShowNextCharacter);
prevButton.onClick.AddListener(ShowPreviousCharacter);
selectButton.onClick.AddListener(OnSelectButtonClick);
// Load the saved character name from PlayerPrefs
string savedCharacterName = PlayerPrefs.GetString("selectedCharacterKey");
if (string.IsNullOrEmpty(savedCharacterName))
{
currentIndex = 0; // Automatically select the first character
PlayerPrefs.SetString("selectedCharacterKey", characters[currentIndex].name);
PlayerPrefs.Save();
}
else
{
// Find the index of the saved character
currentIndex = Array.FindIndex(characters, character => character.name == savedCharacterName);
if (currentIndex < 0 || currentIndex >= characters.Length)
{
currentIndex = 0; // Default to the first character if not found
}
}
// Show the initial character and update the button text
ShowCharacter(currentIndex);
UpdateSelectButtonText(characters[currentIndex]);
}
void ShowNextCharacter()
{
currentIndex = (currentIndex + 1) % characters.Length;
ShowCharacter(currentIndex);
UpdateSelectButtonText(characters[currentIndex]);
}
void ShowPreviousCharacter()
{
currentIndex = (currentIndex - 1 + characters.Length) % characters.Length;
ShowCharacter(currentIndex);
UpdateSelectButtonText(characters[currentIndex]);
}
void OnSelectButtonClick()
{
// Handle the selection logic
GameObject selectedCharacter = characters[currentIndex];
// Save the selected character name to PlayerPrefs
PlayerPrefs.SetString("selectedCharacterKey", selectedCharacter.name);
PlayerPrefs.SetInt("currentIndex", currentIndex);
PlayerPrefs.Save();
Debug.Log("current index" + currentIndex);
Debug.Log("Selected Character: " + selectedCharacter.name);
// Update the "Select" button text to "Selected"
UpdateSelectButtonText(selectedCharacter);
}
void ShowCharacter(int index)
{
// Hide all characters
foreach (GameObject character in characters)
{
character.SetActive(false);
}
// Show the character at the specified index
if (index >= 0 && index < characters.Length)
{
characters[index].SetActive(true);
}
}
void UpdateSelectButtonText(GameObject currentCharacter)
{
// Update the "Select" button text to "Selected" if this character is the one saved in PlayerPrefs
string savedCharacterName = PlayerPrefs.GetString("selectedCharacterKey");
if (currentCharacter.name == savedCharacterName)
{
selectButton.GetComponentInChildren<Text>().text = "Selected";
}
else
{
selectButton.GetComponentInChildren<Text>().text = "Select";
}
}
public void playGame()
{
SceneManager.LoadScene("Level_1");
}
}