-
Notifications
You must be signed in to change notification settings - Fork 26
/
PhotonInit.cs
182 lines (151 loc) · 5.32 KB
/
PhotonInit.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using Photon.Bolt;
using Photon.Bolt.Matchmaking;
using Photon.Bolt.Utils;
using UdpKit;
using UdpKit.Platform;
using UdpKit.Platform.Photon;
using UnityEngine;
namespace Bolt.Samples.Photon.Simple
{
public class PhotonInit : GlobalEventListener
{
// helper enum and attribute to hold which mode application is running on
enum State
{
SelectMode,
ModeServer,
ModeClient
}
State _state;
public override void BoltStartBegin()
{
BoltNetwork.RegisterTokenClass<RoomProtocolToken>();
BoltNetwork.RegisterTokenClass<ServerAcceptToken>();
BoltNetwork.RegisterTokenClass<ServerConnectToken>();
// Custom properties Token
BoltNetwork.RegisterTokenClass<PhotonRoomProperties>();
}
void Awake()
{
// Set Bolt to use Photon as transport layer
// this will connect to Photon using config values from Bolt's settings window
BoltLauncher.SetUdpPlatform(new PhotonPlatform());
// Optionally, you may want to config the Photon transport layer programatically:
// BoltLauncher.SetUdpPlatform(new PhotonPlatform(new PhotonPlatformConfig
// {
// AppId = "<your-app-id>", // your App ID
// Region = PhotonRegion.GetRegion("<region>"), // your desired region
// UsePunchThrough = true, // enable the punch through feature
// RoomUpdateRate = 5, // session update rate
// RoomCreateTimeout = 10, // timeout when creating a room
// RoomJoinTimeout = 10 // timeout when joining a room
// }));
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));
switch (_state)
{
// starting Bolt is the same regardless of the transport layer
case State.SelectMode:
if (GUILayout.Button("Start Server", GUILayout.ExpandWidth(true)))
{
BoltLauncher.StartServer();
_state = State.ModeServer;
}
if (GUILayout.Button("Start Client", GUILayout.ExpandWidth(true)))
{
BoltLauncher.StartClient();
_state = State.ModeClient;
}
break;
// Publishing a session into the matchmaking server
case State.ModeServer:
if (BoltNetwork.IsRunning && BoltNetwork.IsServer)
{
if (GUILayout.Button("Publish HostInfo And Load Map", GUILayout.ExpandWidth(true)))
{
// RoomProtocolToken token = new RoomProtocolToken()
// {
// ArbitraryData = "My DATA",
// password = "mysuperpass123"
// };
// Uncomment if you want to pass custom properties into your room
// This is just an example data
//PhotonCloudRoomProperties token = new PhotonCloudRoomProperties();
//properties.AddRoomProperty("t", 1);
//properties.AddRoomProperty("m", 4);
PhotonRoomProperties token = new PhotonRoomProperties();
token.IsOpen = true;
token.IsVisible = true;
token.AddRoomProperty("t", 1);
token.AddRoomProperty("m", 2);
string matchName = "MyPhotonGame #" + UnityEngine.Random.Range(1, 100);
BoltMatchmaking.CreateSession(
sessionID: matchName,
token: token,
sceneToLoad: "PhotonGame"
);
}
}
break;
// for the client, after Bolt is innitialized, we should see the list
// of available sessions and join one of them
case State.ModeClient:
if (BoltNetwork.IsRunning && BoltNetwork.IsClient)
{
GUILayout.Label("Session List");
foreach (var session in BoltNetwork.SessionList)
{
// Simple session
UdpSession udpSession = session.Value as UdpSession;
// Skip if is not a Photon session
if (udpSession.Source != UdpSessionSource.Photon)
continue;
// Photon Session
PhotonSession photonSession = udpSession as PhotonSession;
string sessionDescription = String.Format("{0} / {1} ({2})",
photonSession.Source, photonSession.HostName, photonSession.Id);
RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;
if (token != null)
{
sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
}
else
{
object value_t = -1;
object value_m = -1;
if (photonSession.Properties.ContainsKey("t"))
{
value_t = photonSession.Properties["t"];
}
if (photonSession.Properties.ContainsKey("m"))
{
value_m = photonSession.Properties["m"];
}
sessionDescription += String.Format(" :: {0}/{1}", value_t, value_m);
}
if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
{
ServerConnectToken connectToken = new ServerConnectToken
{
data = "ConnectTokenData"
};
BoltMatchmaking.JoinSession(photonSession, connectToken);
}
}
}
break;
}
GUILayout.EndArea();
}
public override void SessionConnectFailed(UdpSession session, IProtocolToken token, UdpSessionError error)
{
var photonSession = session as PhotonSession;
string sessionDescription = String.Format("{0} / {1} ({2})",
photonSession.Source, photonSession.HostName, photonSession.Id);
BoltLog.Warn("Failed to Connect to: {0}", sessionDescription);
}
}
}