개발환경 네트워크 문제 개선
This commit is contained in:
159
Assets/Scripts/NetworkConnectionFixer.cs
Normal file
159
Assets/Scripts/NetworkConnectionFixer.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using Unity.Netcode.Transports.UTP;
|
||||
|
||||
namespace Northbound
|
||||
{
|
||||
/// <summary>
|
||||
/// Fixes common network connection issues with Unity Netcode
|
||||
/// </summary>
|
||||
public class NetworkConnectionFixer : MonoBehaviour
|
||||
{
|
||||
[Header("Auto-Fix Settings")]
|
||||
[SerializeField] private bool autoFixOnStart = true;
|
||||
[SerializeField] private bool ensureTransportSettings = true;
|
||||
[SerializeField] private bool increaseTimeouts = true;
|
||||
|
||||
private NetworkManager _networkManager;
|
||||
private UnityTransport _transport;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_networkManager = NetworkManager.Singleton;
|
||||
if (_networkManager == null)
|
||||
{
|
||||
Debug.LogError("[NetworkConnectionFixer] NetworkManager not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
_transport = _networkManager.GetComponent<UnityTransport>();
|
||||
if (_transport == null)
|
||||
{
|
||||
Debug.LogError("[NetworkConnectionFixer] UnityTransport not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (autoFixOnStart)
|
||||
{
|
||||
FixNetworkSettings();
|
||||
}
|
||||
}
|
||||
|
||||
public void FixNetworkSettings()
|
||||
{
|
||||
Debug.Log("[NetworkConnectionFixer] Applying network fixes...");
|
||||
|
||||
if (ensureTransportSettings)
|
||||
{
|
||||
FixTransportSettings();
|
||||
}
|
||||
|
||||
if (increaseTimeouts)
|
||||
{
|
||||
IncreaseTimeouts();
|
||||
}
|
||||
|
||||
Debug.Log("[NetworkConnectionFixer] Network fixes applied successfully!");
|
||||
}
|
||||
|
||||
private void FixTransportSettings()
|
||||
{
|
||||
Debug.Log("[NetworkConnectionFixer] Fixing transport settings...");
|
||||
|
||||
if (_transport == null) return;
|
||||
|
||||
ushort port = _transport.ConnectionData.Port;
|
||||
string address = _transport.ConnectionData.Address;
|
||||
|
||||
Debug.Log($"Current Port: {port}");
|
||||
Debug.Log($"Current Address: {address}");
|
||||
|
||||
if (port <= 0)
|
||||
{
|
||||
Debug.LogWarning("[NetworkConnectionFixer] Invalid port detected, setting to 7777");
|
||||
_transport.SetConnectionData("0.0.0.0", 7777);
|
||||
}
|
||||
}
|
||||
|
||||
private void IncreaseTimeouts()
|
||||
{
|
||||
Debug.Log("[NetworkConnectionFixer] Increasing network timeouts...");
|
||||
|
||||
if (_transport == null) return;
|
||||
|
||||
int currentTimeout = _transport.ConnectTimeoutMS;
|
||||
Debug.Log($"Current Connect Timeout: {currentTimeout}ms");
|
||||
|
||||
if (currentTimeout < 10000)
|
||||
{
|
||||
_transport.ConnectTimeoutMS = 15000;
|
||||
Debug.Log("[NetworkConnectionFixer] Increased Connect Timeout to 15000ms (15 seconds)");
|
||||
}
|
||||
|
||||
int maxAttempts = _transport.MaxConnectAttempts;
|
||||
Debug.Log($"Current Max Connect Attempts: {maxAttempts}");
|
||||
|
||||
if (maxAttempts < 10)
|
||||
{
|
||||
_transport.MaxConnectAttempts = 10;
|
||||
Debug.Log("[NetworkConnectionFixer] Increased Max Connect Attempts to 10");
|
||||
}
|
||||
}
|
||||
|
||||
public void SetServerAddress(string ipAddress, ushort port)
|
||||
{
|
||||
if (_transport == null) return;
|
||||
|
||||
Debug.Log($"[NetworkConnectionFixer] Setting server address to {ipAddress}:{port}");
|
||||
_transport.SetConnectionData(ipAddress, port);
|
||||
}
|
||||
|
||||
public void SetListenAddress(string listenAddress, ushort port)
|
||||
{
|
||||
if (_transport == null) return;
|
||||
|
||||
Debug.Log($"[NetworkConnectionFixer] Setting listen address to {listenAddress}:{port}");
|
||||
_transport.SetConnectionData("0.0.0.0", port, listenAddress);
|
||||
}
|
||||
|
||||
[ContextMenu("Log Current Transport Settings")]
|
||||
public void LogTransportSettings()
|
||||
{
|
||||
if (_transport == null) return;
|
||||
|
||||
Debug.Log("=== CURRENT TRANSPORT SETTINGS ===");
|
||||
Debug.Log("Connection Data:");
|
||||
Debug.Log(" Address: " + _transport.ConnectionData.Address);
|
||||
Debug.Log(" Port: " + _transport.ConnectionData.Port);
|
||||
Debug.Log(" Server Listen Address: " + _transport.ConnectionData.ServerListenAddress);
|
||||
Debug.Log("Timeouts:");
|
||||
Debug.Log(" Connect Timeout (ms): " + _transport.ConnectTimeoutMS);
|
||||
Debug.Log(" Max Connect Attempts: " + _transport.MaxConnectAttempts);
|
||||
Debug.Log("Other:");
|
||||
Debug.Log(" Max Packet Queue Size: " + _transport.MaxPacketQueueSize);
|
||||
Debug.Log("===================================");
|
||||
}
|
||||
|
||||
[ContextMenu("Fix All Known Issues")]
|
||||
public void FixAllKnownIssues()
|
||||
{
|
||||
Debug.Log("[NetworkConnectionFixer] Fixing all known issues...");
|
||||
|
||||
FixNetworkSettings();
|
||||
LogTransportSettings();
|
||||
|
||||
Debug.Log("[NetworkConnectionFixer] All fixes applied!");
|
||||
}
|
||||
|
||||
[ContextMenu("Test Connection Setup")]
|
||||
public void TestConnectionSetup()
|
||||
{
|
||||
Debug.Log("=== CONNECTION SETUP TEST ===");
|
||||
Debug.Log("NetworkManager: " + (_networkManager != null ? "Found" : "NOT FOUND"));
|
||||
Debug.Log("UnityTransport: " + (_transport != null ? "Found" : "NOT FOUND"));
|
||||
Debug.Log("Player Prefab: " + (_networkManager?.NetworkConfig?.PlayerPrefab != null ? "Found" : "NOT FOUND"));
|
||||
Debug.Log("Connection Approval: " + (_networkManager?.NetworkConfig?.ConnectionApproval ?? false));
|
||||
Debug.Log("=============================");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user