53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
public class SmartAutoHost : MonoBehaviour
|
|
{
|
|
#if UNITY_EDITOR
|
|
private void Start()
|
|
{
|
|
if (NetworkManager.Singleton == null)
|
|
{
|
|
Debug.LogError("[SmartAutoHost] NetworkManager not found!");
|
|
return;
|
|
}
|
|
|
|
if (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsClient)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool isMainEditor = IsMainEditor();
|
|
|
|
if (isMainEditor)
|
|
{
|
|
NetworkManager.Singleton.StartHost();
|
|
Debug.Log("<color=yellow><b>[SmartAutoHost]</b> MAIN EDITOR → Starting as HOST</color>");
|
|
}
|
|
else
|
|
{
|
|
NetworkManager.Singleton.StartClient();
|
|
Debug.Log("<color=blue><b>[SmartAutoHost]</b> SECONDARY EDITOR → Connecting as CLIENT</color>");
|
|
}
|
|
}
|
|
|
|
private bool IsMainEditor()
|
|
{
|
|
string[] args = System.Environment.GetCommandLineArgs();
|
|
return System.Array.Exists(args, arg => arg == "-mainEditor");
|
|
}
|
|
#else
|
|
private void Start()
|
|
{
|
|
// 빌드된 버전은 항상 클라이언트
|
|
if (NetworkManager.Singleton != null &&
|
|
!NetworkManager.Singleton.IsServer &&
|
|
!NetworkManager.Singleton.IsClient)
|
|
{
|
|
NetworkManager.Singleton.StartClient();
|
|
Debug.Log("<color=blue>[SmartAutoHost] Build → Connecting as CLIENT</color>");
|
|
}
|
|
}
|
|
#endif
|
|
}
|