fix: networkmanager undo and move some validation into Reset() (#2990)

* Fix network manager undo and move some validation code into Reset()

* Add comments
This commit is contained in:
Justin Nolan 2021-11-11 14:47:33 +08:00 committed by GitHub
parent d6d0ec88d0
commit b2ce3421e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -133,37 +133,6 @@ public class NetworkManager : MonoBehaviour
// virtual so that inheriting classes' OnValidate() can call base.OnValidate() too // virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
public virtual void OnValidate() 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)
{
// was a transport added yet? if not, add one
transport = GetComponent<Transport>();
if (transport == null)
{
transport = gameObject.AddComponent<KcpTransport>();
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 // always >= 0
maxConnections = Mathf.Max(maxConnections, 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<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)
{
#if UNITY_EDITOR
// RecordObject needs to be called before we make the change
UnityEditor.Undo.RecordObject(gameObject, "Added default Transport");
#endif
transport = GetComponent<Transport>();
// was a transport added yet? if not, add one
if (transport == null)
{
transport = gameObject.AddComponent<KcpTransport>();
Debug.Log("NetworkManager: added default Transport because there was none yet.");
}
}
}
// virtual so that inheriting classes' Awake() can call base.Awake() too // virtual so that inheriting classes' Awake() can call base.Awake() too
public virtual void Awake() public virtual void Awake()
{ {