Untitled

mail@pastecode.io avatar
unknown
csharp
6 days ago
2.8 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;

public class CharacterSelection : MonoBehaviour
{
    public TMP_Dropdown characterDropdown;
    public GameObject[] characterPrefabs;
    public Image characterImage;
    public TextMeshProUGUI characterNameText;
    public TextMeshProUGUI characterDescriptionText;
    public Button startButton;
    public Button mainMenuButton;

    private int selectedCharacterIndex;
    private Character[] characters;

    [Header("Sprites")]
    public Sprite triangle;
    public Sprite capsule;
    public Sprite square;
    public Sprite circle;

    private void Start() {
        characters = new Character[] {
            new Character("Option A", "Character Description", triangle),
            new Character("Option B", "Character Description", capsule),
            new Character("Option C", "Character Description", square),
            new Character("Option D", "Character Description", circle),
        };

        PopulateDropdown();
        characterDropdown.onValueChanged.AddListener(delegate { UpdateCharacterDisplay();});

        characterDropdown.onValueChanged.AddListener(delegate {
            CharacterDropdownValueChanged(characterDropdown);
        });
    }

    void PopulateDropdown() {
        characterDropdown.options.Clear();
        foreach (Character character in characters)
        {
            characterDropdown.options.Add(new TMP_Dropdown.OptionData(character.name));
        }
        characterDropdown.value = 0;
        characterDropdown.RefreshShownValue();
    }

    void CharacterDropdownValueChanged(TMP_Dropdown dropdown) {
        selectedCharacterIndex = dropdown.value;
    }

    public GameObject GetSelectedCharacterPrefab() {
        return characterPrefabs[selectedCharacterIndex];
    }

    public void OnStartRunPressed() {
        PersistentData.Instance.selectedCharacterIndex = selectedCharacterIndex;
        SceneManager.LoadScene("Game Scene");
    }

    void UpdateCharacterDisplay() {
        int index = characterDropdown.value;
        characterNameText.text = characters[index].name;
        characterDescriptionText.text = characters[index].description;
        characterImage.sprite = characters[index].sprite;
    }

    public void MainMenu() {
        SceneManager.LoadScene("Main Menu");
    }
}

[System.Serializable]
public class Character {
    public string name;
    public string description;
    public Sprite sprite;

    public Character(string name, string description, Sprite sprite) {
        this.name = name;
        this.description = description;
        this.sprite = sprite;
    }
}