netcode-02/Assets/Scripts/UIManager.cs

120 lines
2.9 KiB
C#
Raw Permalink Normal View History

2022-07-01 09:54:51 +08:00
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
[SerializeField]
private Button startServerButton;
[SerializeField]
private Button startHostButton;
[SerializeField]
private Button startClientButton;
[SerializeField]
private Button leaveButton;
[SerializeField]
private GameObject connectedPanel;
[SerializeField]
private GameObject unconnectedPanel;
// [SerializeField]
// private TextMeshProUGUI playersInGameText;
// [SerializeField]
// private TMP_InputField joinCodeInput;
// [SerializeField]
// private Button executePhysicsButton;
private bool hasServerStarted;
private void Awake()
{
Cursor.visible = true;
}
void Update()
{
// playersInGameText.text = $"Players in game: {PlayersManager.Instance.PlayersInGame}";
}
void Start()
{
UnconnectedPanel();
// START SERVER
startServerButton?.onClick.AddListener(() =>
{
if (NetworkManager.Singleton.StartServer())
ConnectedPanel();
});
// START HOST
startHostButton?.onClick.AddListener(() =>
{
// this allows the UnityMultiplayer and UnityMultiplayerRelay scene to work with and without
// relay features - if the Unity transport is found and is relay protocol then we redirect all the
// traffic through the relay, else it just uses a LAN type (UNET) communication.
// if (RelayManager.Instance.IsRelayEnabled)
// await RelayManager.Instance.SetupRelay();
if (NetworkManager.Singleton.StartHost())
ConnectedPanel();
});
// START CLIENT
startClientButton?.onClick.AddListener(() =>
{
// if (RelayManager.Instance.IsRelayEnabled && !string.IsNullOrEmpty(joinCodeInput.text))
// await RelayManager.Instance.JoinRelay(joinCodeInput.text);
if (NetworkManager.Singleton.StartClient())
ConnectedPanel();
// else
// Logger.Instance.LogInfo("Unable to start client...");
});
leaveButton?.onClick.AddListener(() =>
{
NetworkManager.Singleton.Shutdown();
UnconnectedPanel();
});
// STATUS TYPE CALLBACKS
// NetworkManager.Singleton.OnClientConnectedCallback += (id) =>
// {
// Logger.Instance.LogInfo($"{id} just connected...");
// };
// NetworkManager.Singleton.OnServerStarted += () =>
// {
// hasServerStarted = true;
// };
// executePhysicsButton.onClick.AddListener(() =>
// {
// if (!hasServerStarted)
// {
// Logger.Instance.LogWarning("Server has not started...");
// return;
// }
// SpawnerControl.Instance.SpawnObjects();
// });
}
void ConnectedPanel()
{
unconnectedPanel.SetActive(false);
connectedPanel.SetActive(true);
}
void UnconnectedPanel()
{
unconnectedPanel.SetActive(true);
connectedPanel.SetActive(false);
}
}