Unity5.1 new network engine Unet (iv) unet Remote Actions

Source: Internet
Author: User

Sun Guangdong 2015.7.12


Network systems have the means to perform actions in the network. These types of actions are sometimes called remote Procedure calls (remotes Procedure Calls).

There are two types of Rpc in a network system:

1. Commands command-called from client and executed on server.

2, CLIENTRPC calls-and execute on server and client.



Shows the direction of the remote operation:


Commands:


The command is a player object that is sent from the Clientplayer object to the server.

For security, commands can only be sent from the your player object. So you can't control the objects of other players. To make a function a command. Add the [Command] own definition attribute to it and add the "CMD" prefix. This function is now executed on the server when it is called on the client. No matter what the parameters are, they will be delivered to the server command.


The Commands function command function must have the prefix "CMD". This is a hint that this function is special when reading code calls the command. is not called locally as a normal function.

Class player:networkbehaviour{public    gameobject Bulletprefab;    [Command]    void Cmddofire (float lifeTime)    {        Gameobject bullet = (gameobject) Instantiate (            Bulletprefab,             Transform.position + transform.right,            quaternion.identity);                    var bullet2d = bullet. Getcomponent<rigidbody2d> ();        bullet2d.velocity = Transform.right * bulletspeed;        Destroy (bullet, lifeTime);        Networkserver.spawn (bullet);    }    void Update ()    {        if (!islocalplayer)            return;        if (Input.getkeydown (keycode.space))        {            cmddofire ();}}    }


Be careful, it sends every frame from the client to the command! This can result in a large amount of network traffic.


By default. command is zero 0-sent by default on a reliable channel. So. By default, all commands are sent reliably to the server. This enables you to customize the [Command] of the "Channel" parameter to define its own properties. This parameter should be an integer representing the number of channels.



Channel 1 is set to a channel that is unreliable by default, so use this. The value of the parameter using the command attribute is 1. : Like this

[Command (Channel=1)]



CLIENTRPC Calls:


The CLIENTRPC call is to send an object from the server to the object on the client. They are able to send from whatever server object. A networkidentity that has been produced.

Because the server has authority authorization. Then there's no security problem. The server object is able to send these calls. To make Clientrpc call a function, add the [CLIENTRPC] own definition attribute to the function. and add the "Rpc" prefix. This function executes on the client when it is called on the server.

Regardless of what participates in the number of their own initiative to pass to CLIENTRPC call client.

The CLIENTRPC function must have the prefix "Rpc". This is a hint. Read the code when the method is called. This function is special and is not called locally as normal functionality.

Class player:networkbehaviour{    [Syncvar]    int health;    [Clientrpc]    void rpcdamage (int amount)    {        Debug.Log ("Took Damage:" + amount);    }    public void takedamage (int amount)    {        if (!isserver)            return;        Health-= amount;        Rpcdamage (amount);    }}


When executed as a localclient host game, CLIENTRPC will be called by Localclient-. Even if it is in the same server process. So the behavior of localclients and Remoteclients is the same. For CLIENTRPC calls.





??

Unity5.1 new network engine Unet (iv) unet Remote Actions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.