fix: #2793 - Interest Management V2 now offers SetHostVisibility overwriting (#2813)

* InterestManagement.SetHostVisibility virtual

* NetworkServer uses it

* NetworkClient uses it
This commit is contained in:
vis2k 2021-07-08 13:36:27 +08:00 committed by GitHub
parent 372976eaab
commit fb052a30b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 20 deletions

View File

@ -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<Renderer>())
rend.enabled = visible;
}
}
}

View File

@ -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);
}
}

View File

@ -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<Renderer>())
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

View File

@ -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);
}
}
}