-
Notifications
You must be signed in to change notification settings - Fork 459
feat: networkingmanager hud #510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c67a5e7
4e217ec
018dec2
86ff3e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| using System; | ||
| using MLAPI; | ||
| using MLAPI.Transports; | ||
| using UnityEngine; | ||
|
|
||
| namespace MLAPI.Prototyping | ||
| { | ||
| [RequireComponent(typeof(NetworkingManager))] | ||
| [DisallowMultipleComponent] | ||
| public class NetworkingManagerHud : MonoBehaviour | ||
| { | ||
| NetworkingManager m_NetworkingManager; | ||
|
|
||
| Transport m_Transport; | ||
|
|
||
| GUIStyle m_LabelTextStyle; | ||
|
|
||
| // This is needed to make the port field more convenient. GUILayout.TextField is very limited and we want to be able to clear the field entirely so we can't cache this as ushort. | ||
| string m_PortString; | ||
|
|
||
| public Vector2 DrawOffset = new Vector2(10, 10); | ||
|
|
||
| public Color LabelColor = Color.black; | ||
|
|
||
| void Awake() | ||
| { | ||
| // Only cache networking manager but not transport here because transport could change anytime. | ||
| m_NetworkingManager = GetComponent<NetworkingManager>(); | ||
| m_LabelTextStyle = new GUIStyle(GUIStyle.none); | ||
| } | ||
|
|
||
| void OnGUI() | ||
| { | ||
| m_LabelTextStyle.normal.textColor = LabelColor; | ||
|
|
||
| m_Transport = m_NetworkingManager.NetworkConfig.NetworkTransport; | ||
|
|
||
| if (m_PortString == null) | ||
| { | ||
| m_PortString = m_Transport.NetworkPort.ToString(); | ||
| } | ||
|
|
||
| GUILayout.BeginArea(new Rect(DrawOffset, new Vector2(200, 200))); | ||
|
|
||
| if (m_NetworkingManager.IsRunning) | ||
| { | ||
| DrawStatusGUI(); | ||
| } | ||
| else | ||
| { | ||
| DrawConnectGUI(); | ||
| } | ||
|
|
||
| GUILayout.EndArea(); | ||
| } | ||
|
|
||
| void DrawConnectGUI() | ||
| { | ||
| GUILayout.BeginHorizontal(); | ||
| GUILayout.Space(10); | ||
| GUILayout.Label("Address", m_LabelTextStyle); | ||
| GUILayout.Label("Port", m_LabelTextStyle); | ||
|
|
||
| GUILayout.EndHorizontal(); | ||
|
|
||
| GUILayout.BeginHorizontal(); | ||
|
|
||
| m_Transport.NetworkAddress = GUILayout.TextField(m_Transport.NetworkAddress); | ||
| m_PortString = GUILayout.TextField(m_PortString); | ||
| if (ushort.TryParse(m_PortString, out ushort port)) | ||
| { | ||
| m_Transport.NetworkPort = port; | ||
| } | ||
|
|
||
| GUILayout.EndHorizontal(); | ||
|
|
||
| if (GUILayout.Button("Host (Server + Client)")) | ||
| { | ||
| m_NetworkingManager.StartHost(); | ||
| } | ||
|
|
||
| GUILayout.BeginHorizontal(); | ||
|
|
||
| if (GUILayout.Button("Server")) | ||
| { | ||
| m_NetworkingManager.StartServer(); | ||
| } | ||
|
|
||
| if (GUILayout.Button("Client")) | ||
| { | ||
| m_NetworkingManager.StartClient(); | ||
| } | ||
|
|
||
| GUILayout.EndHorizontal(); | ||
| } | ||
|
|
||
| void DrawStatusGUI() | ||
| { | ||
| if (m_NetworkingManager.IsServer) | ||
| { | ||
| var mode = m_NetworkingManager.IsHost ? "Host" : "Server"; | ||
| GUILayout.Label($"{mode} active on port: {m_Transport.NetworkPort.ToString()}", m_LabelTextStyle); | ||
| } | ||
| else | ||
| { | ||
| if (m_NetworkingManager.IsConnectedClient) | ||
| { | ||
| GUILayout.Label($"Client connected {m_Transport.NetworkAddress}:{m_Transport.NetworkPort.ToString()}", m_LabelTextStyle); | ||
| } | ||
| } | ||
|
|
||
| GUILayout.Label($"Transport: {m_Transport.GetType().Name}", m_LabelTextStyle); | ||
|
|
||
| if (GUILayout.Button("Stop")) | ||
| { | ||
| m_NetworkingManager.Stop(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,6 +183,13 @@ public ulong LocalClientId | |
| /// Gets if we are connected as a client | ||
| /// </summary> | ||
| public bool IsConnectedClient { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets whether or not a server or client is running. | ||
| /// </summary> | ||
| public bool IsRunning => IsServer || IsClient; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this for convenience to check whether the networking manager is running as a client, server or host. |
||
|
|
||
|
|
||
| /// <summary> | ||
| /// The callback to invoke once a client connects. This callback is only ran on the server and on the local client that connects. | ||
| /// </summary> | ||
|
|
@@ -606,6 +613,25 @@ public void StopClient() | |
| Shutdown(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stops the running server, client or host. | ||
| /// </summary> | ||
| public void Stop() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether this is really a nice way to solve this. But I have run into this issue where I just wanted to have MLAPI stop the simulation and I didn't care whether I'm a server, client or host so I added this for convenience. |
||
| { | ||
| if (IsHost) | ||
| { | ||
| StopHost(); | ||
| } | ||
| else if(IsServer) | ||
| { | ||
| StopServer(); | ||
| } | ||
| else if (IsClient) | ||
| { | ||
| StopClient(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts a Host | ||
| /// </summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update names to new standards once they are out 😃