Кнопка, использующая выбор - PullRequest
0 голосов
/ 12 мая 2019

Я хочу сделать кнопку, которая имеет действие «ВЫБОР», чтобы не загружать сцену напрямую.Я сделал игру с 3 комнатами, и я хочу, чтобы игрок выбрал комнату, а затем нажмите другую кнопку, чтобы начать играть (например, СТАРТ).Как я могу это сделать?

1 Ответ

0 голосов
/ 12 мая 2019
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

[System.Serializable]
public struct SceneButton
{
    public Button Button;
    public string SceneName;

    public void TurnOn()
    {
        // Do what you want to indicate the button is selected
        // Animate it, change colour, ....
    }

    public void TurnOff()
    {
        // Do what you want to indicate the button is NOT selected
        // Animate it, change colour, ....
    }
}
// Attach this script to the gameObject you want
// On an empty or the confirm button for instance
public class SceneSelector : MonoBehaviour
{
    // Fill the array in the inspector
    [SerializeField]
    private SceneButton[] sceneButtons;

    private int selectedButtonIndex = -1;

    private void Start()
    {
        SetupButtons();
    }

    private void SetupButtons()
    {
        for ( int i = 0 ; i < sceneButtons.Length ; ++i )
        {
            int index = i;
            sceneButtons[i].Button.onClick.AddListener( () => SelectButton( index ) );
        }
    }

    public void SelectButton( int buttonIndex )
    {
        if ( selectedButtonIndex >= 0 )
            sceneButtons[selectedButtonIndex].TurnOff();

        selectedButtonIndex = buttonIndex;

        if ( selectedButtonIndex >= 0 )
            sceneButtons[selectedButtonIndex].TurnOn();
    }

    // Use this function as a callback of your button's `onClick`
    // used to confirm the choice of the user
    public void LoadSelectedScene()
    {
        if ( selectedButtonIndex >= 0 )
        {
            SceneManager.LoadScene( sceneButtons[selectedButtonIndex].SceneName );
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...