Remote Events are created in the Unity Editor Inspector by selecting the Add Remote Event button.
You can have up to 250 remote events in one Remote Event Agent.
Maximum message size of a remote event is 4KB.
Name | Function |
Event Name | Unique name of the Event. The Event name is used to find and trigger event across different network. Event name must be unique in the same Remote Event Agent Component. |
Target | Controls the recipients of the remote event. All: All the players in the room. Exclude self: All the players in the room except the sender. Host only: Room host only. |
Remote Events are invoked by calling SWNetwork API in C# scripts.
Invoke a Remote event
public void Invoke(string eventName)
Invoke a Remote event with the specified message
public void Invoke(string eventName, SWNetworkMessage msg)
The eventName string parameter pass to the Invoke method has to match with Event Name value in the Remote Event Agent Inspector.
This example uses SWNetworkMessage to send the bullet launch position(vector3) and aim direction (vector3) along with the remote event.
using SWNetwork;using SWNetwork.RPC;​public class PlayerSkill : MonoBehaviour {​public Gun gun;public Transform launchPosition;RemoteEventAgent remoteEventAgent;​void Start () {remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();}​void Update () {​if (Input.GetMouseButtonDown(0)){fireBullet();}}​void fireBullet(){SWNetworkMessage msg = new SWNetworkMessage();msg.Push(launchPosition.position);msg.Push(transform.forward);remoteEventAgent.Invoke("fire", msg);}}​
Handling remote event is done by adding listener to the remote event.
void Start () {remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();remoteEventAgent.AddListener("fire", RemoteFire);}​void OnDestroy(){remoteEventAgent.RemoveListener("fire", RemoteFire);}
In this example, we used the PopVector3() method on the SWNetworkMessage objet to obtain the launch position and the aim direction of the fire remote event.
public void RemoteFire(SWNetworkMessage msg){Vector3 position = msg.PopVector3();Vector3 direction = msg.PopVector3();gun.fireBullet(position, direction);}