Unity's Network function

Source: Internet
Author: User
Tags network function unity network

The first thing to say is that Unity's own network function is not suitable for MMO-type games. If you want to use unity as a client of an MMO game, it is generally done in C # by establishing a custom network communication through the socket. Unity's own network function is designed for multiplayer games, which is generally a player to create a game (this player is both server and client), other players are connected. The number of players that are generally supported from bandwidth considerations is less than 64 (this is not necessarily the design of the game itself). These players are generally interconnected in a local area network, but if paired with Masterserver, they can also connect to each other on the Internet. Many so-called stand-alone and support multiplayer connected games are basically using this network mode. Unity's own network functions can be cross-platform, can run on pc,ios,android, and support client communication on different platforms. Network Protocol

Unity Network protocol itself is a relatively high-level network protocol, he is not independent of the existence, but with the engine of the game object together, for the game to provide network function support. To illustrate the unity of the network protocol, first of all to explain the game in the network mode of management. Network Game Object Management

In Unity network mode, the creation of a game object instance is accomplished through the network.instantiate () function. This function, like object.instantiate (), creates a game object instance locally and also creates the same instance of the game object on all interconnected hosts with the same Networkview ID. Each created game object uses Networkview.ismine to identify itself. If Ismine is true, it means that the game object was created by the native machine, and false indicates that it was created by another host and synchronized to the native. The Ismine identity of the gameobject has nothing to do with server and client identities at the bottom of the network, and there can be any identity game objects on both the server and the client, depending primarily on who created the game object. If you want to destroy the game object, you should call Network.destroy () instead of Object.destroy (), and he will destroy the object on all hosts. network protocol based on game object

In Unity there is no simple "sent to the server" or "sent to the client" network message, unity of the network protocol is based on a specific game object Communication protocol, which means that all communication contexts are related to a specific game object. Once the game object is created in the network environment, the communication relationship between the game objects created on each host is established, and the game object on any host can communicate with "own" on the other host. Communication between different game objects is independent, as if each game object has a separate communication pipeline.

Communication between game objects is divided into two forms: State synchronization

Game objects are often changed and can be described as data for object properties. The direction of state synchronization can only be ismine to non ismine. For example, a player role object HP attribute, if you need other players to see, you can use state synchronization to complete. This attribute is automatically updated to the same player role object on the other host when the HP change of the Ismine player role. State synchronization also supports guaranteed and non-guaranteed modes. RPC

Executes a remote function call on the same game object on another host. RPC is generally used to notify a one-time game event over the network, such as "object A is 30 damage" and so on. RPC's transport direction is very free, and you can send RPC calls to any other host, including yourself, on one host.

Network Game Object Management + state synchronization +rpc composed of Unity network protocol, all need to implement the network function of multiplayer games are built on this Unity network protocol on the basis of.
using Unity to build multiplayer games passing Game object references

In building multiplayer games, in addition to the need to pass some basic data type data (int, string ...) over the network, there are times when you need to pass a reference to a game object. For example, the game object A through RPC notification to other hosts he eliminated the game object B, where B is the game object reference. There are a lot of similar needs, so how do we pass the reference? It's going to take a networkview.viewid. Networkview.viewid is a networkviewid type of object that RPC itself supports the transmission of this data type. As long as the sender passes the networkview.viewid of B as an RPC parameter to the receiver, the receiver can then convert the Viewid to the corresponding game object reference by Networkview.find () locally. Global Objects

In a stand-alone game, the scene generally creates a game's global object to handle the overall game state. such as the completion of the level, the global event processing and so on. In multiplayer games, the same function is required. In general, this global object can be created on the server side as a network game object, and replicated to other client. This global object is typically used to handle the processing of new connections: When a client connects to the server, the global object is automatically created to that client. The client can use an RPC based on this global object as the first message sent to the server, typically with its own identity and game information. Server handles this RPC to implement game level player login. Synchronization scene Switching: When the game requires all players to switch the scene at the same time, the global object needs to be notified and synchronized. Game state Management and synchronization: Global game objects generally run game logic on the server side and notify all clients of game status and game events that need to be known to the client. Fusion Single Game

Multiplayer games generally have a single mode, and many of them play the same logic. In writing game logic code, in addition to the necessary network function code, should try to conceal the difference between single and multiplayer games, so that the code better understanding and maintenance. We can think of the single player as a multiplayer game with only one local client, and screen out the network function.

Use Networkviewwrapper instead of direct use of networkview, so that ismine in stand-alone mode is also effective, and shielding off the single mode of RPC function.
NetworkViewWrapper.cs[CSharp]  View plain copy print? using unityengine;   using system.collections;     /**  Packaging Networkview to enable it to be compatible with non-network mode . */   [Requirecomponent (typeof (Networkview))]   public  class networkviewwrapper : monobehaviour   {       public  int group       {            get { return networkView.group; }            set { networkView.group = value; }        }       public bool isMine         {            get             {            &Nbsp;   if (network.peertype == networkpeertype.disconnected)                     return true;                return networkView.isMine;             }        }        public Component observed         {           get { return  networkview.observed; }           set {  networkview.observed = value; }       }        public NetworkPlayer owner       {             get { return networkview.owner; }        }        public NetworkStateSynchronization stateSynchronization        {           get {  return networkview.statesynchronization; }            set { networkView.stateSynchronization = value; }        }       public NetworkViewID viewID        {           get { return  networkview.viewid; }           set {  networkview.viewid = value; }       }          public stAtic networkviewwrapper find (Networkviewid viewid)   

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.