From fb052a30b5f2e3d4f58378d4d4309829e8b35f5d Mon Sep 17 00:00:00 2001 From: vis2k Date: Thu, 8 Jul 2021 13:36:27 +0800 Subject: [PATCH] fix: #2793 - Interest Management V2 now offers SetHostVisibility overwriting (#2813) * InterestManagement.SetHostVisibility virtual * NetworkServer uses it * NetworkClient uses it --- Assets/Mirror/Runtime/InterestManagement.cs | 22 ++++++++++++++++++++- Assets/Mirror/Runtime/NetworkClient.cs | 22 +++++++++++++++++++-- Assets/Mirror/Runtime/NetworkIdentity.cs | 15 -------------- Assets/Mirror/Runtime/NetworkServer.cs | 4 ++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Assets/Mirror/Runtime/InterestManagement.cs b/Assets/Mirror/Runtime/InterestManagement.cs index a2d36aa43..2c79f4d25 100644 --- a/Assets/Mirror/Runtime/InterestManagement.cs +++ b/Assets/Mirror/Runtime/InterestManagement.cs @@ -8,7 +8,7 @@ namespace Mirror [DisallowMultipleComponent] public abstract class InterestManagement : MonoBehaviour { - // Awake configures InterestManagement in NetworkServer + // Awake configures InterestManagement in NetworkServer/Client void Awake() { if (NetworkServer.aoi == null) @@ -16,6 +16,12 @@ void Awake() 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."); } // Callback used by the visibility system to determine if an observer @@ -57,5 +63,19 @@ protected void RebuildAll() NetworkServer.RebuildObservers(identity, false); } } + + // Callback used by the visibility system for objects on a host. + // Objects on a host (with a local client) cannot be disabled or + // destroyed when they are not visible to the local client. So this + // function is called to allow custom code to hide these objects. A + // typical implementation will disable renderer components on the + // object. This is only called on local clients on a host. + // => need the function in here and virtual so people can overwrite! + // => not everyone wants to hide renderers! + public virtual void SetHostVisibility(NetworkIdentity identity, bool visible) + { + foreach (Renderer rend in identity.GetComponentsInChildren()) + rend.enabled = visible; + } } } diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e14c8a732..10b3dfdd1 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -91,6 +91,10 @@ public static class NetworkClient static Unbatcher unbatcher = new Unbatcher(); + // interest management component (optional) + // only needed for SetHostVisibility + public static InterestManagement aoi; + // scene loading public static bool isLoadingScene; @@ -1170,7 +1174,13 @@ static void OnHostClientObjectHide(ObjectHideMessage message) if (NetworkIdentity.spawned.TryGetValue(message.netId, out NetworkIdentity localObject) && localObject != null) { - localObject.OnSetHostVisibility(false); + // obsolete legacy system support (for now) +#pragma warning disable 618 + if (localObject.visibility != null) + localObject.visibility.OnSetHostVisibility(false); +#pragma warning restore 618 + else if (aoi != null) + aoi.SetHostVisibility(localObject, false); } } @@ -1184,7 +1194,15 @@ internal static void OnHostClientSpawn(SpawnMessage message) localObject.hasAuthority = message.isOwner; localObject.NotifyAuthority(); localObject.OnStartClient(); - localObject.OnSetHostVisibility(true); + + // obsolete legacy system support (for now) +#pragma warning disable 618 + if (localObject.visibility != null) + localObject.visibility.OnSetHostVisibility(true); +#pragma warning restore 618 + else if (aoi != null) + aoi.SetHostVisibility(localObject, true); + CheckForLocalPlayer(localObject); } } diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index 763a8ad34..0ef344730 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -830,21 +830,6 @@ internal void OnStopAuthority() [Obsolete("Use NetworkServer.RebuildObservers(identity, initialize) instead.")] public void RebuildObservers(bool initialize) => NetworkServer.RebuildObservers(this, initialize); - // Callback used by the visibility system for objects on a host. - // Objects on a host (with a local client) cannot be disabled or - // destroyed when they are not visible to the local client. So this - // function is called to allow custom code to hide these objects. A - // typical implementation will disable renderer components on the - // object. This is only called on local clients on a host. - // => this used to be in proximitychecker, but since day one everyone - // used the same function without any modifications. so let's keep it - // directly in NetworkIdentity. - internal void OnSetHostVisibility(bool visible) - { - foreach (Renderer rend in GetComponentsInChildren()) - rend.enabled = visible; - } - // vis2k: readstring bug prevention: https://github.com/vis2k/Mirror/issues/2617 // -> OnSerialize writes length,componentData,length,componentData,... // -> OnDeserialize carefully extracts each data, then deserializes each component with separate readers diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 235b09ff7..dc284201c 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -1374,8 +1374,8 @@ static void RebuildObserversCustom(NetworkIdentity identity, bool initialize) if (identity.visibility != null) identity.visibility.OnSetHostVisibility(false); #pragma warning restore 618 - else - identity.OnSetHostVisibility(false); + else if (aoi != null) + aoi.SetHostVisibility(identity, false); } } }