-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPhotonVRManager.cs
More file actions
426 lines (362 loc) · 15.5 KB
/
PhotonVRManager.cs
File metadata and controls
426 lines (362 loc) · 15.5 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.VR.Player;
using Photon.Pun;
using Photon.Realtime;
using Photon.Voice;
using ExitGames.Client.Photon;
using Photon.VR.Saving;
namespace Photon.VR
{
public class PhotonVRManager : MonoBehaviourPunCallbacks
{
public static PhotonVRManager Manager { get; private set; }
[Header("Photon")]
public string AppId;
public string VoiceAppId;
[Tooltip("Please read https://doc.photonengine.com/en-us/pun/current/connection-and-authentication/regions for more information")]
public string Region = "eu";
[Header("Player")]
public Transform Head;
public Transform LeftHand;
public Transform RightHand;
public Color Colour;
public Dictionary<string, string> Cosmetics { get; private set; } = new Dictionary<string, string>();
[Header("Networking")]
public string DefaultQueue = "Default";
public int DefaultRoomLimit = 16;
[Header("Other")]
// This shuold always be true
[Tooltip("If the user shall connect when this object has awoken")]
public bool ConnectOnAwake = true;
[Tooltip("If the user shall join a room when they connect")]
public bool JoinRoomOnConnect = true;
[NonSerialized]
public PhotonVRPlayer LocalPlayer;
private RoomOptions options;
private ConnectionState State = ConnectionState.Disconnected;
private void Start()
{
if (Manager == null)
Manager = this;
else
{
Debug.LogError("There can't be multiple PhotonVRManagers in a scene");
Application.Quit();
}
DontDestroyOnLoad(gameObject);
if (ConnectOnAwake)
Connect();
if (!string.IsNullOrEmpty(PlayerPrefs.GetString("Colour")))
Colour = JsonUtility.FromJson<Color>(PlayerPrefs.GetString("Colour"));
if (!string.IsNullOrEmpty(PlayerPrefs.GetString("Cosmetics")))
Cosmetics = PhotonVRValueSaver.GetDictionary("Cosmetics");
}
#if UNITY_EDITOR
public void CheckDefaultValues()
{
bool b = CheckForRig(this);
if (b)
{
if (string.IsNullOrEmpty(AppId))
AppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdFusion;
if (string.IsNullOrEmpty(VoiceAppId))
VoiceAppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice;
Debug.Log("Attempted to set default values");
}
}
private bool CheckForRig(PhotonVRManager manager)
{
GameObject[] objects = FindObjectsOfType<GameObject>();
bool b = false;
if (manager.Head == null)
{
b = true;
foreach (GameObject obj in objects)
{
if (obj.name.Contains("Camera") || obj.name.Contains("Head"))
{
manager.Head = obj.transform;
break;
}
}
}
if (manager.LeftHand == null)
{
b = true;
foreach (GameObject obj in objects)
{
if (obj.name.Contains("Left") && (obj.name.Contains("Hand") || obj.name.Contains("Controller")))
{
manager.LeftHand = obj.transform;
break;
}
}
}
if (manager.RightHand == null)
{
b = true;
foreach (GameObject obj in objects)
{
if (obj.name.Contains("Right") && (obj.name.Contains("Hand") || obj.name.Contains("Controller")))
{
manager.RightHand = obj.transform;
break;
}
}
}
return b;
}
#endif
/// <summary>
/// Connects to Photon using the specified AppId and VoiceAppId
/// </summary>
public static bool Connect()
{
if (string.IsNullOrEmpty(Manager.AppId) || string.IsNullOrEmpty(Manager.VoiceAppId))
{
Debug.LogError("Please input an app id");
return false;
}
PhotonNetwork.AuthValues = null;
Manager.State = ConnectionState.Connecting;
PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime = Manager.AppId;
PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice = Manager.VoiceAppId;
PhotonNetwork.PhotonServerSettings.AppSettings.FixedRegion = Manager.Region;
PhotonNetwork.ConnectUsingSettings();
Debug.Log($"Connecting - AppId: {PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime} VoiceAppId: {PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice}");
return true;
}
/// <summary>
/// Connects to Photon using the specified AppId and VoiceAppId with authentication
/// </summary>
public static bool ConnectAuthenticated(string username, string token)
{
if (string.IsNullOrEmpty(Manager.AppId) || string.IsNullOrEmpty(Manager.VoiceAppId))
{
Debug.LogError("Please input an app id");
return false;
}
AuthenticationValues authentication = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
authentication.AddAuthParameter("username", username);
authentication.AddAuthParameter("token", token);
PhotonNetwork.AuthValues = authentication;
Manager.State = ConnectionState.Connecting;
PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime = Manager.AppId;
PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice = Manager.VoiceAppId;
PhotonNetwork.PhotonServerSettings.AppSettings.FixedRegion = Manager.Region;
PhotonNetwork.ConnectUsingSettings();
Debug.Log($"Connecting - AppId: {PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime} VoiceAppId: {PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice}");
return true;
}
/// <summary>
/// Disconnects from the Photon servers
/// </summary>
public void Disconnect()
{
PhotonNetwork.Disconnect();
}
/// <summary>
/// Changes Photon servers
/// </summary>
/// <param name=Id">The new AppId</param>
/// <param name="VoiceId">The new VoiceAppId</param>
public static void ChangeServers(string Id, string VoiceId)
{
PhotonNetwork.Disconnect();
Manager.AppId = Id;
Manager.VoiceAppId = VoiceId;
Connect();
}
/// <summary>
/// Changes Photon servers
/// </summary>
/// <param name=Id">The new AppId</param>
/// <param name="VoiceId">The new VoiceAppId</param>
public static void ChangeServersAuthenticated(string Id, string VoiceId, string username, string token)
{
PhotonNetwork.Disconnect();
Manager.AppId = Id;
Manager.VoiceAppId = VoiceId;
ConnectAuthenticated(username, token);
}
/// <summary>
/// Sets the Photon nickname to something
/// </summary>
/// <param name="Name">The string you want the Photon nickname to be</param>
public static void SetUsername(string Name)
{
PhotonNetwork.LocalPlayer.NickName = Name;
PlayerPrefs.SetString("Username", Name);
if (PhotonNetwork.InRoom)
if (Manager.LocalPlayer != null)
Manager.LocalPlayer.RefreshPlayerValues();
}
/// <summary>
/// Sets the colour
/// </summary>
/// <param name="PlayerColour">The colour you want the player to be</param>
public static void SetColour(Color PlayerColour)
{
Manager.Colour = PlayerColour;
ExitGames.Client.Photon.Hashtable hash = PhotonNetwork.LocalPlayer.CustomProperties;
hash["Colour"] = JsonUtility.ToJson(PlayerColour);
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
PlayerPrefs.SetString("Colour", JsonUtility.ToJson(PlayerColour));
if (PhotonNetwork.InRoom)
if (Manager.LocalPlayer != null)
Manager.LocalPlayer.RefreshPlayerValues();
}
/// <summary>
/// Sets the cosmetics
/// </summary>
/// <param name="PlayerCosmetics">The cosmetics you want to set</param>
public static void SetCosmetics(Dictionary<string, string> PlayerCosmetics)
{
Manager.Cosmetics = PlayerCosmetics;
ExitGames.Client.Photon.Hashtable hash = PhotonNetwork.LocalPlayer.CustomProperties;
hash["Cosmetics"] = Manager.Cosmetics;
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
PhotonVRValueSaver.SaveDictionary("Cosmetics", Manager.Cosmetics);
if (PhotonNetwork.InRoom)
if (Manager.LocalPlayer != null)
Manager.LocalPlayer.RefreshPlayerValues();
}
/// <summary>
/// Sets a specefic cosmetic
/// </summary>
/// <param name="Type">The type of cosmetic you want to set</param>
public static void SetCosmetic(string Type, string CosmeticId)
{
Manager.Cosmetics[Type] = CosmeticId;
ExitGames.Client.Photon.Hashtable hash = PhotonNetwork.LocalPlayer.CustomProperties;
hash["Cosmetics"] = Manager.Cosmetics;
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
PhotonVRValueSaver.SaveDictionary("Cosmetics", Manager.Cosmetics);
if (PhotonNetwork.InRoom)
if (Manager.LocalPlayer != null)
Manager.LocalPlayer.RefreshPlayerValues();
}
public override void OnConnectedToMaster()
{
State = ConnectionState.Connected;
Debug.Log("Connected");
PhotonNetwork.LocalPlayer.NickName = PlayerPrefs.GetString("Username");
PhotonNetwork.LocalPlayer.CustomProperties["Colour"] = JsonUtility.ToJson(Colour);
PhotonNetwork.LocalPlayer.CustomProperties["Cosmetics"] = Cosmetics;
if (JoinRoomOnConnect)
JoinRandomRoom(DefaultQueue, DefaultRoomLimit);
}
/// <summary>
/// Gets the connection state
/// </summary>
/// <returns>The current connection state</returns>
public static ConnectionState GetConnectionState()
{
return Manager.State;
}
/// <summary>
/// Switches scenes and joins a new room
/// </summary>
/// <param name="SceneIndex"></param>
/// <param name="MaxPlayers"></param>
public static void SwitchScenes(int SceneIndex, int MaxPlayers)
{
SceneManager.LoadScene(SceneIndex);
JoinRandomRoom(SceneIndex.ToString(), MaxPlayers);
}
/// <summary>
/// Switches scenes and joins a new room
/// </summary>
/// <param name="SceneIndex"></param>
public static void SwitchScenes(int SceneIndex)
{
SceneManager.LoadScene(SceneIndex);
JoinRandomRoom(SceneIndex.ToString(), Manager.DefaultRoomLimit);
}
/// <summary>
/// Joins a room
/// </summary>
public static void JoinRandomRoom(string Queue, int MaxPlayers) => _JoinRandomRoom(Queue, MaxPlayers);
/// <summary>
/// Joins a room
/// </summary>
public static void JoinRandomRoom(string Queue) => _JoinRandomRoom(Queue, Manager.DefaultRoomLimit);
private static void _JoinRandomRoom(string Queue, int MaxPlayers)
{
Manager.State = ConnectionState.JoiningRoom;
ExitGames.Client.Photon.Hashtable hastable = new ExitGames.Client.Photon.Hashtable();
hastable.Add("queue", Queue);
hastable.Add("version", Application.version);
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = (byte)MaxPlayers;
roomOptions.IsVisible = true;
roomOptions.IsOpen = true;
roomOptions.CustomRoomProperties = hastable;
roomOptions.CustomRoomPropertiesForLobby = new string[] { "queue", "version" };
Manager.options = roomOptions;
PhotonNetwork.JoinRandomRoom(hastable, (byte)roomOptions.MaxPlayers, MatchmakingMode.RandomMatching, null, null, null);
Debug.Log($"Joining random with type {hastable["queue"]}");
}
/// <summary>
/// Joins a private room
/// </summary>
/// <param name="RoomId">The room code</param>
/// <param name="MaxPlayers">The maximum amount of players that can be in the room</param>
public static void JoinPrivateRoom(string RoomId, int MaxPlayers) => _JoinPrivateRoom(RoomId, MaxPlayers);
/// <summary>
/// Joins a private room
/// </summary>
/// <param name="RoomId">The room code</param>
/// <param name="MaxPlayers">The maximum amount of players that can be in the room</param>
public static void JoinPrivateRoom(string RoomId) => _JoinPrivateRoom(RoomId, Manager.DefaultRoomLimit);
public static void _JoinPrivateRoom(string RoomId, int MaxPlayers)
{
PhotonNetwork.JoinOrCreateRoom(RoomId, new RoomOptions()
{
IsVisible = false,
IsOpen = true,
MaxPlayers = (byte)MaxPlayers
}, null, null);
Debug.Log($"Joining a private room: {RoomId}");
}
public override void OnJoinedRoom()
{
Debug.Log("Joined a room");
State = ConnectionState.InRoom;
}
public override void OnDisconnected(DisconnectCause cause)
{
base.OnDisconnected(cause);
State = ConnectionState.Disconnected;
Debug.Log("Disconnected from server");
}
public override void OnJoinRandomFailed(short returnCode, string message) => HandleJoinError();
private void HandleJoinError()
{
Debug.Log("Failed to join room - creating a new one");
string roomCode = CreateRoomCode();
Debug.Log($"Joining {roomCode}");
PhotonNetwork.CreateRoom(roomCode, options, null, null);
}
/// <summary>
/// Generates a random room code
/// </summary>
/// <returns>A room code</returns>
public string CreateRoomCode()
{
return new System.Random().Next(99999).ToString();
}
}
public enum ConnectionState
{
Disconnected,
Connecting,
Connected,
JoiningRoom,
InRoom
}
}