네트워크 멀티플레이 대응
This commit is contained in:
@@ -3,24 +3,63 @@ using Unity.Netcode;
|
||||
|
||||
public class AutoHost : MonoBehaviour
|
||||
{
|
||||
// 에디터에서만 작동하도록 설정
|
||||
#if UNITY_EDITOR
|
||||
private bool _hasStarted = false;
|
||||
|
||||
void Start()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// 1. NetworkManager가 씬에 존재하는지 확인
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
// 2. 이미 서버나 클라이언트가 실행 중이 아닐 때만 실행
|
||||
if (!NetworkManager.Singleton.IsServer && !NetworkManager.Singleton.IsClient)
|
||||
{
|
||||
NetworkManager.Singleton.StartHost();
|
||||
Debug.Log("<color=yellow><b>[AutoHost]</b> 에디터 전용 호스트 자동 시작됨</color>");
|
||||
}
|
||||
}
|
||||
else
|
||||
if (NetworkManager.Singleton == null)
|
||||
{
|
||||
Debug.LogError("[AutoHost] NetworkManager를 찾을 수 없습니다!");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsClient)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TryStartAsHost();
|
||||
}
|
||||
|
||||
private void TryStartAsHost()
|
||||
{
|
||||
try
|
||||
{
|
||||
NetworkManager.Singleton.StartHost();
|
||||
_hasStarted = true;
|
||||
Debug.Log("<color=yellow><b>[AutoHost]</b> 호스트로 시작됨 (MAIN EDITOR)</color>");
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.Log($"<color=orange><b>[AutoHost]</b> 호스트 시작 실패: {e.Message}</color>");
|
||||
Debug.Log("<color=blue><b>[AutoHost]</b> 클라이언트 모드로 전환...</color>");
|
||||
|
||||
NetworkManager.Singleton.Shutdown();
|
||||
TryStartAsClient();
|
||||
}
|
||||
}
|
||||
|
||||
private void TryStartAsClient()
|
||||
{
|
||||
try
|
||||
{
|
||||
NetworkManager.Singleton.StartClient();
|
||||
_hasStarted = true;
|
||||
Debug.Log("<color=blue><b>[AutoHost]</b> 클라이언트로 연결됨 (SECONDARY EDITOR)</color>");
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError($"<color=red><b>[AutoHost]</b> 클라이언트 연결 실패: {e.Message}</color>");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
NetworkManager.Singleton.Shutdown();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user