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