Search Unity

Making My Own NetworkManager HUD?

Discussion in 'Multiplayer' started by wwm0nkey, Jul 8, 2015.

  1. wwm0nkey

    wwm0nkey

    Joined:
    Jul 7, 2015
    Posts:
    42
    Okay so I've been working on this project for Oculus for a few weeks now and I've been working on the networking aspect of it. Networking works, but I have the problem where the Network Manager HUD wont show up because of the way its set up to display on the screen. I've already started on making my own HUD but I do not know how to set it up to behave exactly like the Network Manager HUD.

    Is the actual script for that out anywhere and if so where can I find it since Unity wont let me access it? Also is there just a way to change how the Manager HUD is displayed on the screen so I don't have to do this? If it was Screen Space Camera it would work perfectly, but its not so now I need to make my own.
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  3. wwm0nkey

    wwm0nkey

    Joined:
    Jul 7, 2015
    Posts:
    42
    Okay so I can get it to create an Internet Match via only button commands but I get debug errors on my build saying my player id is -1 and it needs to be 0 or 1. When I try to join it just falls on its face.

    Im using the source code though so I don't know why its throwing me these issues since the normal Network Manager HUD works without any of these errors and the only thing I did was add in keycommands.

    Here is the code:
    Code (CSharp):
    1. #if ENABLE_UNET
    2.  
    3. namespace UnityEngine.Networking
    4. {
    5.     [AddComponentMenu("Network/NetworkManagerHUD")]
    6.     [RequireComponent(typeof(NetworkManager))]
    7.     [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    8.     public class NetworkManagerHUD_Custom : MonoBehaviour
    9.     {
    10.         public NetworkManager manager;
    11.         [SerializeField] public bool showGUI = true;
    12.         [SerializeField] public int offsetX;
    13.         [SerializeField] public int offsetY;
    14.  
    15.         // Runtime variable
    16.         bool showServer = false;
    17.  
    18.         void Awake()
    19.         {
    20.             manager = GetComponent<NetworkManager>();
    21.         }
    22.  
    23.         void Update()
    24.         {
    25.             if (!showGUI)
    26.                 return;
    27.  
    28.             if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
    29.             {
    30.                 if (Input.GetKeyDown(KeyCode.S))
    31.                 {
    32.                     manager.StartServer();
    33.                 }
    34.                 if (Input.GetKeyDown(KeyCode.H))
    35.                 {
    36.                     manager.StartHost();
    37.                 }
    38.                 if (Input.GetKeyDown(KeyCode.C))
    39.                 {
    40.                     manager.StartClient();
    41.                 }
    42.                 if (Input.GetKeyDown(KeyCode.M))
    43.                 {
    44.                     manager.StartMatchMaker();
    45.                 }
    46.  
    47.                 /*if (Input.GetKeyDown(KeyCode.J))
    48.                 {
    49.                     manager.matchName = match.name;
    50.                     manager.matchSize = (uint)match.currentSize;
    51.                     manager.matchMaker.JoinMatch(match.networkId, "", manager.OnMatchJoined);
    52.                 }*/
    53.             }
    54.             if (NetworkServer.active && NetworkClient.active)
    55.             {
    56.                 if (Input.GetKeyDown(KeyCode.X))
    57.                 {
    58.                     manager.StopHost();
    59.                 }
    60.             }
    61.         }
    62.  
    63.         void OnGUI()
    64.         {
    65.             if (!showGUI)
    66.                 return;
    67.  
    68.             int xpos = 10 + offsetX;
    69.             int ypos = 40 + offsetY;
    70.             int spacing = 24;
    71.  
    72.             if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
    73.             {
    74.                 if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Host(H)"))
    75.                 {
    76.                     manager.StartHost();
    77.                 }
    78.                 ypos += spacing;
    79.  
    80.                 if (GUI.Button(new Rect(xpos, ypos, 105, 20), "LAN Client(C)"))
    81.                 {
    82.                     manager.StartClient();
    83.                 }
    84.                 manager.networkAddress = GUI.TextField(new Rect(xpos + 100, ypos, 95, 20), manager.networkAddress);
    85.                 ypos += spacing;
    86.  
    87.                 if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Server Only(S)"))
    88.                 {
    89.                     manager.StartServer();
    90.                 }
    91.                 ypos += spacing;
    92.             }
    93.             else
    94.             {
    95.                 if (NetworkServer.active)
    96.                 {
    97.                     GUI.Label(new Rect(xpos, ypos, 300, 20), "Server: port=" + manager.networkPort);
    98.                     ypos += spacing;
    99.                 }
    100.                 if (NetworkClient.active)
    101.                 {
    102.                     GUI.Label(new Rect(xpos, ypos, 300, 20), "Client: address=" + manager.networkAddress + " port=" + manager.networkPort);
    103.                     ypos += spacing;
    104.                 }
    105.             }
    106.  
    107.             if (NetworkClient.active && !ClientScene.ready)
    108.             {
    109.                 if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Client Ready")||Input.GetKeyDown(KeyCode.R))
    110.                 {
    111.                     ClientScene.Ready(manager.client.connection);
    112.                
    113.                     if (ClientScene.localPlayers.Count == 0)
    114.                     {
    115.                         ClientScene.AddPlayer(0);
    116.                     }
    117.                 }
    118.                 ypos += spacing;
    119.             }
    120.  
    121.             if (NetworkServer.active || NetworkClient.active)
    122.             {
    123.                 if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Stop (X)")|| Input.GetKeyDown(KeyCode.X))
    124.                 {
    125.                     manager.StopHost();
    126.                 }
    127.                 ypos += spacing;
    128.             }
    129.  
    130.             if (!NetworkServer.active && !NetworkClient.active)
    131.             {
    132.                 ypos += 10;
    133.  
    134.                 if (manager.matchMaker == null || Input.GetKeyDown(KeyCode.M))
    135.                 {
    136.                     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Enable Match Maker (M)"))
    137.                     {
    138.                         manager.StartMatchMaker();
    139.                     }
    140.                     ypos += spacing;
    141.                 }
    142.                 else
    143.                 {
    144.                     if (manager.matchInfo == null)
    145.                     {
    146.                         if (manager.matches == null)
    147.                         {
    148.                             if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Create Internet Match")|| Input.GetKeyDown(KeyCode.N))
    149.                             {
    150.                                 manager.matchMaker.CreateMatch(manager.matchName, manager.matchSize, true, "", manager.OnMatchCreate);
    151.                             }
    152.                             ypos += spacing;
    153.  
    154.                             GUI.Label(new Rect(xpos, ypos, 100, 20), "Room Name:");
    155.                             manager.matchName = GUI.TextField(new Rect(xpos+100, ypos, 100, 20), manager.matchName);
    156.                             ypos += spacing;
    157.  
    158.                             ypos += 10;
    159.  
    160.                             if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Find Internet Match")|| Input.GetKeyDown(KeyCode.J))
    161.                             {
    162.                                 manager.matchMaker.ListMatches(0,20, "", manager.OnMatchList);
    163.                             }
    164.                             ypos += spacing;
    165.                         }
    166.                         else
    167.                         {
    168.                             foreach (var match in manager.matches)
    169.                             {
    170.                                 if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Join Match:" + match.name)|| Input.GetKeyDown(KeyCode.K))
    171.                                 {
    172.                                     manager.matchName = match.name;
    173.                                     Debug.Log(match.name);
    174.                                     manager.matchSize = (uint)match.currentSize;
    175.                                     manager.matchMaker.JoinMatch(match.networkId, "", manager.OnMatchJoined);
    176.                                 }
    177.                                 ypos += spacing;
    178.                             }
    179.                         }
    180.                     }
    181.  
    182.                     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Change MM server"))
    183.                     {
    184.                         showServer = !showServer;
    185.                     }
    186.                     if (showServer)
    187.                     {
    188.                         ypos += spacing;
    189.                         if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Local"))
    190.                         {
    191.                             manager.SetMatchHost("localhost", 1337, false);
    192.                             showServer = false;
    193.                         }
    194.                         ypos += spacing;
    195.                         if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Internet"))
    196.                         {
    197.                             manager.SetMatchHost("mm.unet.unity3d.com", 443, true);
    198.                             showServer = false;
    199.                         }
    200.                         ypos += spacing;
    201.                         if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Staging"))
    202.                         {
    203.                             manager.SetMatchHost("staging-mm.unet.unity3d.com", 443, true);
    204.                             showServer = false;
    205.                         }
    206.                     }
    207.  
    208.                     ypos += spacing;
    209.  
    210.                     GUI.Label(new Rect(xpos, ypos, 300, 20), "MM Uri: " + manager.matchMaker.baseUri);
    211.                     ypos += spacing;
    212.  
    213.                     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Disable Match Maker"))
    214.                     {
    215.                         manager.StopMatchMaker();
    216.                     }
    217.                     ypos += spacing;
    218.                 }
    219.             }
    220.         }
    221.     }
    222. };
    223. #endif //ENABLE_UNET
    Here is the error I get in the build:


    and in the editor it says "A Connection Has Already Been Set To Ready, There Can Only Be One"
     
    Last edited: Jul 14, 2015
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Looks like it got called multiple times?
     
    RandomAgent likes this.
  5. wontonst

    wontonst

    Joined:
    Dec 29, 2013
    Posts:
    6
  6. wwm0nkey

    wwm0nkey

    Joined:
    Jul 7, 2015
    Posts:
    42
    Okay so I have my HUD working, but Im still getting the player ID error, but the weird thing is, it only happens when in Oculus for some odd reason.
     
  7. StepanBlog

    StepanBlog

    Joined:
    Mar 30, 2017
    Posts:
    1
    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Networking.NetworkManagerHUD_Simple.OnGUI () (at Assets/NetworkManagerHUD_Simple.cs:58)