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 VisibilityOne 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#
Additional Visibility Methods: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:
Make a NetworkObject
invisible/hidden from a single client:
Make several NetworkObject
s visible to a single client (static method):
Make several NetworkObject
s invisible/hidden to a single client (static method):