네트워크 멀티플레이 대응
This commit is contained in:
52
Assets/Scripts/SmartAutoHost.cs
Normal file
52
Assets/Scripts/SmartAutoHost.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user