개발자용 멀티플레이 기능 추가
This commit is contained in:
295
Assets/Scripts/Editor/NetworkConnectionWindow.cs
Normal file
295
Assets/Scripts/Editor/NetworkConnectionWindow.cs
Normal file
@@ -0,0 +1,295 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Unity.Netcode;
|
||||
using System.IO;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Northbound.Editor
|
||||
{
|
||||
public class NetworkConnectionWindow : UnityEditor.EditorWindow
|
||||
{
|
||||
private string _serverIP = "127.0.0.1";
|
||||
private string _port = "7777";
|
||||
private bool _isHost = true;
|
||||
private NetworkConnectionMode _connectionMode = NetworkConnectionMode.Host;
|
||||
private string _savedSettingsPath;
|
||||
|
||||
private enum NetworkConnectionMode
|
||||
{
|
||||
Host,
|
||||
Client,
|
||||
Server
|
||||
}
|
||||
|
||||
[MenuItem("Window/Network/Connection Manager %#n")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
var window = GetWindow<NetworkConnectionWindow>("Network Connection");
|
||||
window.minSize = new Vector2(350, 250);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_savedSettingsPath = Path.Combine(Application.persistentDataPath, "NetworkConnectionSettings.json");
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
GUILayout.Label("Network Connection Manager", EditorStyles.boldLabel);
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
DrawConnectionModeUI();
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
DrawConnectionSettings();
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
DrawStatusDisplay();
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
DrawActionButtons();
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
DrawSettingsButtons();
|
||||
}
|
||||
|
||||
private void DrawConnectionModeUI()
|
||||
{
|
||||
EditorGUILayout.LabelField("Connection Mode", EditorStyles.boldLabel);
|
||||
_connectionMode = (NetworkConnectionMode)EditorGUILayout.EnumPopup("Mode", _connectionMode);
|
||||
|
||||
if (_connectionMode == NetworkConnectionMode.Host)
|
||||
{
|
||||
_isHost = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawConnectionSettings()
|
||||
{
|
||||
EditorGUILayout.LabelField("Connection Settings", EditorStyles.boldLabel);
|
||||
|
||||
if (_connectionMode == NetworkConnectionMode.Client)
|
||||
{
|
||||
_serverIP = EditorGUILayout.TextField("Server IP", _serverIP);
|
||||
_port = EditorGUILayout.TextField("Port", _port);
|
||||
}
|
||||
else if (_connectionMode == NetworkConnectionMode.Host || _connectionMode == NetworkConnectionMode.Server)
|
||||
{
|
||||
_port = EditorGUILayout.TextField("Port", _port);
|
||||
}
|
||||
|
||||
if (GUI.changed)
|
||||
{
|
||||
SaveSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawStatusDisplay()
|
||||
{
|
||||
EditorGUILayout.LabelField("Status", EditorStyles.boldLabel);
|
||||
|
||||
string status = GetNetworkStatus();
|
||||
Color statusColor = GetStatusColor();
|
||||
|
||||
var oldColor = GUI.color;
|
||||
GUI.color = statusColor;
|
||||
EditorGUILayout.LabelField(status, EditorStyles.helpBox);
|
||||
GUI.color = oldColor;
|
||||
}
|
||||
|
||||
private void DrawActionButtons()
|
||||
{
|
||||
EditorGUI.BeginDisabledGroup(IsNetworkActive());
|
||||
|
||||
if (GUILayout.Button(_connectionMode == NetworkConnectionMode.Client ? "Connect" : "Start", GUILayout.Height(30)))
|
||||
{
|
||||
StartConnection();
|
||||
}
|
||||
|
||||
EditorGUI.EndDisabledGroup();
|
||||
|
||||
EditorGUI.BeginDisabledGroup(!IsNetworkActive());
|
||||
|
||||
if (GUILayout.Button("Disconnect", GUILayout.Height(30)))
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
EditorGUI.EndDisabledGroup();
|
||||
}
|
||||
|
||||
private void DrawSettingsButtons()
|
||||
{
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
|
||||
if (GUILayout.Button("Save Settings"))
|
||||
{
|
||||
SaveSettings();
|
||||
Debug.Log("[NetworkConnectionWindow] Settings saved.");
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Load Settings"))
|
||||
{
|
||||
LoadSettings();
|
||||
Debug.Log("[NetworkConnectionWindow] Settings loaded.");
|
||||
}
|
||||
|
||||
EditorGUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
private void StartConnection()
|
||||
{
|
||||
if (NetworkManager.Singleton == null)
|
||||
{
|
||||
EditorUtility.DisplayDialog("Error", "NetworkManager not found in the scene!", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
ushort port;
|
||||
if (!ushort.TryParse(_port, out port))
|
||||
{
|
||||
EditorUtility.DisplayDialog("Error", "Invalid port number!", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>()?.SetConnectionData(
|
||||
_serverIP,
|
||||
port
|
||||
);
|
||||
|
||||
switch (_connectionMode)
|
||||
{
|
||||
case NetworkConnectionMode.Host:
|
||||
NetworkManager.Singleton.StartHost();
|
||||
Debug.Log($"[NetworkConnectionWindow] Started Host on port {port}");
|
||||
break;
|
||||
|
||||
case NetworkConnectionMode.Client:
|
||||
NetworkManager.Singleton.StartClient();
|
||||
Debug.Log($"[NetworkConnectionWindow] Started Client connecting to {_serverIP}:{port}");
|
||||
break;
|
||||
|
||||
case NetworkConnectionMode.Server:
|
||||
NetworkManager.Singleton.StartServer();
|
||||
Debug.Log($"[NetworkConnectionWindow] Started Server on port {port}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
NetworkManager.Singleton.Shutdown();
|
||||
Debug.Log("[NetworkConnectionWindow] Disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsNetworkActive()
|
||||
{
|
||||
return NetworkManager.Singleton != null &&
|
||||
(NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer);
|
||||
}
|
||||
|
||||
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 Color GetStatusColor()
|
||||
{
|
||||
if (NetworkManager.Singleton == null)
|
||||
return Color.red;
|
||||
|
||||
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer)
|
||||
return new Color(0f, 0.7f, 0f);
|
||||
|
||||
return new Color(1f, 0.7f, 0f);
|
||||
}
|
||||
|
||||
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 SaveSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = $"{{\"serverIP\":\"{_serverIP}\",\"port\":\"{_port}\",\"connectionMode\":{_connectionMode}}}";
|
||||
File.WriteAllText(_savedSettingsPath, json);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError($"[NetworkConnectionWindow] Failed to save settings: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(_savedSettingsPath))
|
||||
{
|
||||
string json = File.ReadAllText(_savedSettingsPath);
|
||||
var settings = JsonUtility.FromJson<ConnectionSettings>(json);
|
||||
_serverIP = settings.serverIP;
|
||||
_port = settings.port;
|
||||
_connectionMode = (NetworkConnectionMode)settings.connectionMode;
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError($"[NetworkConnectionWindow] Failed to load settings: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (IsNetworkActive())
|
||||
{
|
||||
Repaint();
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class ConnectionSettings
|
||||
{
|
||||
public string serverIP;
|
||||
public string port;
|
||||
public int connectionMode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user