Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

SyncEventAttribute

class in UnityEngine.Networking

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

This is an attribute that can be put on events in NetworkBehaviour classes to allow them to be invoked on client when the event is called on the sserver.

[SyncEvent] events are called by user code on UNET servers, and then invoked on corresponding client objects on clients connected to the server. The arguments to the Event call are seriialized across the network, so that the client event is invoked with the same values as the function on the server. These events must begin with the prefix "Event".

no example available in JavaScript
using UnityEngine;
using UnityEngine.Networking;

public class DamageClass : NetworkBehaviour { public delegate void TakeDamageDelegate( int amount, float dir );

[SyncEvent] public event TakeDamageDelegate EventTakeDamage; [Command] public void CmdDoMe( int val ) { EventTakeDamage( val, 1.0f ); } }

public class Other : NetworkBehaviour { public DamageClass damager; int health = 100; void Start( ) { if( NetworkClient.active ) damager.EventTakeDamage += TakeDamage; } public void TakeDamage( int amount, float dir ) { health -= amount; } }

SyncEvents allow networked actions to be propagated to other scripts attached to the object. In the example above, the Other class registers for the TakeDamage event on the DamageClass. When the event happens on the DamageClass on the server, the TakeDamage() method will be invoked on the Other class on the client object. This allows modular network aware systems to be created, that can be extended by new scripts that respond to the events generated by them.

Variables

channelThe UNET QoS channel that this event should be sent on.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961