Object Visibility
What Is NetworkObject Visibility?
Object (NetworkObject) visibility is a Netcode for GameObjects term used to describe whether a NetworkObject
is visible to one or more clients as it pertains to a netcode/network perspective. When a NetworkObject
is visible to a client, the server will assure the client has a spawned version (a clone) of the NetworkObject
. This also means that all network traffic generated by the server for the visible NetworkObject
will be sent to all clients that are aware (that is, it's "visible to the clients") of it. Likewise, when a NetworkObject
is "hidden" (that is, not visible) from a client, then the client will despawn and destroy the NetworkObject
if it was previously visible and no network traffic generated by the hidden NetworkObject
will be received by the client(s) it's hidden from.
Using Visibility
One way to determine visibility is to assign a callback to NetworkObject.CheckObjectVisibility
. This callback is invoked when new clients connect or just before the associated NetworkObject
is spawned. Looking at the example below, we can see the callback includes a client identifier (clientId) value as a parameter which is used to determine whether the NetworkObject
is visible to the client. If NetworkObject.CheckObjectVisibility
isn't assigned, then Netcode for GameObjects assumes it's visible to all clients.
CheckObjectVisibility Callback Example
NetworkObject netObject = GetComponent<NetworkObject>();
netObject.CheckObjectVisibility = ((clientId) => {
// return true to show the object, return false to hide it
if (Vector3.Distance(NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject.transform.position, transform.position) < 5)
{
// Only show the object to players that are within 5 meters. Note that this has to be rechecked by your own code
// If you want it to update as the client and objects distance change.
// This callback is usually only called once per client
return true;
}
else
{
// Hide this NetworkObject
return false;
}
});
Additional Visibility Methods and Properties:
The CheckObjectVisibility
callback helps you determine if a NetworkObject
is visible to a specific client when the NetworkObject
is spawned. However, you might have the need to change a NetworkObject
's visibility after it's spawned. To change the visibility of a NetworkObject
that is already spawned, you can use the following methods:
Make a NetworkObject
visible to a single client:
NetworkObject netObject = GetComponent<NetworkObject>();
netObject.NetworkShow(clientIdToShowTo);
Make a NetworkObject
invisible/hidden from a single client:
NetworkObject netObject = GetComponent<NetworkObject>();
netObject.NetworkHide(clientIdToHideFrom);
Make several NetworkObject
s visible to a single client (static method):
/// networkObjects is of type List<NetworkObject>
NetworkObject.NetworkShow(networkObjects, clientId);
Make several NetworkObject
s invisible/hidden to a single client (static method):
/// networkObjects is of type List<NetworkObject>
NetworkObject.NetworkHide(networkObjects, clientId);
Spawn a NetworkObject with no observers (i.e. not visible to any clients initially):
NetworkObject.SpawnWithObservers = false;
NetworkObject.Spawn();
See Spawning With (or Without) Observers for more information.