Communication

Let's now jump back into the Unity Editor, select NetworkManager via the Hierarchy panel, and inspect the properties shown in the Inspector panel:

The sheer amount of properties exposed illustrates the complexities encapsulated within this class; we will now step through the relevant properties so that we have a better understanding of the NetworkManager, starting with the Spawn Info section:

The NetworkManager is a Swiss army knife for multiplayer games; it surfaces a lot in order to hide the complexities of networking from the game developer, leaving them to concentrate on what matters, making a great game, and leaving networking to Unity. With Unity's HLAPI, it's possible to integrate networking into your game with little or no code. One of the features it offers is the ability to spawn (create) an associated player. If this is something you want, you can assign a prefab to the Player Prefab property (shown in the preceding screenshot) along with tweaking how it's spawned. For any other GameObject you want to spawn across the network during game play, you must register it with the NetworkManager via the RegisteredSpawnablePrefabs. Here, we have assigned the three prefabs we are using for this game: the two players and a bullet.

Next, we turn our attention to channels; channels in this context refers to the Quality of Service (QoSpolicy applied to a message (message here meaning the structure sent between peers of a networked game); the message is still transmitted through the same connection/path as other packets, but with the addition (or removal) of checks depending on the specified channel and associated policy. From within the Inspector panel, and in the following screenshot, you can see the channels used in this project:

 

Channel #0 and Channel #1 are the default channels assigned to the NetworkManager; Channel #3 is an additional channel that has been added and will later be used to transmit the WorldAnchor. We have assigned the QoS policy Reliable Fragmented, "reliable" meaning certain mechanisms are put in place to ensure that the packet is delivered, and "fragmented" allows for a message to be broken down into small chunks and allows for larger messages.

The PWNetworkManager class inherits the NetworkManager, but offers little additions; the main motivation for extending the NetworkManager is due to its vast virtual methods that allow you to monitor the state of network activity better. Here, we make use of the OnServerConnect(NetworkConnection conn) and OnClientConnect(NetworkConnection conn) hooks (called when a client connects to a server and vice versa) to signal to other components that a connection has been established, using the OnNetworkReady and OnConnectionOpen events; the following is the code snippet for the method OnClientConnect(NetworkConnection conn):

    public override void OnClientConnect(NetworkConnection conn)
{
IsServer = false;
base.OnClientConnect(conn);
IsReady = true;
OnConnectionOpen(this, conn);
}

The following is the code snippet for the method OnServerConnect(NetworkConnection conn):

public override void OnStartServer()
{
IsReady = true;
}

public override void OnServerConnect(NetworkConnection conn)
{
base.OnServerConnect(conn);
OnConnectionOpen(this, conn);
}

It's worth noting that when a connection is established, the IsReady property is set to true; this is something we will make use of within the AppManager class.

With discovery and communication taken care of, we will now shift our focus to a discussion on how we can get the instances to use a shared space, which is the topic of our next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.222.47