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

123 lines
4.3 KiB
C#

using Animancer;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
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
public Avatar[] characterAvatar;
void Start()
{
nextButton.onClick.AddListener(ShowNextCharacter);
prevButton.onClick.AddListener(ShowPreviousCharacter);
//selectButton.onClick.AddListener(OnSelectButtonClick);
// Load the saved character index from PlayerPrefs
if (PlayerPrefs.HasKey("selectedCharacterIndex"))
{
currentIndex = PlayerPrefs.GetInt("selectedCharacterIndex");
}
else
{
currentIndex = 0; // Default to the first character
PlayerPrefs.SetInt("selectedCharacterIndex", currentIndex);
PlayerPrefs.Save();
}
// Validate the loaded index
if (currentIndex < 0 || currentIndex >= characters.Length)
{
currentIndex = 0; // Default to the first character if the saved index is invalid
}
// Show the initial character and update the button text
ShowCharacter(currentIndex);
// UpdateSelectButtonText(currentIndex);
}
void ShowNextCharacter()
{
currentIndex = (currentIndex + 1) % characters.Length;
ShowCharacter(currentIndex);
// UpdateSelectButtonText(currentIndex);
}
void ShowPreviousCharacter()
{
currentIndex = (currentIndex - 1 + characters.Length) % characters.Length;
ShowCharacter(currentIndex);
// UpdateSelectButtonText(currentIndex);
}
public void OnSelectButtonClick()
{
// Save the selected character index to PlayerPrefs
PlayerPrefs.SetInt("selectedCharacterIndex", currentIndex);
PlayerPrefs.Save();
Debug.Log("Current Index: " + currentIndex);
Debug.Log("Selected Character: " + characters[currentIndex].name);
foreach (GameObject character in characters)
{
//character.SetActive(true);
character.GetComponent<Animator>().runtimeAnimatorController = null;
character.transform.GetChild(2).gameObject.SetActive(false);
}
// Update the "Select" button text to "Selected"
// UpdateSelectButtonText(currentIndex);
}
void ShowCharacter(int index)
{
// Hide all characters
foreach (GameObject character in characters)
{
character.SetActive(false);
}
characters[index].transform.parent.GetComponent<AnimancerComponent>().Animator.avatar = characterAvatar[index];
// Show the character at the specified index
if (index >= 0 && index < characters.Length)
{
characters[index].SetActive(true);
//characters[index].transform.GetChild(2).transform.DOLocalMove(new Vector3(-1, 0, -1),2f).SetLoops(-1);
}
}
//void UpdateSelectButtonText(int index)
//{
// // Update the "Select" button text to "Selected" if this character is the one saved in PlayerPrefs
// int savedIndex = PlayerPrefs.GetInt("selectedCharacterIndex", -1);
// if (index == savedIndex)
// {
// selectButton.GetComponentInChildren<Text>().text = "Selected";
// }
// else
// {
// selectButton.GetComponentInChildren<Text>().text = "Select";
// }
// //characters[savedIndex].SetActive(true);
// //characters[savedIndex].transform.parent.GetComponent<AnimancerComponent>().Animator.avatar = characterAvatar[savedIndex];
// characters[index].transform.parent.GetComponent<AnimancerComponent>().Animator.avatar = characterAvatar[index];
//}
public void playGame()
{
foreach (GameObject character in characters)
{
//character.SetActive(true);
character.GetComponent<Animator>().runtimeAnimatorController = null;
character.transform.GetChild(2).gameObject.SetActive(false);
}
}
}