40 lines
994 B
C#
40 lines
994 B
C#
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
public class AutoHost : MonoBehaviour
|
|
{
|
|
[Header("Auto Host Settings")]
|
|
[SerializeField] private bool enableAutoHost = true;
|
|
[SerializeField] private bool onlyInEditor = true;
|
|
|
|
private void Start()
|
|
{
|
|
if (ShouldAutoStart())
|
|
{
|
|
if (NetworkManager.Singleton != null)
|
|
{
|
|
if (!NetworkManager.Singleton.IsServer && !NetworkManager.Singleton.IsClient)
|
|
{
|
|
NetworkManager.Singleton.StartHost();
|
|
Debug.Log("<color=yellow><b>[AutoHost]</b> Auto-host started</color>");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("[AutoHost] NetworkManager not found!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool ShouldAutoStart()
|
|
{
|
|
if (!enableAutoHost)
|
|
return false;
|
|
|
|
#if UNITY_EDITOR
|
|
return true;
|
|
#else
|
|
return !onlyInEditor;
|
|
#endif
|
|
}
|
|
} |