Network Identityโ๏ธ
The NetworkIdentity
component is at the heart of the Omni networking high-level API. It controls a game object's unique identity on the network, and it uses that identity to make the networking system aware of the game object.
The NetworkIdentity
component is essential for network-aware GameObjects in Omni. It:
- Assigns unique identifiers to objects
- Enables network synchronization
- Manages object ownership
- Handles object spawning/despawning
NetworkIdentity Nesting Rules
Hierarchy Rules
- Only parent objects can have
NetworkIdentity
- Child objects cannot have
NetworkIdentity
- Children access parent's
NetworkIdentity
viaIdentity
property
Example Structure
Text Only | |
---|---|
Code Example
C# | |
---|---|
Common Error
If you see: "Multiple NetworkIdentity components in hierarchy", check for duplicate components in child objects.
๐ NetworkIdentity Properties
Properties:
Property | Type | Description |
---|---|---|
IdentityId |
int |
Unique identifier used for network synchronization and object tracking |
IsServer |
bool |
Indicates if this instance is running on the server side |
IsClient |
bool |
Indicates if this instance is running on a client machine |
IsLocalPlayer |
bool |
Indicates if this object represents the local player on the local machine |
IsServerOwner |
bool |
Indicates if the server has authority over this objec |
LocalPlayer |
NetworkIdentity |
Reference to the local player's NetworkIdentity component |
Owner |
NetworkPeer |
Reference to the client that has authority over this object |
IsRegistered |
bool |
Indicates if this object is registered with the network system |
LocalPlayer Assignment Rules
Overview
The NetworkIdentity.LocalPlayer
property:
- Only available on client-side
- Requires specific naming conventions
- Auto-assigns based on prefab name or tag
Valid Naming Patterns
Prefab name must include "Player":
Valid Tag Patterns
GameObject tag must include "Player":
Troubleshooting
If LocalPlayer
is null, check:
- Prefab name contains "Player"
- GameObject tag contains "Player"
- Script is running on client-side
Network Behaviourโ๏ธ
The NetworkBehaviour
component is a specialized MonoBehaviour that provides network-aware functionality to GameObjects. It extends the base MonoBehaviour class with additional networking features, such as:
- Remote procedure calls (RPCs)
- Network variable management
- Network event handling
Events and Callbacksโ๏ธ
NetworkBehaviour
provides a range of events and callbacks to handle network-related actions. These include:
-
OnAwake
Called when the object is created on the network, like
Awake()
for networked objects.Example
Override Considerations
Base Method Calls
- Always call the base method when overriding unity events to ensure proper
- When overriding
Awake()
, make sure to callbase.Awake()
Best Practice
- Prefer using
OnAwake()
overAwake()
for network-aware initialization OnAwake()
guarantees proper network setup order and not require manual base method calls
-
OnStart
Called when the object is initialized on the network, like
Start()
for networked objects.Example
Override Considerations
Base Method Calls
- Always call the base method when overriding unity events to ensure proper execution
- When overriding
Start()
, make sure to callbase.Start()
Best Practice
- Prefer using
OnStart()
overStart()
for network-aware initialization OnStart()
guarantees proper network setup order and not require manual base method calls
-
OnStartLocalPlayer
Called when the object is initialized as the local player on the network.
-
OnStartRemotePlayer
Called when the object is initialized as a remote player on the network.
Best Practices
- Use
OnAwake
for network variable initialization - Use
OnStart
for component references and general setup - Use
OnStartLocalPlayer
for local player-specific setup - Use
OnStartRemotePlayer
for remote player visualization
Always check IsServer
or IsClient
when needed to ensure code runs in the correct context.
-
OnServerClientSpawned
Called on the server when a client-side object is fully spawned and registered. This method ensures that the client object is ready and fully initialized on the network before the server performs any post-spawn operations.
The
peer
parameter represents the client that instantiated this NetworkIdentity, providing access to client-specific information like connection ID and status.Example
Usage Tip: Leveraging OnServerClientSpawned for Client Initialization
When you need to execute server-side logic that depends on the client being completely initialized, overriding the
OnServerClientSpawned
method is ideal. This approach can be used to:- Synchronize Data: Send initial game state or identity-specific data to the client that instantiates it's identity.
- Log Client Activity: Confirm the client has been successfully registered and log the event.
- Initialize Resources: Set up server-side components or trigger game mechanics that rely on the clientโs active participation that instantiates your identity.
-
OnTick
Called on each network update tick, useful for synchronized updates.
Example
Warning
It is necessary to enable the module in the Network Manager object in your scene for this code to work properly.
-
OnNetworkDestroy
Called when the object is unregistered from the network and destroyed.
-
OnRequestedAction
The
OnRequestedAction
method is a server-side callback designed to handle remote actions initiated by client-side entities. When a client requests an operationโsuch as activating an item, modifying configuration settings, or triggering an eventโthis method is invoked on the server to perform the corresponding action, identically to a remote procedure call (RPC), but embedded within theNetworkBehaviour
class.Example
C# Usage Tip: Handling Client-Triggered Actions
After a client has spawned and is fully initialized, it can call a method like
RequestAction()
to send a remote request to the server. Common use cases include:- Item Interactions: Activate or deactivate in-game items.
- Configuration Updates: Change settings or preferences stored on the server.
- Event Triggers: Initiate specific game events or mechanics.
- State Synchronization: Update or synchronize the clientโs state with the server.
-
Scene-Related Events
A set of events for handling scene loading and unloading:
Example
Event Execution Order
The typical execution order of network events is:
OnAwake
OnStart
OnStartLocalPlayer
orOnStartRemotePlayer
OnServerClientSpawned
(server-side)OnTick
(repeated)- Scene events (as needed)
OnNetworkDestroy