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("[AutoHost] 호스트로 시작됨 (MAIN EDITOR)");
}
catch (System.Exception e)
{
Debug.Log($"[AutoHost] 호스트 시작 실패: {e.Message}");
Debug.Log("[AutoHost] 클라이언트 모드로 전환...");
NetworkManager.Singleton.Shutdown();
TryStartAsClient();
}
}
private void TryStartAsClient()
{
try
{
NetworkManager.Singleton.StartClient();
_hasStarted = true;
Debug.Log("[AutoHost] 클라이언트로 연결됨 (SECONDARY EDITOR)");
}
catch (System.Exception e)
{
Debug.LogError($"[AutoHost] 클라이언트 연결 실패: {e.Message}");
}
}
private void OnDestroy()
{
if (NetworkManager.Singleton != null)
{
NetworkManager.Singleton.Shutdown();
}
}
#endif
}