Some time ago, the study of Unet, after the project practice, the general collation of the problems encountered.
SOURCE BitBucket: It is necessary to note that this project contains only the upper layer of the package, some low-level network implementations within unity, such as the Networktransport class is not included.
Introduction to Unet Concept
Spawn: Simply put, gameobject on the server, according to the networkidentity components above to find the corresponding monitoring connection, in the monitoring connection to generate the corresponding gameobject.
Command: The client calls, the server executes, so that the parameters that the client invokes must be unet serializable, so that the server executes the parameters before deserializing them. It is important to note that the Networkidentity component that the client needs to have permission to invoke command commands.
CLIENTRPC: Server-side invocation, client execution, IBID., server-side parameter serialization to client execution, in general, the server will find the above networkidentity components, to determine which clients are monitoring this networkidentity, The RPC command is sent to all monitoring clients.
Server/servercallback: Run only on the server side, callback is Unity intrinsics.
Client/clientcallback: Ditto, run only on the client, callback is the unity intrinsic function.
Syncvar: The value of the server can be automatically synchronized to the client, keeping the client's value as the server. Changing the client value does not affect the server's value.
Most of the above features will be converted to the corresponding Msgtype, where the server calls, the client executes the corresponding msgtype as Spawn,clientrpc,syncvar corresponding Msgtype respectively Objectspawn,rpc,updatevars , these are networkserver calls, and the client gets the corresponding message and executes the appropriate method. The client calls, the server executes the Msgtype as command, the client sends, and the server detects the corresponding message and executes.
Unet Main class Introduction
Networkidentity components: The most basic components of the network object, the client and the server to confirm whether it is an object (NetID), also used to indicate the state, such as whether it is a server, whether it is a client, whether it has permissions, whether it is a local player, etc.
A simple example, A is the host (again the server, and the client), B is a client,a and B respectively have a player playa and Playb. On machine A, Playa and Playb Isserver are true,isclent true, Where Playa has permissions, is a local player, B does not have permission, is not a local player. On Machine B, Playa and Playb Isserver are false,isclent true, where Playb has permission, is local player, a does not have permission, and is not a local player. A is the same as the NetID on Playa B, and the NetID of Playb on A and B are the same, where netid is used to indicate that they are the same network object on different machines.
In the following, a network object is used to represent the gameobject with the networkidentity component.
Networkconnection: Defines a client-to-server connection that contains current clients that monitor network objects on those servers and encapsulates messages that are sent and received to the server.
Networkclient: A static object that holds the current Networkconnection object with all Networkclient lists, and handles some default client messages.
A watcher on a network object is one or more networkconnection that indicates that one or more clients are keeping a watch on the network object, and when the network object is updated on the server, it automatically updates the corresponding network object for all monitors.
Networkscene: In short, 1Server and the client need to maintain a list of network objects, the server can traverse all the network objects to send messages, and maintain the server and the client network objects in sync, And the client records the list of prefab that need to be registered. Networkserver and Clientscene contain a Networkscene object that references a list of network objects.
Networkserver: Mainly hold a networkscene and do something that only on the server can do to the network service, such as Spawn, destory and so on. and maintain all client connections.
Clientscene: Mainly holds a static Networkscene object, which is used to register the prefab list of network objects, as well as the list of network objects already on the client scene, processing syncvar,rpc,syncevent features, etc. There are also objectspawn,objectdestroy,objecthide news and so on.
Unet time to think of the problem with the source answer question 1 spawn what happened, the client why to register the corresponding prefab.
1 Network Objects call Onstartserver, assign NetID. and register to the Networkscene network object list on the appropriate server, update information such as Isserver to true.
2 when the server spawn a network object, look for all client connections to see if each client connection needs to monitor this network object, if True, Then give this client a message msgtype.objectspawn or msgtype.objectspawnscene (which is usually called automatically after a service scenario transformation) and pass the above NetID.
3 When the client receives the OBJECTSPAWN message, it will look in the registered prefab, find the instantiate network object, and when the Objectspawnscene is received, it will find the network object in the scene. They are then registered to the Networkscene network object list in Clientscene, and the update NetID is the same as the server. Update information such as Isclent is true.
When we manually spawn an object, call the Objectspawn message, the client receives this message processing to get a assetid, we want to base on the Prefabe instance a new object, Only the client has registered the appropriate Prefabe information to find Prefabe based on the corresponding AssetID.
Question 2 Networkidentity's netid represents what, at that time assigned.
When the server and the client NetID the same, indicating that they are the same object, the corresponding marked as Syncvar, the server changed, corresponding to the client on the same NetID network objects, update the data on the server, Rpc,command is also the same netid call.
Allocations generally occur when the server spawn a network object, and the network object calls Onstartserver when it generates NETID.
When the client accepts the corresponding Objectspawn message, it passes the netid of the corresponding object on the server, creating a new network object and assigning the NetID.
Question 3 What is the Sceneid of networkidentity, the object in the scene that already has the Networkidentity component connected to the server in the client.
When the network object is not spawn generated in the server with the client, but in the server and the client scene itself sometimes, we also need to establish a connection between the server and the client, this object will have a Sceneid to mark, this model is usually after the server scene transformation is completed, Networkserver Call Spawnobjects synchronizes this network object with all clients and generates a netid when it is generated.
Issue 4 after the server scene switch, how the objects of each networkidentity component contact the client.
The following order is not determined because there are asynchronous operations, and the following order is only a general possible order.
1. The server asynchronously invokes the scene and sends it to all clients to start switching scenes. Msgtype.scene
2. The client accepts Msgtype.scene and starts switching scenes.
3. When the server scene is complete, it finds all the network objects and then spawn the network objects so that each network object is connected by the same netid.
4. After the client scenario is complete, call Onclientconnect again, generally, without doing anything.
Question 5 Why does the client have permission to the network object, and what does it have permission to do?
In general, when spawn a server on a network object, the server has its permissions, the client can not change the network object, or change the corresponding properties of the network object can not be synchronized to the server and other clients, but the local to see the change.
So if I need to be able to change the state of this network object and be able to synchronize it to all the other clients, we need to have the permission of this network object, because this can send command command on this machine to tell the server I changed the state, The server can also tell all clients that the network object has changed state.
When local player is created, the current client has permissions on the local player. The Syncvar on a network object with permissions on the client will not be able to synchronize to the server, and the server does not register the Updatevars message at all, or it requires the client to invoke command commands on its own.
Problem 6 Unet Common Package State synchronous processing has those, where networktransform and networkanimator respectively how to communicate, if the client is the right network objects and how to communicate.
There are two ways to unet the common state of package state synchronization.
One is to send messages via CLIENTRPC and command is encapsulated. The client is called with the service side and then serializes the corresponding parameters, which are then executed to the server with the client's deserialization parameters.
The second is the network built-in serialization and deserialization, the state of the serialized server, and then the client deserializes the corresponding values, such as Syncvar through the corresponding onserialize,ondeserialize. This can only synchronize the server to the client.
These two kinds of essence are the client and the server sends each other MSGTYPE message, corresponding server and the client registers the corresponding message processing. The animation on the Networkanimator server changes, sending a message informing all clients that the corresponding state has changed, such as RPC. The Networktransform server serializes the corresponding value by Onserialize, and the client deserializes the corresponding value.
If the client has permission to correspond to Networktransform and Networkanimator network objects. Networkanimator the corresponding client submits the state to the server and then distributes it to all clients, equivalent to invoking the command and calling the RPC method in the command. Networktransform the corresponding client sends a message to the server, the server updates the corresponding location, direction. It is then deserialized to all clients.
So if the client has authorization, then networkanimator and networktransform in the server or authorized client state changes can be updated to all clients, which note the Syncvar feature, even if the client authorization, after the client changes, and will not sync to other machines.
So if we design a similar network component ourselves, we need to consider the corresponding processing of the client authorization, that is to add a command.
Question 7 What is the difference between client-side licensing and local play?
Local play is also a client-side authorization, and the corresponding hasauthority and Islocalplayer are true, where Playercontrollerid is an integer.
The client authorizes only hasauthority to True,islocalplayer for False,playercontrollerid-1.
Question 8 unet How to achieve fog map
Through Networkproximitychecker, so that each frame detects the current network object monitoring connection, to determine those clients need this network object. Similarly, to achieve more complex can be implemented on their own similar.
Question 9 What did networkserver.destory do?
Must be a network object, and preferably in the server call, call, send all the monitoring connect, destroy the corresponding network objects, the last server destroyed. Take a look at the msgtype.objectdestory message flow.
It should be noted that in the server, destory a network object, will automatically call Networkserver.destory. The code is in Networkidentity.ondestory.
Issue 10 The server has done those things when adding roles.
When the client connects to the server, the role is automatically created when the role is set automatically.
1 server Add a player, set Playercontrollerid
2 Sets the current Conn's ready to true. Then detects whether the current Conn needs to monitor the various network objects on the Networkscene list of network objects on the server, where the isspawnfinished on the client indicates whether the list of Networkscene network objects is detected.
3 The server player's spawn down, set the corresponding network object record local permissions client for the current client, the corresponding Playercontrollerid sent to the client.
The question one by one networkclient and Networkserver's active represents what, those times with
When Networkserver starts listening, set active to true.
Networkclient is set to True after the server is connected.
When some messages are sent, or when called by RPC and command, the timing may throw an error before active.
Question 12 What the network update does.
1 server updates, processing some such as client links and lost links, and receiving messages and finding corresponding event handling, as well as serializing the server network objects to update the data.
2 client updates, such as the processing of servers on the server, are primarily the corresponding message processing.
3 Check that the server and client scenarios are loaded and completed.
Finally, imagine, in the network environment, we pull the bow and arrow, generate arrows, arrows on the client slowly pull, how should we do?
First, the bow and arrow to let all clients see, we want to build on the server, and then spawn distribution to the corresponding multiple clients, and then the current client also needs the current arrow permissions, so that the current user can control the arrow, and the current user control arrow generated new location synchronization to all clients.
Second, if you use Valve's labrender renderer, you need to shut down when you start the server, wait for the corresponding role to load, and then open the corresponding valvecamer through Localplayer, otherwise the Valvecamer on the server may not get the correct shadow map.
If there is an analysis of the wrong place, you are welcome to point out.
Unity5 Network Module Unet Introduction