fix(InterestManagement): calling OnEnable multiple times doesn't show an error anymore

This commit is contained in:
mischa 2024-01-06 11:57:13 +01:00
parent be6c6c7c64
commit 0cc4726728

View File

@ -9,24 +9,17 @@ namespace Mirror
[HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")] [HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
public abstract class InterestManagementBase : MonoBehaviour public abstract class InterestManagementBase : MonoBehaviour
{ {
// Configures InterestManagementBase in NetworkServer/Client // initialize NetworkServer/Client .aoi.
// Do NOT check for active server or client here. // previously we did this in Awake(), but that's called for disabled
// OnEnable must always set the static aoi references. // components too. if we do it OnEnable(), then it's not set for
// make sure to call base.OnEnable when overwriting! // disabled components.
// Previously used Awake()
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
if (NetworkServer.aoi == null) // do not check if == null or error if already set.
{ // users may enabled/disable components randomly,
NetworkServer.aoi = this; // causing this to be called multiple times.
} NetworkServer.aoi = this;
else Debug.LogError($"Only one InterestManagement component allowed. {NetworkServer.aoi.GetType()} has been set up already."); NetworkClient.aoi = this;
if (NetworkClient.aoi == null)
{
NetworkClient.aoi = this;
}
else Debug.LogError($"Only one InterestManagement component allowed. {NetworkClient.aoi.GetType()} has been set up already.");
} }
[ServerCallback] [ServerCallback]