Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

need example of custom Network Manager HUD

Discussion in 'Connected Games' started by linojon, Jun 14, 2015.

  1. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    48
    Hi I want to make a Network Manager HUD in world space. It can look exactly the same as the built-in one, for now. I'm having trouble finding example code or explanation for how to do this. Thanks!
     
  2. kalvinmoraes

    kalvinmoraes

    Joined:
    May 31, 2015
    Posts:
    2
  3. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    48
    Hi, yes I have read these but could benefit from a C-Sharp example how to simply start a client connecting to a specific IP address. I tried

    Network.Connect("10.0.1.14", 7777);

    but that doesnt seem to be sufficient
     
    Last edited: Jun 17, 2015
  4. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    723
  5. thecraftymccrafterson

    thecraftymccrafterson

    Joined:
    Dec 5, 2016
    Posts:
    2
    Ok I know I'm reviving and old post -but from what I know from the docs, you need to call StartHost or StartServer, and StartClient on the network manager from the GUI. I haven't done this yet.

    Were you able to do this?
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    5,918
    Code (csharp):
    1.  
    2. // (c) Copyright Cleverous 2016. All rights reserved.
    3.  
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. namespace Isobox
    8. {
    9.     public class IsoboxNetHUD : MonoBehaviour
    10.     {
    11.         public string IpAddress;
    12.         public string Port;
    13.         public float GuiOffset;
    14.         private bool _started;
    15.  
    16.         public void Start()
    17.         {
    18.             _started = false;
    19.         }
    20.         public void OnGUI()
    21.         {
    22.             GUILayout.Space(GuiOffset);
    23.             if (!_started)
    24.             {
    25.                 if (GUILayout.Button("Host"))
    26.                 {
    27.                     _started = true;
    28.                     NetworkManager.singleton.networkPort = int.Parse(Port);
    29.                     NetworkManager.singleton.StartHost();
    30.                 }
    31.  
    32.                 GUILayout.Space(25);
    33.                 IpAddress = GUILayout.TextField(IpAddress, GUILayout.Width(100));
    34.                 Port = GUILayout.TextField(Port, 5);
    35.                 if (GUILayout.Button("Connect"))
    36.                 {
    37.                     _started = true;
    38.                     NetworkManager.singleton.networkAddress = IpAddress;
    39.                     NetworkManager.singleton.networkPort = int.Parse(Port);
    40.                     NetworkManager.singleton.StartClient();
    41.                 }
    42.             }
    43.             else
    44.             {
    45.                 if (GUILayout.Button("Disconnect"))
    46.                 {
    47.                     _started = false;
    48.                     NetworkManager.singleton.StopHost();
    49.                 }
    50.             }
    51.         }
    52.     }
    53. }
    54.  
    Use 'localhost' as the IP to connect if the editor is the Host.

    You must have an instance of NetworkManager.cs somewhere so it can register .singleton.
     
    Last edited: Dec 8, 2016
  7. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    I wanted to make a custom Network Manager HUD script, but the script I got from the Unity help documentation doesn't work because it is obsolete/wrong. Anyways, the script below is a properly updated version of Unity's NetworkManagerHUD.cs, but with no extra's added yet. It works with Unity's Network Manager (just add a Network Manager component to an object, then add this NetworkManagerHUD script to the object also). The next step is to replace it's default GUI.Buttons and change the keycodes around, but at least this is a starting point..

    Code (CSharp):
    1. #if ENABLE_UNET
    2.  
    3.  
    4. namespace UnityEngine.Networking
    5.  
    6. {
    7.  
    8. [AddComponentMenu("Network/NetworkManagerHUD")]
    9.  
    10. [RequireComponent(typeof(NetworkManager))]
    11.  
    12. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    13.  
    14. public class NetworkManagerHUD : MonoBehaviour
    15.  
    16. {
    17.  
    18.   public NetworkManager manager;
    19.  
    20.   [SerializeField] public bool showGUI = true;
    21.  
    22.   [SerializeField] public int offsetX;
    23.  
    24.   [SerializeField] public int offsetY;
    25.  
    26.  
    27.   // Runtime variable
    28.  
    29.   bool showServer = false;
    30.  
    31.  
    32.   void Awake()
    33.  
    34.   {
    35.  
    36.    manager = GetComponent<NetworkManager>();
    37.  
    38.   }
    39.  
    40.  
    41.   void Update()
    42.  
    43.   {
    44.  
    45.    if (!showGUI)
    46.  
    47.     return;
    48.  
    49.  
    50.    if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
    51.  
    52.    {
    53.  
    54.     if (Input.GetKeyDown(KeyCode.S))
    55.  
    56.     {
    57.  
    58.      manager.StartServer();
    59.  
    60.     }
    61.  
    62.     if (Input.GetKeyDown(KeyCode.H))
    63.  
    64.     {
    65.  
    66.      manager.StartHost();
    67.  
    68.     }
    69.  
    70.     if (Input.GetKeyDown(KeyCode.C))
    71.  
    72.     {
    73.  
    74.      manager.StartClient();
    75.  
    76.     }
    77.  
    78.    }
    79.  
    80.    if (NetworkServer.active && NetworkClient.active)
    81.  
    82.    {
    83.  
    84.     if (Input.GetKeyDown(KeyCode.X))
    85.  
    86.     {
    87.  
    88.      manager.StopHost();
    89.  
    90.     }
    91.  
    92.    }
    93.  
    94.   }
    95.  
    96.  
    97.   void OnGUI()
    98.  
    99.   {
    100.  
    101.    if (!showGUI)
    102.  
    103.     return;
    104.  
    105.  
    106.    int xpos = 10 + offsetX;
    107.  
    108.    int ypos = 40 + offsetY;
    109.  
    110.    int spacing = 24;
    111.  
    112.  
    113.  
    114.    if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
    115.  
    116.    {
    117.  
    118.     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Host(H)"))
    119.  
    120.     {
    121.  
    122.      manager.StartHost();
    123.  
    124.     }
    125.  
    126.     ypos += spacing;
    127.  
    128.  
    129.     if (GUI.Button(new Rect(xpos, ypos, 105, 20), "LAN Client(C)"))
    130.  
    131.     {
    132.  
    133.      manager.StartClient();
    134.  
    135.     }
    136.  
    137.     manager.networkAddress = GUI.TextField(new Rect(xpos + 100, ypos, 95, 20), manager.networkAddress);
    138.  
    139.     ypos += spacing;
    140.  
    141.  
    142.  
    143.     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Server Only(S)"))
    144.  
    145.     {
    146.  
    147.      manager.StartServer();
    148.  
    149.     }
    150.  
    151.     ypos += spacing;
    152.  
    153.    }
    154.  
    155.    else
    156.  
    157.    {
    158.  
    159.     if (NetworkServer.active)
    160.  
    161.     {
    162.  
    163.      GUI.Label(new Rect(xpos, ypos, 300, 20), "Server: port=" + manager.networkPort);
    164.  
    165.      ypos += spacing;
    166.  
    167.     }
    168.  
    169.     if (NetworkClient.active)
    170.  
    171.     {
    172.  
    173.      GUI.Label(new Rect(xpos, ypos, 300, 20), "Client: address=" + manager.networkAddress + " port=" + manager.networkPort);
    174.  
    175.      ypos += spacing;
    176.  
    177.     }
    178.  
    179.    }
    180.  
    181.  
    182.  
    183.  
    184.    if (NetworkClient.active && !ClientScene.ready)
    185.  
    186.    {
    187.  
    188.     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Client Ready"))
    189.  
    190.     {
    191.  
    192.      ClientScene.Ready(manager.client.connection);
    193.  
    194.  
    195.  
    196.      if (ClientScene.localPlayers.Count == 0)
    197.  
    198.      {
    199.  
    200.       ClientScene.AddPlayer(0);
    201.  
    202.      }
    203.  
    204.     }
    205.  
    206.     ypos += spacing;
    207.  
    208.    }
    209.  
    210.  
    211.  
    212.  
    213.    if (NetworkServer.active || NetworkClient.active)
    214.  
    215.    {
    216.  
    217.     if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Stop (X)"))
    218.  
    219.     {
    220.  
    221.      manager.StopHost();
    222.  
    223.     }
    224.  
    225.     ypos += spacing;
    226.  
    227.    }
    228.  
    229.  
    230.  
    231.  
    232.    if (!NetworkServer.active && !NetworkClient.active)
    233.  
    234.    {
    235.  
    236.     ypos += 10;
    237.  
    238.  
    239.  
    240.  
    241.     if (manager.matchMaker == null)
    242.  
    243.     {
    244.  
    245.      if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Enable Match Maker (M)"))
    246.  
    247.      {
    248.  
    249.       manager.StartMatchMaker();
    250.  
    251.      }
    252.  
    253.      ypos += spacing;
    254.  
    255.     }
    256.  
    257.     else
    258.  
    259.     {
    260.  
    261.      if (manager.matchInfo == null)
    262.  
    263.      {
    264.  
    265.       if (manager.matches == null)
    266.  
    267.       {
    268.  
    269.        if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Create Internet Match"))
    270.  
    271.        {
    272.  
    273.         manager.matchMaker.CreateMatch(manager.matchName, manager.matchSize, true, "", "", "", 0, 0, manager.OnMatchCreate);
    274.  
    275.        }
    276.  
    277.        ypos += spacing;
    278.  
    279.  
    280.  
    281.  
    282.        GUI.Label(new Rect(xpos, ypos, 100, 20), "Room Name:");
    283.  
    284.        manager.matchName = GUI.TextField(new Rect(xpos+100, ypos, 100, 20), manager.matchName);
    285.  
    286.        ypos += spacing;
    287.  
    288.  
    289.  
    290.  
    291.        ypos += 10;
    292.  
    293.  
    294.  
    295.  
    296.        if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Find Internet Match"))
    297.  
    298.        {
    299.  
    300.         manager.matchMaker.ListMatches(0,20, "", true, 0, 0, manager.OnMatchList);
    301.  
    302.        }
    303.  
    304.        ypos += spacing;
    305.  
    306.       }
    307.  
    308.       else
    309.  
    310.       {
    311.  
    312.        foreach (var match in manager.matches)
    313.  
    314.        {
    315.  
    316.         if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Join Match:" + match.name))
    317.  
    318.         {
    319.  
    320.          manager.matchName = match.name;
    321.  
    322.          manager.matchSize = (uint)match.currentSize;
    323.  
    324.          manager.matchMaker.JoinMatch(match.networkId, "" , "", "", 0, 0, manager.OnMatchJoined);
    325.  
    326.         }
    327.  
    328.         ypos += spacing;
    329.  
    330.        }
    331.  
    332.       }
    333.  
    334.      }
    335.  
    336.  
    337.  
    338.  
    339.      if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Change MM server"))
    340.  
    341.      {
    342.  
    343.       showServer = !showServer;
    344.  
    345.      }
    346.  
    347.      if (showServer)
    348.  
    349.      {
    350.  
    351.       ypos += spacing;
    352.  
    353.       if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Local"))
    354.  
    355.       {
    356.  
    357.        manager.SetMatchHost("localhost", 1337, false);
    358.  
    359.        showServer = false;
    360.  
    361.       }
    362.  
    363.       ypos += spacing;
    364.  
    365.       if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Internet"))
    366.  
    367.       {
    368.  
    369.        manager.SetMatchHost("mm.unet.unity3d.com", 443, true);
    370.  
    371.        showServer = false;
    372.  
    373.       }
    374.  
    375.       ypos += spacing;
    376.  
    377.       if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Staging"))
    378.  
    379.       {
    380.  
    381.        manager.SetMatchHost("staging-mm.unet.unity3d.com", 443, true);
    382.  
    383.        showServer = false;
    384.  
    385.       }
    386.  
    387.      }
    388.  
    389.  
    390.  
    391.  
    392.      ypos += spacing;
    393.  
    394.  
    395.  
    396.  
    397.      GUI.Label(new Rect(xpos, ypos, 300, 20), "MM Uri: " + manager.matchMaker.baseUri);
    398.  
    399.      ypos += spacing;
    400.  
    401.  
    402.  
    403.  
    404.      if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Disable Match Maker"))
    405.  
    406.      {
    407.  
    408.       manager.StopMatchMaker();
    409.  
    410.      }
    411.  
    412.      ypos += spacing;
    413.  
    414.     }
    415.  
    416.    }
    417.  
    418.   }
    419.  
    420. }
    421.  
    422. };
    423.  
    424. #endif //ENABLE_UNET
    425.  
    *This is for Unity versions up to 5.x, I'm not sure if it still works for versions 2017.x
    The problem I had with the original script was Unity did not update their documentation or network scripts, and the original Unity NM-HUD script was no longer valid because of missing arguments in the functions. The default addComponent-->Network Manager HUD script that is added through the editor has the correct function arguments, but does not allow access to change the NM HUD script.
    -----------
    Quick Update: April 2018 - There might be another minor problem in the Network Manager HUD script. There are keys set up to control the Network Manager HUD, such as 'X' to exit, 'H' to host, etc. I noticed that not all the keys work during runtime. Additionally, some of those key settings might interfere with common game-key setups. --Maybe someone could confirm the issue with the keys that control the HUD (without having to use the GUI buttons), because I don't see anything wrong in the key input code.
     
    Last edited: Apr 5, 2018
    ParityJames, Burkard, dasouth and 3 others like this.
  8. cLick1338

    cLick1338

    Joined:
    Feb 23, 2017
    Posts:
    3
    Honorsoft's code is spot on, and it also works great on 2017.3 for me. Many thanks, very useful.
     
  9. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    I'm glad. I am still learning Unity, but I have a lot of previous programming experience. I didn't know about Unity2017 because I'm still stuck on a 32-bit system (I'm thankful for Unity supporting 32-bit so well, Unreal doesn't at all). I would love to take 2017.x for a spin, but I'll have to wait.
     
  10. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    Just to clarify, if anyone had the same problem with the Unity Documentation's NetworkManagerHUD script, it apparently was missing a function (or functions). I keep hearing that Unity never bothered updating their Networking scripts after updates to Unity made them obsolete/non-functioning. So this one should work, and I'll try to post a version that is even more customized soon, so that other's learning can see how to make them more stylized.
     
  11. ParityJames

    ParityJames

    Joined:
    Mar 29, 2012
    Posts:
    1
    Sweet thanks much Honorsoft i was just looking for the script you posted above as a starting point
     
  12. Jamesthe1

    Jamesthe1

    Joined:
    May 9, 2018
    Posts:
    3
    Thanks Honorsoft, but how would one disconnect if they aren't the one hosting the server?
     
  13. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    I'm having trouble picturing it right now (I must be tired), but if I understand what you're asking, it's an online (multi-player) game you're talking about, and you want to be able to disconnect from a live game or in-game chat room. I've seen some good code examples (and even tutorials) so far about how to set up a really adaptable network system for a MMORPG. They dealt with being able to switch from being 'host' to re-joining the game as a 'client' and transferring the hosting to another user, without leaving the game, so it can be done. It isn't as hard as it seems. Sorry I don't have any code for you(yet), but basically you want to have a game that keeps really good track of the hosts and clients, and can re-assign the host without dropping anyone from the game. I think the way the default Unity network script works is that you can disconnect from the game as a client, and it boots you from that game. Also, you can disconnect from the game as a host, and it boots everybody (host and clients). Someone correct me if I'm wrong. I'm going to have to figure that out too for my own projects soon.
     
  14. Jamesthe1

    Jamesthe1

    Joined:
    May 9, 2018
    Posts:
    3
    Yeah, I saw a function that changes who the host is. Though I just want the client leave the game. The default HUD offers a way to disconnect, but I'm working on one that offers me more control.
     
  15. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    Okay, I understand better now. I will work on the Networking HUD now too (I will post any good results here), some of my projects are ready to be made into online versions. I already fixed the default Unity NetworkManager.cs script, when I first tried to use it in Unity 5 I received a bunch of 'missing functions' errors, so I re-wrote parts of it (honestly I don't quite remember what I changed now), and it worked great. I got some good feedback from users online, and even someone using Unity2017 said it helped them too. But there is a lot more work to be done on NetworkManager.cs and NetworkManagerHUD.cs.
     
  16. Jamesthe1

    Jamesthe1

    Joined:
    May 9, 2018
    Posts:
    3
    I looked more into it, and discovered that there is a way to do so.

    Here's the snippet that includes the disconnect function:
     
  17. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    Thanks, I'm going to try that out. I've been experimenting with Unity's Networking Lobby Example too, I will probably build on the basic NetworkManager and NetworkManagerHUD scripts.