fgm24/Assets/Scripts/Multiplayer/NetworkSetup.cs

62 lines
1.7 KiB
C#

using Netcode.Transports.Facepunch;
using System;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class NetworkSetup : MonoBehaviour
{
[SerializeField] private Button LocalBtn;
[SerializeField] private Button SteamBtn;
[SerializeField] private Button OfflineBtn;
[SerializeField] private GameObject ButtonsHolder;
[SerializeField] private GameObject LoadingText;
public static ServerType serverType = ServerType.NotSelected;
private void OnEnable()
{
OfflineBtn.onClick.AddListener(() => { StartLocalServer(); serverType = ServerType.Offline; });
LocalBtn.onClick.AddListener(()=> { StartLocalServer(); serverType = ServerType.Local; });
SteamBtn.onClick.AddListener(() => { StartSteamServer(); serverType = ServerType.Steam; });
}
private void StartLocalServer()
{
LoadingText.SetActive(true);
ButtonsHolder.SetActive(false);
var transport = NetworkManager.Singleton.gameObject.AddComponent<UnityTransport>();
NetworkManager.Singleton.NetworkConfig.NetworkTransport = transport;
NextScene();
}
private void StartSteamServer()
{
LoadingText.SetActive(true);
ButtonsHolder.SetActive(false);
var transport = NetworkManager.Singleton.gameObject.AddComponent<FacepunchTransport>();
NetworkManager.Singleton.NetworkConfig.NetworkTransport = transport;
NextScene();
}
private void NextScene()
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(++currentScene);
}
}
public enum ServerType
{
NotSelected,
Offline,
Local,
Steam
}