Unity5.1 new network engine UNET (4) UNET Remote Actions, unity5.1unet

Source: Internet
Author: User

Unity5.1 new network engine UNET (4) UNET Remote Actions, unity5.1unet

Sun Guangdong


The network system has a method for performing operations in the network. These types of actions are sometimes called Remote Procedure CILS ). There are two types of Rpc in the network system:

1. Commands command-call from the client and run on the server.

2. ClientRpc CILS-and run on the server and client.

The Remote Operation direction is displayed:


Commands:


Command is the player Object sent from the player object on the client to the server. For security, commands can only be sent from the YOUR player Object, so you cannot control other player objects. To make a function a Command, add the [Command] custom feature to it and add the "Cmd" prefix. When it is called on the client, this function will now run on the server. Any parameter is automatically passed to the server command.


The Commands FUNCTION command must have the prefix "Cmd ". This is a suggestion. when reading the code to call this command, this function is special and 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 commands from the client for each frame! This results in a large amount of network traffic.


By default, the commands are all zero-sent over a reliable channel by default. Therefore, by default, all commands are reliably sent to the server. This allows you to customize the [Command] attributes of the "Channel" parameter. This parameter must be an integer indicating the number of channels.

Channel 1 is a Channel that is set by default and unreliable. Therefore, to use this Channel, use the value of the command attribute parameter to be 1:

[Command(channel=1)]



ClientRpc CILS:


ClientRpc calls are the objects sent from the server to the client. They can be sent from any server object, an existing NetworkIdentity. Because the server has authority authorization and there is no security issue, the server object can send these calls. To call a function as a ClientRpc, add the [ClientRpc] custom feature to the function and add the "Rpc" prefix. This function runs on the client when the server is upgraded. Any parameter will automatically pass the number to the ClientRpc call client.

The ClientRpc function must have the prefix "Rpc ". This is a hint. when reading the code to call this method, this function is special and not called locally like a normal function.

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 the game is running as a LocalClient host, ClientRpc will be called by LocalClient-even if it is in the same server process. Therefore, LocalClients and RemoteClients have the same behavior and are called for ClientRpc.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.