62 lines
1.7 KiB
C#
62 lines
1.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Mirror;
|
|
using TMPro;
|
|
|
|
public class LoginUI : MonoBehaviour
|
|
{
|
|
[Header("UI Elements")]
|
|
[SerializeField] internal TMP_InputField usernameInput;
|
|
[SerializeField] internal Button clientButton;
|
|
[SerializeField] internal Text errorText;
|
|
|
|
public static LoginUI instance;
|
|
|
|
string originalNetworkAddress;
|
|
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(NetworkManager.singleton.networkAddress))
|
|
NetworkManager.singleton.networkAddress = "localhost";
|
|
|
|
originalNetworkAddress = NetworkManager.singleton.networkAddress;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(NetworkManager.singleton.networkAddress))
|
|
NetworkManager.singleton.networkAddress = originalNetworkAddress;
|
|
}
|
|
|
|
public void ToggleButton(string username)
|
|
{
|
|
clientButton.interactable = !string.IsNullOrWhiteSpace(username);
|
|
}
|
|
|
|
public void OnClientButton()
|
|
{
|
|
var username = usernameInput.text.Trim();
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
if (errorText != null) { errorText.text = "Enter a username"; errorText.gameObject.SetActive(true); }
|
|
return;
|
|
}
|
|
|
|
// set the name on the ChatAuthenticator (if present)
|
|
var auth = NetworkManager.singleton.GetComponent<ChatAuthenticator>();
|
|
if (auth != null)
|
|
auth.SetPlayerName(username);
|
|
else
|
|
Debug.LogWarning("LoginUI: ChatAuthenticator not found on NetworkManager.");
|
|
|
|
// start client
|
|
NetworkManager.singleton.StartClient();
|
|
}
|
|
}
|