feat: Prevent Nested Network Managers (see #2867)

This commit is contained in:
vis2k 2021-08-13 11:44:09 +08:00
parent 8ca5203262
commit 966af93e50

View File

@ -141,6 +141,20 @@ public class NetworkManager : MonoBehaviour
// virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
public virtual void OnValidate()
{
// make sure someone doesn't accidentally add another NetworkManager
// need transform.root because when adding to a child, the parent's
// OnValidate isn't called.
foreach (NetworkManager manager in transform.root.GetComponentsInChildren<NetworkManager>())
{
if (manager != this)
{
Debug.LogError($"{name} detected another component of type {typeof(NetworkManager)} in its hierarchy on {manager.name}. There can only be one, please remove one of them.");
// return early so that transport component isn't auto-added
// to the duplicate NetworkManager.
return;
}
}
// add transport if there is none yet. makes upgrading easier.
if (transport == null)
{