123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // vis2k: GUILayout instead of spacey += ...; removed Update hotkeys to avoid
- // confusion if someone accidentally presses one.
- using System;
- using UnityEngine;
- namespace Mirror
- {
- /// <summary>Shows NetworkManager controls in a GUI at runtime.</summary>
- [DisallowMultipleComponent]
- [AddComponentMenu("Network/NetworkManagerHUD")]
- [RequireComponent(typeof(NetworkManager))]
- [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-manager-hud")]
- public class NetworkManagerHUD : MonoBehaviour
- {
- NetworkManager manager;
- // Deprecated 2021-02-24
- [Obsolete("showGUI will be removed unless someone has a valid use case. Simply use or don't use the HUD component.")]
- public bool showGUI = true;
- public int offsetX;
- public int offsetY;
- void Awake()
- {
- manager = GetComponent<NetworkManager>();
- }
- void OnGUI()
- {
- #pragma warning disable 618
- if (!showGUI) return;
- #pragma warning restore 618
- GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999));
- if (!NetworkClient.isConnected && !NetworkServer.active)
- {
- StartButtons();
- }
- else
- {
- StatusLabels();
- }
- // client ready
- if (NetworkClient.isConnected && !NetworkClient.ready)
- {
- if (GUILayout.Button("Client Ready"))
- {
- NetworkClient.Ready();
- if (NetworkClient.localPlayer == null)
- {
- NetworkClient.AddPlayer();
- }
- }
- }
- StopButtons();
- GUILayout.EndArea();
- }
- void StartButtons()
- {
- if (!NetworkClient.active)
- {
- // Server + Client
- if (Application.platform != RuntimePlatform.WebGLPlayer)
- {
- if (GUILayout.Button("Host (Server + Client)"))
- {
- manager.StartHost();
- }
- }
- // Client + IP
- GUILayout.BeginHorizontal();
- if (GUILayout.Button("Client"))
- {
- manager.StartClient();
- }
- manager.networkAddress = GUILayout.TextField(manager.networkAddress);
- GUILayout.EndHorizontal();
- // Server Only
- if (Application.platform == RuntimePlatform.WebGLPlayer)
- {
- // cant be a server in webgl build
- GUILayout.Box("( WebGL cannot be server )");
- }
- else
- {
- if (GUILayout.Button("Server Only")) manager.StartServer();
- }
- }
- else
- {
- // Connecting
- GUILayout.Label("Connecting to " + manager.networkAddress + "..");
- if (GUILayout.Button("Cancel Connection Attempt"))
- {
- manager.StopClient();
- }
- }
- }
- void StatusLabels()
- {
- // host mode
- // display separately because this always confused people:
- // Server: ...
- // Client: ...
- if (NetworkServer.active && NetworkClient.active)
- {
- GUILayout.Label($"<b>Host</b>: running via {Transport.activeTransport}");
- }
- // server only
- else if (NetworkServer.active)
- {
- GUILayout.Label($"<b>Server</b>: running via {Transport.activeTransport}");
- }
- // client only
- else if (NetworkClient.isConnected)
- {
- GUILayout.Label($"<b>Client</b>: connected to {manager.networkAddress} via {Transport.activeTransport}");
- }
- }
- void StopButtons()
- {
- // stop host if host mode
- if (NetworkServer.active && NetworkClient.isConnected)
- {
- if (GUILayout.Button("Stop Host"))
- {
- manager.StopHost();
- }
- }
- // stop client if client-only
- else if (NetworkClient.isConnected)
- {
- if (GUILayout.Button("Stop Client"))
- {
- manager.StopClient();
- }
- }
- // stop server if server-only
- else if (NetworkServer.active)
- {
- if (GUILayout.Button("Stop Server"))
- {
- manager.StopServer();
- }
- }
- }
- }
- }
|