[Client]
The '[Client]' attribute designates that the method can only be executed on the client.
Key Features
- Executes only on the client.
- A warning occurs if called from the server.
- Can be used in both Unity's game loop methods (such as Start, Update, etc.) and regular methods.
- When used in overridden methods, the attribute must also be specified in the overridden method.
Use Examples
Basic Use
public class PlayerController : NetworkBehaviour
{
[Client]
void Update()
{
// Logic that runs only on the client
// Example: Player input handling
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
[Client]
private void Jump()
{
// Jump logic
// Actual movement is requested on the server via Command
}
}
Using in Inherited Methods
public abstract class BasePlayer : NetworkBehaviour
{
[Client]
protected virtual void HandleInput()
{
// Basic input handling logic
}
}
public class SpecificPlayer : BasePlayer
{
[Client]
protected override void HandleInput()
{
base.HandleInput();
// Additional input handling logic
}
}
Using with State Checks
public class PlayerUI : NetworkBehaviour
{
[Client]
private void UpdateUI()
{
// Client check
if (!isClient)
{
Debug.LogWarning("UI updates can only be done on the client");
return;
}
// UI update logic
UpdateHealthBar();
UpdateAmmoDisplay();
}
private void UpdateHealthBar()
{
// Health bar update logic
}
private void UpdateAmmoDisplay()
{
// Ammo display update logic
}
}
note
- Methods with the
[Client]
attribute should contain logic that needs to be executed only on the client. - Any actual game state changes should be requested on the server via
[Command]
. - Typical use cases include UI updates, input handling, visual/sound effects, etc. :::