Chat Refactor + Spawn Points Y Axis Fix
This commit is contained in:
61
Assets/Scripts/Chat/LoginUI.cs
Normal file
61
Assets/Scripts/Chat/LoginUI.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user