LobbyMenu (UI Handler)

 avatar
unknown
csharp
10 months ago
2.3 kB
5
Indexable
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;

public class LobbyMenu : MonoBehaviour
{
    LobbyCreateOrJoinHandler _manager;
    bool _isCreateLobbyMenuOpen = false;

    [SerializeField] Animator _createLobbyMenuAnimator;
    [SerializeField] TMP_InputField _lobbyName;
    [SerializeField] TMP_InputField _joinLobbyCode;
    [SerializeField] TextMeshProUGUI _statusText;
    [SerializeField] Button _createLobbyButton;
    [SerializeField] Button _openCreateLobbyButton;
    [SerializeField] Button _joinLobbyByCodeButton;

    void Start()
    {
        _openCreateLobbyButton.onClick.AddListener(ShowCreateLobbyMenu);
        _createLobbyButton.onClick.AddListener(CreateLobby);
        _joinLobbyByCodeButton.onClick.AddListener(JoinLobby);
        _manager = GetComponent<LobbyCreateOrJoinHandler>();
    }

    void Update()
    {
        if (_isCreateLobbyMenuOpen && Input.GetKeyDown(KeyCode.Escape))
        {
            HideCreateLobbyMenu();
        }
    }

    void CreateLobby()
    {
        if (_lobbyName.text.Length > 3 && _lobbyName.text.Length < 17)
        {
            _manager.CreateLobby(_lobbyName.text);
            _lobbyName.text = "";
            _statusText.text = _manager.GetRoomCode();
        }

        else
        {
            _statusText.color = Color.red;
            _lobbyName.text = "";
        }
    }

    void JoinLobby()
    {
        if (_joinLobbyCode.text != string.Empty)
        {
            _manager.JoinLobby(_joinLobbyCode.text);
        }

        _joinLobbyCode.text = "";
    }

    void ShowCreateLobbyMenu()
    {
        if (_isCreateLobbyMenuOpen) return;
        _isCreateLobbyMenuOpen = true;
        _createLobbyMenuAnimator.gameObject.SetActive(true);
        _createLobbyMenuAnimator.Play("Show");
    }

    void HideCreateLobbyMenu()
    {
        if (!_isCreateLobbyMenuOpen) return;

        _isCreateLobbyMenuOpen = false;
        StartCoroutine(HideLobbyMenuCoroutine());
    }

    IEnumerator HideLobbyMenuCoroutine()
    {
        _createLobbyMenuAnimator.Play("Hide");
        yield return new WaitForSeconds(0.75f);
        _createLobbyMenuAnimator.gameObject.SetActive(false);
    }
}
Editor is loading...
Leave a Comment