83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using Assets.Scripts.Multiplayer;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using Unity.Netcode.Transports.UTP;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class LocalManager : ZNetworkData
|
|
{
|
|
[SerializeField] private Button HostBtn;
|
|
[SerializeField] private Button JoinBtn;
|
|
[SerializeField] private Button StartButton;
|
|
|
|
[SerializeField] private GameObject MainMenu;
|
|
[SerializeField] private GameObject InLobbyMenu;
|
|
|
|
private void Awake()
|
|
{
|
|
if (NetworkManager.Singleton == null) return;
|
|
|
|
bool isLocal = NetworkManager.Singleton.NetworkConfig.NetworkTransport is UnityTransport;
|
|
if (!isLocal) Destroy(gameObject);
|
|
|
|
HostBtn.onClick.AddListener(HostLobby);
|
|
JoinBtn.onClick.AddListener(JoinLobby);
|
|
StartButton.onClick.AddListener(StartGameServer);
|
|
|
|
DisableLoadingText();
|
|
|
|
// Start offline server if selected offline
|
|
if (NetworkSetup.serverType != ServerType.Offline) return;
|
|
|
|
HostLobby();
|
|
StartGameServer();
|
|
}
|
|
|
|
private void HostLobby()
|
|
{
|
|
IsServer = true;
|
|
|
|
NetworkManager.Singleton.StartHost();
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
private void JoinLobby()
|
|
{
|
|
NetworkManager.Singleton.StartClient();
|
|
NetworkManager.Singleton.StartClient();
|
|
|
|
NetworkManager.Singleton.OnClientConnectedCallback += _ => UpdateUI();
|
|
}
|
|
|
|
private void UpdateUI()
|
|
{
|
|
if (!NetworkManager.Singleton.IsConnectedClient)
|
|
{
|
|
// Show main menu
|
|
MainMenu.SetActive(true);
|
|
InLobbyMenu.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
// Show In lobby ui
|
|
MainMenu.SetActive(false);
|
|
InLobbyMenu.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void StartGameServer()
|
|
{
|
|
if (!NetworkManager.Singleton.IsHost) return;
|
|
|
|
// Gets next scene name
|
|
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
|
|
string nextSceneName = SceneUtility.GetScenePathByBuildIndex(nextSceneIndex);
|
|
|
|
NetworkManager.Singleton.SceneManager.LoadScene(nextSceneName, UnityEngine.SceneManagement.LoadSceneMode.Single);
|
|
}
|
|
} |