-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectConnect.js
executable file
·93 lines (87 loc) · 2.67 KB
/
DirectConnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//DontDestroyOnLoad(this);
var remoteIP = "127.0.0.1";
var remotePort = 25001;
var listenPort = 25000;
var remoteGUID = "";
var useNat = false;
private var connectionInfo = "";
function Awake()
{
// if (FindObjectOfType(ConnectGuiMasterServer))
// this.enabled = false;
}
function OnGUI ()
{
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if (Network.peerType == NetworkPeerType.Disconnected)
{
useNat = GUILayout.Toggle(useNat, "Use NAT punchthrough");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.BeginVertical();
if (GUILayout.Button ("Connect"))
{
if (useNat)
{
if (!remoteGUID)
Debug.LogWarning("Invalid GUID given, must be a valid one as reported by Network.player.guid or returned in a HostData struture from the master server");
else
Network.Connect(remoteGUID);
}
else
{
Network.Connect(remoteIP, remotePort);
}
}
if (GUILayout.Button ("Start Server"))
{
Network.InitializeServer(32, listenPort, useNat);
// Notify our objects that the level and the network is ready
for (var go in FindObjectsOfType(GameObject))
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
GUILayout.EndVertical();
if (useNat)
{
remoteGUID = GUILayout.TextField(remoteGUID, GUILayout.MinWidth(145));
}
else
{
remoteIP = GUILayout.TextField(remoteIP, GUILayout.MinWidth(100));
remotePort = parseInt(GUILayout.TextField(remotePort.ToString()));
}
}
else
{
if (useNat)
GUILayout.Label("GUID: " + Network.player.guid + " - ");
GUILayout.Label("Local IP/port: " + Network.player.ipAddress + "/" + Network.player.port);
GUILayout.Label(" - External IP/port: " + Network.player.externalIP + "/" + Network.player.externalPort);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button ("Disconnect"))
Network.Disconnect(200);
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
function OnServerInitialized()
{
if (useNat)
Debug.Log("==> GUID is " + Network.player.guid + ". Use this on clients to connect with NAT punchthrough.");
Debug.Log("==> Local IP/port is " + Network.player.ipAddress + "/" + Network.player.port + ". Use this on clients to connect directly.");
}
function OnConnectedToServer() {
// Notify our objects that the level and the network is ready
for (var go in FindObjectsOfType(GameObject))
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
function OnDisconnectedFromServer () {
if (this.enabled != false)
Application.LoadLevel(Application.loadedLevel);
else
FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
}