NetworkManager is a component that is used to manage the state of a network multiplayer game. It is actually completely implemented using HLAPI, so it does everything programmers can do with other means. However, NetworkManager encapsulates a lot of useful features into one place, making it as simple as possible to create, run, and debug multiplayer games.
NetworkManager can be used to make it completely non-scripted. In the editor's inspector, it allows you to configure all of its features. Networkmanagerhud provides a simple, default user interface that allows online games to be controlled by the user at run time. For more advanced usage, a developer can inherit from a class from NetworkManager and customize its behavior by overriding any virtual functions it provides.
The properties of the NetworkManager include the following:
Getting started with NetworkManager
NetworkManager can be used as a core control component in multiplayer online games. Start by creating an empty game object at the beginning of your scene, or choose an object that is easy to manage. Then add the NetworkManager component.
The editor's inspector allows you to configure and control a lot of things related to the network.
Networkmanagerhud is another component that needs to work with NetworkManager. It gives you a simple user interface that shows the game running control network status. It's good to start using a Web project, but not as the ultimate UI for a game.
Game State Management
Network multiplayer games can run in three modes-as a client, as a dedicated server, or as a "host" (both client and server). The network is designed to implement the same game code and assets in all of these situations. The game version developed for single player and multiplayer version should be the same.
NetworkManager provides a way to enter every mode.
Networkmanager.startclient ()
Networkmanager.startserver ()
Networkmanager.starthost ()
are available for scripting code, so they can enter the calling handler or custom user interface from the keyboard. The default run-time controls can also display calls to these functions. There are also buttons available for Networkmanagerhud inspectors in play mode, calling the same function.
Whatever method is used to change the game state, the properties networkaddress and Networkport are used. When a-server or host is started, Networkport becomes the listen port. When a client is started, networkaddress are the address to connect to, and Networkport are the port to connect to.
Regardless of the method used to change the game state, use the NetworkAddress and Networkport properties. When the server or host is started, Networkport becomes the listener port. When the client starts, the NetworkAddress address is connected, and the Networkport port is connected.
derived (network instantiation) Management
In addition to the player prefab, the prefabs of other objects that would be dynamically spawned must is registered with th E Clientscene. This can is done with the Clientscene.registerprefab () functions, or it can is done by the NetworkManager automatically. Adding prefabs to the Spawn list would make them be auto-registered. The Spawn configuration section of the NetworkManager inspector looks like:
NetworkManager can be used to manage the instantiation of network objects from a prefab. Most games have a pre-set as the protagonist object, so NetworkManager has a position to drag the player's preset body. When a player preset is set, the Player object will automatically instantiate the object in each user's game. This applies to local players on the host as well as remote players on the remote client. Note that you must add the Networkidentity component to the player's default body.
In addition to the player presets, other preset objects want to be dynamically generated and must be registered in the Clientscene. This can be done with the Clientscene.registerprefab () function, or by NetworkManager automatically. Adding the prefab to the list will allow them to be automatically registered.
Once the player preset is set, you should be able to see the player object in the start game console. Stop the game should see the player object is destroyed. Running another copy of the game and connecting to localhost as a client should let another player object appear, and stop, and the customer should be destroyed by the client's player object.
Player Object generation is the default method for calling NetworkManager: Onserveraddplayer. If you want to customize the way players create objects, you can override virtual functions. The default implementation is this:
Public Virtual void Short Playercontrollerid) { var player = (gameobject) gameobject.instantiate (Playerprefab, Playerspawnpos, quaternion.identity); Networkserver.addplayerforconnection (conn, player, Playercontrollerid); }
Note that the Networkserver.addplayerforconnection () function must be used to create a new player object, which establishes a contact with the client. This will generate the object, so the networkserver.spawn does not need to be called on the player object.
Start Position
Control where the player is generated, you can use the Networkstartposition component. NetworkManager looks for the Networkstartposition object in the scene, and if it finds any, it will generate the player in that position and apply one of them. Custom code can access the available networkstartpositions so that the list passes through Networkmanager.startpositions, and there is a helper function getstartposition () The NetworkManager can be used to implement Onserveraddplayer to find a starting position.
Scene Management
Most games have multiple scenes. Usually at least one title or Start menu scene in the screen is added to the actual game scene. NetworkManager set up the automatic management of scene state and scene switching mode function for a multiplayer game. NetworkManager has two slots, Offlinescene and Onlinescene. Drag the scene to the slot to activate the network scene management feature.
When the server or host is started, the onlinescene scene is loaded. This will become the current network scenario. Any client connected to the server will also load this scenario. The name of the scene is stored in the Networkscenename property.
When the network is stopped (the server or host is stopped) or disconnected by the client, the offline scene is loaded. This causes the game to automatically return to the menu scene when it disconnects.
We can also change the scene in the game by calling the method Networkmanager.serverchangescene (). This will make all current connections to the client's change scenario and will update networkscenename so that new customers will also be loaded into the new scene.
When network scene management is activated, any call to the Game state Management feature can result in a change in the scene, for example: Networkmanager.starthost () or networkmanager.stopclient (), which applies to the runtime control UI. It is easy to control the scene synchronization of multiplayer online games by setting the scene and calling these functions.
Note that scene changes cause objects in all scenes to be destroyed. This may include networkmanager! if you want to make it not destroyed during the scene switching process, make sure that the "Dont Destroy on Load" setting is set to true. In simple cases, this is the best configuration. However, it is possible to have different networkmanager in each scene to control incremental loading prefabrication, or different scene transitions.
Debugging InformationThe Networkmanagerhud Properties panel displays additional information about the state of the network at run time. This includes:
- Network connection
- Server-side: Networkidentity activated objects
- Client: Networkidentity Activated object
- Simultaneous online Client
Competition Arrangement (matchmaking)
The NetworkManager runtime UI and the NetworkManager inspector UI allow interaction with matchmaker server. The function Networkmanager.startmatchmaker () makes the connection and is saved in the Networkmanager.matchmaker property. Once activated, the default UI is changed and the callback function is executed to simply complete the matchmaking.
You can inherit the Networkmanger override virtual method to implement a custom callback.
User Customization (customization)
There is virtual functions on NetworkManager this derived classes can use to customize behaviour. When implementing these functions, being sure to take care of the functionality, the default implementations provide. For example, in Onserveraddplayer (), the function Networkserver.addplayer must is called to active the player object for T He connection.
Functions invoked on the Server/host:
After inheriting NetworkManager, users can rewrite many virtual methods as needed. When overriding these methods, it is important to pay attention to what the method originally implemented. For example, the Onserveraddplayer () function, you must call method Networkserver. Addplayer implements the connection of the player object.
The methods that are called on the server/host are:
//called when there is a client connection Public Virtual voidOnserverconnect (Networkconnection conn); //called when a client disconnects Public Virtual voidOnserverdisconnect (Networkconnection conn) {networkserver.destroyplayersforconnection (conn); } //called when there is a client preparation (ready) Public Virtual voidOnserverready (Networkconnection conn) {Networkserver.setclientready (conn); } //called when the object is generated to the client Public Virtual voidOnserveraddplayer (Networkconnection Conn, ShortPlayercontrollerid) { varPlayer =(Gameobject) gameobject.instantiate (Playerprefab, Playerspawnpos, quaternion.identity); Networkserver.addplayerforconnection (conn, player, Playercontrollerid); } //called when the player destroys from the client Public Virtual voidOnserverremoveplayer (Networkconnection Conn, ShortPlayercontrollerid) {Playercontroller player; if(Conn. Getplayer (Playercontrollerid, outplayer)) { if(Player.networkidentity! =NULL&& Player.NetworkIdentity.gameObject! =NULL) Networkserver.destroy (player.NetworkIdentity.gameObject); } } //called when an error occurs on the server Public Virtual voidOnservererror (Networkconnection Conn,intErrorCode);
Methods that are called on the client:
//called when connecting to the server Public Virtual voidOnclientconnect (Networkconnection conn) {Clientscene.ready (conn); Clientscene.addplayer (0); } //called when the server is disconnected Public Virtual voidOnclientdisconnect (Networkconnection conn) {stopclient (); } //called when a network problem occurs Public Virtual voidOnclienterror (Networkconnection Conn,intErrorCode); //call when the server is not ready. Public Virtual voidOnclientnotready (Networkconnection conn);
Matchmaker the method that will be called:
// when there is a match created Public Virtual void onmatchcreate (creatematchresponse matchinfo) // called when a match list is received Public Virtual void onmatchlist (listmatchresponse matchlist) // when the game is called Public void onmatchjoined (Joinmatchresponse matchinfo)
Unity New Network-networkmanager