2023-05-08 15:56:10 +03:00

118 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.Net;
using UnityEngine;
namespace FishNet.Discovery
{
public sealed class NetworkDiscoveryHUD : MonoBehaviour
{
[SerializeField]
private NetworkDiscovery networkDiscovery;
private Camera _camera;
private readonly List<IPEndPoint> _endPoints = new List<IPEndPoint>();
private Vector2 _serversListScrollVector;
private bool _useVR;
private void Start()
{
if (networkDiscovery == null) networkDiscovery = FindObjectOfType<NetworkDiscovery>();
_camera = GetComponentInChildren<Camera>();
_useVR = PlayerPrefs.GetInt("UseVR", 0) == 1;
networkDiscovery.ServerFoundCallback += (endPoint) =>
{
if (!_endPoints.Contains(endPoint)) _endPoints.Add(endPoint);
};
}
private void OnGUI()
{
GUILayoutOption buttonHeight = GUILayout.Height(30.0f);
using (new GUILayout.AreaScope(new Rect(Screen.width - 240.0f - 10.0f, 10.0f, 240.0f, Screen.height - 20.0f)))
{
GUILayout.Box("Settings");
using (new GUILayout.HorizontalScope())
{
bool useVRNew = GUILayout.Toggle(_useVR, "Use VR");
if (useVRNew != _useVR)
{
_useVR = useVRNew;
PlayerPrefs.SetInt("UseVR", _useVR ? 1 : 0);
Debug.Log($"UseVR set to {_useVR}");
}
}
GUILayout.Box("Server");
using (new GUILayout.HorizontalScope())
{
if (GUILayout.Button("Start", buttonHeight)) InstanceFinder.ServerManager.StartConnection();
if (GUILayout.Button("Stop", buttonHeight)) InstanceFinder.ServerManager.StopConnection(true);
}
GUILayout.Box("Advertising");
using (new GUILayout.HorizontalScope())
{
if (networkDiscovery.IsAdvertising)
{
if (GUILayout.Button("Stop", buttonHeight)) networkDiscovery.StopAdvertisingServer();
}
else
{
if (GUILayout.Button("Start", buttonHeight)) networkDiscovery.StartAdvertisingServer();
}
}
GUILayout.Box("Searching");
using (new GUILayout.HorizontalScope())
{
if (networkDiscovery.IsSearching)
{
if (GUILayout.Button("Stop", buttonHeight)) networkDiscovery.StopSearchingForServers();
}
else
{
if (GUILayout.Button("Start", buttonHeight)) networkDiscovery.StartSearchingForServers();
}
}
if (_endPoints.Count > 0)
{
GUILayout.Box("Servers");
using (new GUILayout.ScrollViewScope(_serversListScrollVector))
{
for (int i = 0; i < _endPoints.Count; i++)
{
string ipAddress = _endPoints[i].Address.ToString();
if (GUILayout.Button(ipAddress))
{
//networkDiscovery.StopAdvertisingServer();
networkDiscovery.StopSearchingForServers();
_camera.enabled = false;
InstanceFinder.ClientManager.StartConnection(ipAddress);
}
}
}
}
}
}
}
}