[ClientRpc]
[ClientRpc]
attribute is used when the server calls a function on all clients. It is used to notify all connected clients about changes in the server's state.
Key Features
- Can only be called from the server to all clients.
- Executed on the client.
- The function name must begin with the
Rpc
prefix. - Executes on all connected clients.
Use Examples
Basic Use
public class GameManager : NetworkBehaviour
{
[Server]
private void StartGame()
{
// Game start logic
RpcNotifyGameStart();
}
[ClientRpc]
private void RpcNotifyGameStart()
{
// Client-side logic executed when game starts
// UI updates, sound playback, etc.
}
}
Passing Parameters
public class PlayerStatus : NetworkBehaviour
{
[Server]
public void TakeDamage(int damage)
{
// Damage handling logic
RpcUpdateHealth(currentHealth);
}
[ClientRpc]
private void RpcUpdateHealth(int newHealth)
{
// Update health status on the client side
// UI updates, visual effects, etc.
}
}
Synchronizing Multiple Information
public class WorldState : NetworkBehaviour
{
[Server]
private void UpdateWeather(WeatherType weather, float intensity)
{
// Weather change logic
RpcSyncWeather(weather, intensity);
}
[ClientRpc]
private void RpcSyncWeather(WeatherType weather, float intensity)
{
// Update weather on the client side
// Particle effects, sounds, etc.
}
}
note
- The [ClientRpc] attribute is used to notify all clients about changes on the server.
- The function name must begin with
Rpc
to ensure proper recognition as a remote procedure call. - It is typically used to communicate game state changes, event notifications, or environmental updates to all connected clients. :::