개발자용 멀티플레이 기능 추가
This commit is contained in:
269
Assets/Scripts/NetworkJoinUI.cs
Normal file
269
Assets/Scripts/NetworkJoinUI.cs
Normal file
@@ -0,0 +1,269 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Unity.Netcode;
|
||||
|
||||
namespace Northbound
|
||||
{
|
||||
public class NetworkJoinUI : MonoBehaviour
|
||||
{
|
||||
[Header("UI References")]
|
||||
[SerializeField] private GameObject joinPanel;
|
||||
[SerializeField] private InputField ipInputField;
|
||||
[SerializeField] private InputField portInputField;
|
||||
[SerializeField] private Button joinButton;
|
||||
[SerializeField] private Button hostButton;
|
||||
[SerializeField] private Button disconnectButton;
|
||||
[SerializeField] private Text statusText;
|
||||
[SerializeField] private KeyCode toggleKey = KeyCode.J;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private string defaultIP = "127.0.0.1";
|
||||
[SerializeField] private string defaultPort = "7777";
|
||||
|
||||
private bool _isVisible = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeUI();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
SetupDefaultValues();
|
||||
SubscribeToEvents();
|
||||
UpdateStatus();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(toggleKey))
|
||||
{
|
||||
TogglePanel();
|
||||
}
|
||||
|
||||
if (NetworkManager.Singleton != null &&
|
||||
(NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer))
|
||||
{
|
||||
UpdateStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeUI()
|
||||
{
|
||||
if (joinPanel != null)
|
||||
{
|
||||
joinPanel.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupDefaultValues()
|
||||
{
|
||||
if (ipInputField != null)
|
||||
{
|
||||
ipInputField.text = defaultIP;
|
||||
}
|
||||
|
||||
if (portInputField != null)
|
||||
{
|
||||
portInputField.text = defaultPort;
|
||||
}
|
||||
}
|
||||
|
||||
private void SubscribeToEvents()
|
||||
{
|
||||
if (joinButton != null)
|
||||
{
|
||||
joinButton.onClick.AddListener(OnJoinClicked);
|
||||
}
|
||||
|
||||
if (hostButton != null)
|
||||
{
|
||||
hostButton.onClick.AddListener(OnHostClicked);
|
||||
}
|
||||
|
||||
if (disconnectButton != null)
|
||||
{
|
||||
disconnectButton.onClick.AddListener(OnDisconnectClicked);
|
||||
}
|
||||
}
|
||||
|
||||
private void UnsubscribeFromEvents()
|
||||
{
|
||||
if (joinButton != null)
|
||||
{
|
||||
joinButton.onClick.RemoveListener(OnJoinClicked);
|
||||
}
|
||||
|
||||
if (hostButton != null)
|
||||
{
|
||||
hostButton.onClick.RemoveListener(OnHostClicked);
|
||||
}
|
||||
|
||||
if (disconnectButton != null)
|
||||
{
|
||||
disconnectButton.onClick.RemoveListener(OnDisconnectClicked);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnJoinClicked()
|
||||
{
|
||||
if (NetworkManager.Singleton == null)
|
||||
{
|
||||
ShowError("NetworkManager not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
string ip = ipInputField != null ? ipInputField.text : defaultIP;
|
||||
string port = portInputField != null ? portInputField.text : defaultPort;
|
||||
|
||||
ushort portNum;
|
||||
if (!ushort.TryParse(port, out portNum))
|
||||
{
|
||||
ShowError("Invalid port number!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer)
|
||||
{
|
||||
ShowError("Already connected!");
|
||||
return;
|
||||
}
|
||||
|
||||
var transport = NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
||||
if (transport != null)
|
||||
{
|
||||
transport.SetConnectionData(ip, portNum);
|
||||
}
|
||||
|
||||
NetworkManager.Singleton.StartClient();
|
||||
UpdateStatus();
|
||||
Debug.Log($"[NetworkJoinUI] Connecting to {ip}:{port}");
|
||||
}
|
||||
|
||||
private void OnHostClicked()
|
||||
{
|
||||
if (NetworkManager.Singleton == null)
|
||||
{
|
||||
ShowError("NetworkManager not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
string port = portInputField != null ? portInputField.text : defaultPort;
|
||||
|
||||
ushort portNum;
|
||||
if (!ushort.TryParse(port, out portNum))
|
||||
{
|
||||
ShowError("Invalid port number!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer)
|
||||
{
|
||||
ShowError("Already connected!");
|
||||
return;
|
||||
}
|
||||
|
||||
var transport = NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
||||
if (transport != null)
|
||||
{
|
||||
transport.SetConnectionData("0.0.0.0", portNum);
|
||||
}
|
||||
|
||||
NetworkManager.Singleton.StartHost();
|
||||
UpdateStatus();
|
||||
Debug.Log($"[NetworkJoinUI] Started Host on port {port}");
|
||||
}
|
||||
|
||||
private void OnDisconnectClicked()
|
||||
{
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
NetworkManager.Singleton.Shutdown();
|
||||
UpdateStatus();
|
||||
Debug.Log("[NetworkJoinUI] Disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStatus()
|
||||
{
|
||||
if (statusText == null) return;
|
||||
|
||||
string status = GetNetworkStatus();
|
||||
statusText.text = status;
|
||||
}
|
||||
|
||||
private string GetNetworkStatus()
|
||||
{
|
||||
if (NetworkManager.Singleton == null)
|
||||
return "NetworkManager not found";
|
||||
|
||||
if (NetworkManager.Singleton.IsHost)
|
||||
return $"Hosting (Port: {GetActivePort()})";
|
||||
if (NetworkManager.Singleton.IsServer)
|
||||
return $"Server (Port: {GetActivePort()})";
|
||||
if (NetworkManager.Singleton.IsClient)
|
||||
return $"Client connected to {GetActiveIP()}:{GetActivePort()}";
|
||||
|
||||
return "Not connected";
|
||||
}
|
||||
|
||||
private string GetActiveIP()
|
||||
{
|
||||
if (NetworkManager.Singleton == null) return "N/A";
|
||||
|
||||
var transport = NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
||||
if (transport != null)
|
||||
{
|
||||
return transport.ConnectionData.Address;
|
||||
}
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
private string GetActivePort()
|
||||
{
|
||||
if (NetworkManager.Singleton == null) return "N/A";
|
||||
|
||||
var transport = NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
||||
if (transport != null)
|
||||
{
|
||||
return transport.ConnectionData.Port.ToString();
|
||||
}
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
private void TogglePanel()
|
||||
{
|
||||
if (joinPanel != null)
|
||||
{
|
||||
_isVisible = !_isVisible;
|
||||
joinPanel.SetActive(_isVisible);
|
||||
UpdateStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowError(string message)
|
||||
{
|
||||
Debug.LogError($"[NetworkJoinUI] {message}");
|
||||
if (statusText != null)
|
||||
{
|
||||
statusText.text = $"Error: {message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
UnsubscribeFromEvents();
|
||||
}
|
||||
|
||||
public void SetUIReferences(GameObject panel, InputField ipField, InputField portField, Button joinBtn, Button hostBtn, Button disconnectBtn, Text status)
|
||||
{
|
||||
joinPanel = panel;
|
||||
ipInputField = ipField;
|
||||
portInputField = portField;
|
||||
joinButton = joinBtn;
|
||||
hostButton = hostBtn;
|
||||
disconnectButton = disconnectBtn;
|
||||
statusText = status;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user