65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
public class AutoHost : MonoBehaviour
|
|
{
|
|
#if UNITY_EDITOR
|
|
private bool _hasStarted = false;
|
|
|
|
void Start()
|
|
{
|
|
if (NetworkManager.Singleton == null)
|
|
{
|
|
Debug.LogError("[AutoHost] NetworkManager를 찾을 수 없습니다!");
|
|
return;
|
|
}
|
|
|
|
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
|
|
} |