mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-26 22:10:17 +00:00
implement network discovery
This commit is contained in:
parent
92bbc4bd53
commit
c3382928c2
3 changed files with 89 additions and 12 deletions
|
@ -1 +1 @@
|
|||
Subproject commit b11160df6ed8e33adca201b1ae1e7d2fb882dc41
|
||||
Subproject commit 1ac747602d897211ba2db8703172a54ee9a69a4c
|
|
@ -11,6 +11,8 @@ GameObject:
|
|||
- component: {fileID: 434938348018962339}
|
||||
- component: {fileID: 2570981519678807036}
|
||||
- component: {fileID: 4862460140142645991}
|
||||
- component: {fileID: 8720984785530044672}
|
||||
- component: {fileID: 2615086005589439192}
|
||||
m_Layer: 0
|
||||
m_Name: NetworkManager
|
||||
m_TagString: Untagged
|
||||
|
@ -116,3 +118,36 @@ MonoBehaviour:
|
|||
NoDelay: 1
|
||||
serverMaxMessageSize: 16384
|
||||
clientMaxMessageSize: 16384
|
||||
--- !u!114 &8720984785530044672
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4549513672316258300}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 65d5e9cc49a255842837a7d853db68fe, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_serverPort: 18418
|
||||
simulateResponding: 0
|
||||
gameServerPortNumber: 7777
|
||||
--- !u!114 &2615086005589439192
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4549513672316258300}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 97d06c4b188731a47baf1a65a4b8a512, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
drawGUI: 1
|
||||
offsetX: 5
|
||||
offsetY: 150
|
||||
width: 500
|
||||
height: 400
|
||||
refreshInterval: 3
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using UnityEngine;
|
||||
using SanAndreasUnity.Utilities;
|
||||
using SanAndreasUnity.Net;
|
||||
using System.Linq;
|
||||
|
||||
namespace SanAndreasUnity.UI
|
||||
{
|
||||
|
@ -11,6 +12,12 @@ namespace SanAndreasUnity.UI
|
|||
[SerializeField] string m_ipStr = "127.0.0.1";
|
||||
string m_portStr = NetManager.defaultListenPortNumber.ToString();
|
||||
|
||||
string[] m_tabNames = new string[]{"Direct", "LAN"};
|
||||
int m_currentTabIndex = 0;
|
||||
|
||||
Mirror.NetworkDiscoveryHUD m_netDiscoveryHUD;
|
||||
|
||||
|
||||
|
||||
JoinGameWindow()
|
||||
{
|
||||
|
@ -25,7 +32,13 @@ namespace SanAndreasUnity.UI
|
|||
void Start ()
|
||||
{
|
||||
// adjust rect
|
||||
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(550, 300));
|
||||
float width = Mathf.Min(650, Screen.width * 0.9f);
|
||||
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(width, 400));
|
||||
|
||||
m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow<Mirror.NetworkDiscoveryHUD>();
|
||||
m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery;
|
||||
m_netDiscoveryHUD.drawGUI = false;
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
|
@ -38,19 +51,29 @@ namespace SanAndreasUnity.UI
|
|||
protected override void OnWindowGUI ()
|
||||
{
|
||||
|
||||
m_currentTabIndex = GUIUtils.TabsControl(m_currentTabIndex, m_tabNames);
|
||||
|
||||
GUILayout.Space(20);
|
||||
|
||||
if (0 == m_currentTabIndex)
|
||||
{
|
||||
GUILayout.Label ("IP:");
|
||||
m_ipStr = GUILayout.TextField(m_ipStr, GUILayout.Width(200));
|
||||
|
||||
GUILayout.Label ("Port:");
|
||||
m_portStr = GUILayout.TextField(m_portStr, GUILayout.Width(100));
|
||||
}
|
||||
else if (1 == m_currentTabIndex)
|
||||
{
|
||||
m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30;
|
||||
m_netDiscoveryHUD.DisplayServers();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnWindowGUIAfterContent()
|
||||
{
|
||||
|
||||
GUILayout.Space(40);
|
||||
|
||||
// label with status
|
||||
string strStatus = "Disconnected";
|
||||
if (NetStatus.IsClientConnecting())
|
||||
|
@ -65,10 +88,10 @@ namespace SanAndreasUnity.UI
|
|||
}
|
||||
GUILayout.Label("Status: " + strStatus);
|
||||
|
||||
// button for connecting/disconnecting
|
||||
// button for connecting/disconnecting/refreshing LAN
|
||||
|
||||
string buttonText = "Connect";
|
||||
System.Action buttonAction = this.Connect;
|
||||
System.Action buttonAction = this.ConnectDirectly;
|
||||
if (NetStatus.IsClientConnecting())
|
||||
{
|
||||
buttonText = "Disconnect";
|
||||
|
@ -80,17 +103,36 @@ namespace SanAndreasUnity.UI
|
|||
buttonText = "Connected";
|
||||
buttonAction = () => {};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (1 == m_currentTabIndex)
|
||||
{
|
||||
GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing;
|
||||
buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN";
|
||||
buttonAction = () => m_netDiscoveryHUD.Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
if (GUIUtils.ButtonWithCalculatedSize(buttonText, 80, 30))
|
||||
buttonAction();
|
||||
|
||||
}
|
||||
|
||||
void Connect()
|
||||
void ConnectDirectly()
|
||||
{
|
||||
this.Connect(m_ipStr, ushort.Parse(m_portStr));
|
||||
}
|
||||
|
||||
void ConnectFromDiscovery(Mirror.NetworkDiscovery.DiscoveryInfo info)
|
||||
{
|
||||
this.Connect(info.EndPoint.Address.ToString(), ushort.Parse( info.KeyValuePairs[Mirror.NetworkDiscovery.kPortKey] ));
|
||||
}
|
||||
|
||||
void Connect(string ip, ushort port)
|
||||
{
|
||||
try
|
||||
{
|
||||
NetManager.StartClient(m_ipStr, ushort.Parse(m_portStr));
|
||||
NetManager.StartClient(ip, port);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue