Unity5.1 new network engine unet (iii) Unet NetworkManager

Source: Internet
Author: User
Tags class manager dedicated server

Sun Guangdong 2015.7.12

Let's take a look at the definition of the first big class: Http://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html

Directly inherited from the Monobehaviour, there is also is designed to be a single case singleton


The NetworkManager network Manager is a convenient HLAPI class for managing network systems.

For simple network Applications NetworkManager network Manager can use Hlapi control. It provides an easy way to start and stop clients and servers, as well as manage scenarios, and has virtual functions, which user code can use to implement handlers for network events. The NetworkManager network manager processes one client at a time. The following example shows a minimal network setting.

Using Unityengine.networking;public class Manager:networkmanager {public override void Onserverconnect ( Networkconnection conn) {Debug.Log ("onplayerconnected");}}


The NetworkManager network manager is an integral part of a multiplayer game that manages network status. It is actually implemented completely using HLAPI, so everything it does is available to other forms developers. However, the network manager has packaged many useful functions to create, run, and debug multiplayer games as simple as possible.


The network manager can use no script at all. It has inspector controls in the editor, allows configuration of all its features, and Networkmanagerhud provides a simple, default user interface that allows user-controlled network games to be allowed at run time. For advanced usage, developers can derive classes from the network manager and customize their behavior by overriding virtual functions for it.

Features of the network manager include:
? Game State Management
? Spawning Management
? Scene Management
? Debugging information
? Matchmaking Matching System
? Customization Customizable


Getting Started NetworkManager


Network manager can be the core of a multiplayer game that controls components. To start, in the scene you started, create an empty game object or pick a convenient manager object. From the Network/networkmanager menu item, and then add the NetworkManager component. The newly added networkmanager should look like the following:



The NetworkManager Inspector panel in the editor allows you to configure and control many things that are related to the network.

Networkmanagerhud is another component that is related to NetworkManager. When the game runs control network state, it gives you a simple user interface. This is a good introductory knowledge network project, but it cannot be used as the ultimate UI design for the game. Networkmanagerhud looks like this:


The real game has the right UI for controlling the state of the game and allowing the player to choose what kind of game. However, to get started, we can control the game temporarily.


Game State Management


As a client, as a dedicated server, or as "Host" is available at the same time as the client and server, Unet multiplayer can be run in three modes. Unet is designed to make the same game code and assets work in all of these situations. Development for single player version game and multiplayer version game should be the same thing.


The network manager has a method for entering each pattern. Networkmanager.startclient (), Networkmanager.startserver (), and Networkmanager.starthost () are all available scripting code, so they can be called, A handler or custom UI event entered from the keyboard. You can choose to display the default run-time controls that also call these same features. There are also buttons on the Networkmanagerhud inspector that call the same function in play mode:


No matter which method is used to change the game state, networkaddress network address and Networkport network port properties are used. When the server or host is started, Networkport becomes the listening port. When the client starts, NetworkAddress is the address to connect to, and Networkport is the port to connect to.


Spawning Management

Network manager can be used to manage the instantiation of network objects from a preset. Most games have prefab as the player object, so the network manager has a slot (field), and the player prefab is dragged to assign the value. When a player prefab is set, the Player object is automatically generated from the prefab and created for each user in the game. This applies to remote players participants on the local player hosted hosted server, and on remote clients. Please note that the player prefab must have networkidentity components.


In addition to player prefab, the Presets for other objects are generated dynamically but must be clientscene registered. This can be accomplished by using the Clientscene.registerprefab () function, or it can be done automatically by the network manager. Adding a preset build list will allow them to register automatically. The field configuration of the NetworkManager inspector looks like this:


Once you have set up a player prefab, you should be able to start the game as a host and see the player object generated. Stopping this game should cause the player object to be destroyed. When you want to run another copy of the game and connect to the local host, the client should make another player object appear, and stop the client that should make the client player object be destroyed.


The player object is generated by the Networkmanager.onserveraddplayer default implementation. If you want to customize the way the Player object is created, you can override the virtual function. The default implementation is similar to the following:

public virtual void Onserveraddplayer (Networkconnection conn, short Playercontrollerid) {    var player = (Gameobject) Gameobject.instantiate (Playerprefab, Playerspawnpos, quaternion.identity);    Networkserver.addplayerforconnection (conn, player, Playercontrollerid);}


Note that the new player object is created when networkserver.addplayerforconnection () must be called function, so it is generated and associated with the client connection. This will produce the object, so networkserver.spawn does not need to be a player object.

Start positions


To control players is created, here you can use the Networkstartposition component. The network manager looks for the Networkstartposition object in the scene, if it finds it, and then it produces the player's position and orientation. Custom code can access the available networkstartpositions through the Networkmanager.startpositions list, and also a helper function getstartposition () to the network manager, which can be executed in the The Onserveraddplayer is used to find the starting position.


Scene Management


Most games have multiple scenes. At least there is usually a title screen or a Start menu scene and there are scenes to play this game. Network manager is a way to set up to automatically manage scene state and scene transitions for multiplayer games. The Properties panel of the network manager has two slots: Offlinescene and Onlinescene. Drag and drop scene objects into these slots to activate network scene management.


When the server or host starts, the online scenario is loaded. This will be the current network scenario. Any client connected to the server will be instructed to load the scene. The name of the scene is stored in the Networkscenename property.


When the network stops, by stopping the server or host, or disconnecting, the client loads the offline offline scene. When you break a multiplayer game, the game allows you to automatically return to the menu scene.


You can also change the scene when the game is active by calling Networkmanager.serverchangescene (). This will cause all currently connected clients to change the scene and will update Networkscenename, and the new customer will also load the new scene.


Although the scene management of the network is active, calling any game state management function Networkmanager.starthost () or networkmanager.stopclient () can cause the scene to change. This applies to the run-time control UI. So by setting the scene and calling these functions it is easy to control the flow of multiplayer games.


Note that scene changes cause all objects to be destroyed in the scene.  This may include network manager! If you want the network manager to not be destroyed in the conversion, make sure that the "Dont Destroy on Load" property is set to true. In a simple case, this is the best configuration. However, it is possible to have a network manager in each scenario that uses different settings to control incremental pre-loading or different scene transitions.


Debugging information


The Networkmanagerhud Properties panel displays additional information about network status at run time. This includes:


? Network connection
? Activating the Networkidentity server object
? Activating the Networkidentity client object
? Client peers


Additionally, the client message handlers that are registered are displayed in the preview window.



Matchmaking Matching System


The network manager at run time user interface and network management Inspector UI allows interaction with the matching service. The Networkmanager.startmatchmaker () function acts as a matching interface and populates the Networkmanager.matchmaker property with a Networkmatch object. Once this is activated the default Ui uses it and the callback function to the network Manager lets you perform simple pairing.

A function that has a network manager for a virtual derived class can use a custom respoding to give the matchmaker callback the behavior.


Customization Custom


A virtual function on a derived class of a network manager can use custom behavior. When performing these functions, be sure to take care of the functionality provided by the default implementation. For example, in Onserveraddplayer (), the Networkserver.addplayer function must be called to the active Player object connection.


Functions that are called on Server/host:

Called when a client connects public virtual void Onserverconnect (Networkconnection conn);//Called when a client disco Nnectspublic virtual void Onserverdisconnect (Networkconnection conn) {networkserver.destroyplayersforconnection ( conn);} Called when a client is readypublic virtual void Onserverready (Networkconnection conn) {Networkserver.setclientready (conn);} Called when a new player was added for a clientpublic virtual void Onserveraddplayer (Networkconnection conn, short Playe    Rcontrollerid) {var player = (Gameobject) gameobject.instantiate (Playerprefab, Playerspawnpos, quaternion.identity); Networkserver.addplayer (conn, player, Playercontrollerid);} Called when a player was removed for a clientpublic virtual void Onserverremoveplayer (Networkconnection conn, short play    Ercontrollerid) {Uplayer player; IF (Conn. Getplayer (Playercontrollerid, out player)) {if (player). Networkidentity! = null && player.          Networkidentity.gameobject = null)  Networkserver.destroy (Player.NetworkIdentity.gameObject); }}//called when a network error occurspublic virtual void Onservererror (networkconnection conn, int errorCode);

Functions that are called on the client:

Called when connected to a serverpublic virtual void Onclientconnect (Networkconnection conn) {    Clientscene.ready ( conn);    Clientscene.addplayer (0);} Called when disconnected from a serverpublic virtual void Onclientdisconnect (Networkconnection conn) {    stopclient ( );} Called when a network error occurspublic virtual void Onclienterror (networkconnection conn, int errorCode);//called WH En told to is not-ready by a serverpublic virtual void Onclientnotready (Networkconnection conn);

function to call when matching:

Called when a match is createdpublic virtual void onmatchcreate (Creatematchresponse matchinfo)//Called when a list of  Matches is receivedpublic virtual void onmatchlist (Listmatchresponse matchlist)//Called when a match is joinedpublic void onmatchjoined (Joinmatchresponse matchinfo)






Inspector

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unity5.1 new network engine unet (iii) Unet NetworkManager

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.