Search Unity

More examples for Unity's new Networking classes!

Discussion in 'Documentation' started by _Adriaan, Oct 6, 2015.

  1. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Hi there!

    I'd like to contribute to the (lack of) examples of Unity's new HLAPI Networking docs! I hope to transfer what I learned after endlessly scrolling through sample projects, Networking forum posts, and decompiling the HLAPI Networking components. Ready... set... go!

    --- NetworkDiscovery ---
    The text on this class needs to state that this class should be overriden to receive the call on OnReceivedBroadcast. Both the class itself and the OnReceiveBroadcast page could include the following example:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class OverriddenNetworkDiscovery : NetworkDiscovery
    6. {
    7.  
    8.     public override void OnReceivedBroadcast (string fromAddress, string data)
    9.     {
    10.         NetworkManager.singleton.networkAddress = fromAddress;
    11.         NetworkManager.singleton.StartClient();
    12.     }
    13.  
    14. }
    15.  
    --- NetworkMatch ---
    Main class could get a barebones example like below. I would also suggest adding the specific example functions below to individual matchmaker functions, such as the CreateMatch, JoinMatch, and ListMatches methods.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.Match;
    4. using UnityEngine.Networking.Types;
    5. using System.Collections;
    6.  
    7. public class SimpleMatchMaker : MonoBehaviour
    8. {
    9.  
    10.     void Start()
    11.     {
    12.         NetworkManager.singleton.StartMatchMaker();
    13.     }
    14.    
    15.     //call this method to request a match to be created on the server
    16.     public void CreateInternetMatch(string matchName)
    17.     {
    18.         CreateMatchRequest create = new CreateMatchRequest();
    19.         create.name = matchName;
    20.         create.size = 4;
    21.         create.advertise = true;
    22.         create.password = "";
    23.        
    24.         NetworkManager.singleton.matchMaker.CreateMatch(create, OnInternetMatchCreate);
    25.     }
    26.  
    27.     //this method is called when your request for creating a match is returned
    28.     private void OnInternetMatchCreate(CreateMatchResponse matchResponse)
    29.     {
    30.         if (matchResponse != null && matchResponse.success)
    31.         {
    32.             //Debug.Log("Create match succeeded");
    33.            
    34.             Utility.SetAccessTokenForNetwork(matchResponse.networkId, new NetworkAccessToken(matchResponse.accessTokenString));
    35.             MatchInfo hostInfo = new MatchInfo(matchResponse);
    36.             NetworkServer.Listen(hostInfo, 9000);
    37.            
    38.             NetworkManager.singleton.StartHost(hostInfo);
    39.         }
    40.         else
    41.         {
    42.             Debug.LogError("Create match failed");
    43.         }
    44.     }
    45.  
    46.     //call this method to find a match through the matchmaker
    47.     public void FindInternetMatch(string matchName)
    48.     {
    49.         NetworkManager.singleton.matchMaker.ListMatches(0, 20, matchName, OnInternetMatchList);
    50.     }
    51.  
    52.     //this method is called when a list of matches is returned
    53.     private void OnInternetMatchList(ListMatchResponse matchListResponse)
    54.     {
    55.         if (matchListResponse.success)
    56.         {
    57.             if(matchListResponse.matches.Count != 0)
    58.             {
    59.                 //Debug.Log("A list of matches was returned");
    60.  
    61.                 //join the last server (just in case there are two...)
    62.                 NetworkManager.singleton.matchMaker.JoinMatch(matchListResponse.matches[matchListResponse.matches.Count - 1].networkId, "", OnJoinInternetMatch);
    63.             }
    64.             else
    65.             {
    66.                 Debug.Log ("No matches in requested room!");
    67.             }
    68.         }
    69.         else
    70.         {
    71.             Debug.LogError("Couldn't connect to match maker");
    72.         }
    73.     }
    74.  
    75.     //this method is called when your request to join a match is returned
    76.     private void OnJoinInternetMatch(JoinMatchResponse matchJoin)
    77.     {
    78.         if (matchJoin.success)
    79.         {
    80.             //Debug.Log("Able to join a match");
    81.  
    82.             if(Utility.GetAccessTokenForNetwork(matchJoin.networkId) == null)
    83.                 Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
    84.            
    85.             MatchInfo hostInfo = new MatchInfo(matchJoin);
    86.             NetworkManager.singleton.StartClient(hostInfo);
    87.         }
    88.         else
    89.         {
    90.             Debug.LogError("Join match failed");
    91.            
    92.         }
    93.     }
    94.  
    95. }
    --- NetworkBehaviour.OnStartLocalPlayer ---
    Code (CSharp):
    1. public override void OnStartLocalPlayer()
    2.     {
    3.         //add an additional component to this object which listens to Input
    4.         AddComponent<PlayerInput>();
    5.     }
    I'll probably have more later, which I will be posting here if that's okay! Also, do let me know if you'd like me to give you these examples in a different way, I'll gladly adjust.
     
    Carrotpie likes this.
  2. master_rigel

    master_rigel

    Joined:
    Mar 24, 2014
    Posts:
    69
    Hmm. Oops. I wasn't patient enough! In any case. These examples look really good. I hope they get some good attention.
     
  3. dumbfire

    dumbfire

    Joined:
    Dec 11, 2013
    Posts:
    3
    @_Adriaan I have been trying your posted example for NetworkDiscovery and it works fine when there is a single host on the local network. However, if there are more servers for a single game on the network then it just auto-starts the client against the first broadcast received. This does seem odd as the GUI for NetworkDiscovery shows each of the possible IPs of hosts on the network with a clickable button.