From b2ce3421e50dc6082d207febc703a60c7798d976 Mon Sep 17 00:00:00 2001 From: Justin Nolan Date: Thu, 11 Nov 2021 14:47:33 +0800 Subject: [PATCH] fix: networkmanager undo and move some validation into Reset() (#2990) * Fix network manager undo and move some validation code into Reset() * Add comments --- Assets/Mirror/Runtime/NetworkManager.cs | 70 ++++++++++++++----------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index a76604bba..18ac046cc 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -133,37 +133,6 @@ 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()) - { - 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) - { - // was a transport added yet? if not, add one - transport = GetComponent(); - if (transport == null) - { - transport = gameObject.AddComponent(); - Debug.Log("NetworkManager: added default Transport because there was none yet."); - } -#if UNITY_EDITOR - // For some insane reason, this line fails when building unless wrapped in this define. Stupid but true. - // error CS0234: The type or namespace name 'Undo' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?) - UnityEditor.Undo.RecordObject(gameObject, "Added default Transport"); -#endif - } - // always >= 0 maxConnections = Mathf.Max(maxConnections, 0); @@ -181,6 +150,45 @@ public virtual void OnValidate() } } + // virtual so that inheriting classes' Reset() can call base.Reset() too + // Reset only gets called when the component is added or the user resets the component + // Thats why we validate these things that only need to be validated on adding the NetworkManager here + // If we would do it in OnValidate() then it would run this everytime a value changes + public virtual void Reset() + { + // make sure someone doesn't accidentally add another NetworkManager + // need transform.root because when adding to a child, the parent's + // Reset isn't called. + foreach (NetworkManager manager in transform.root.GetComponentsInChildren()) + { + 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) + { +#if UNITY_EDITOR + // RecordObject needs to be called before we make the change + UnityEditor.Undo.RecordObject(gameObject, "Added default Transport"); +#endif + + transport = GetComponent(); + + // was a transport added yet? if not, add one + if (transport == null) + { + transport = gameObject.AddComponent(); + Debug.Log("NetworkManager: added default Transport because there was none yet."); + } + } + } + // virtual so that inheriting classes' Awake() can call base.Awake() too public virtual void Awake() {