Skip to content

Commit 4c8dbbc

Browse files
authored
Feature/region support for photon (#234)
1 parent 7a2bed6 commit 4c8dbbc

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:47624a78617a32ba8435adf87279f7a2612deecb3a2b7da79ccdea4bcc466f06
3-
size 180973
2+
oid sha256:a06bac4969d1d9631c18d73feb0bfac049182e1c9242b784ca25806dc822fecd
3+
size 180959

Assets/BossRoom/Scripts/Client/Net/ClientGameNetPortal.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MLAPI.Transports.LiteNetLib;
77
using MLAPI.Transports.PhotonRealtime;
88
using MLAPI.Transports.UNET;
9+
using Photon.Realtime;
910

1011
namespace BossRoom.Client
1112
{
@@ -122,22 +123,38 @@ public static void StartClient(GameNetPortal portal, string ipaddress, int port)
122123
/// Relay version of <see cref="StartClient"/>, see start client remarks for more information.
123124
/// </remarks>
124125
/// <param name="portal"> </param>
125-
/// <param name="roomName">The room name of the host to connect to.</param>
126-
public static void StartClientRelayMode(GameNetPortal portal, string roomName)
126+
/// <param name="roomKey">The room name of the host to connect to.</param>
127+
public static bool StartClientRelayMode(GameNetPortal portal, string roomKey, out string failMessage)
127128
{
129+
var splits = roomKey.Split('_');
130+
131+
if (splits.Length != 2)
132+
{
133+
failMessage = "Malformatted Room Key!";
134+
return false;
135+
}
136+
137+
var region = splits[0];
138+
var roomName = splits[1];
139+
140+
128141
var chosenTransport = NetworkManager.Singleton.gameObject.GetComponent<TransportPicker>().RelayTransport;
129142
NetworkManager.Singleton.NetworkConfig.NetworkTransport = chosenTransport;
130143

131144
switch (chosenTransport)
132145
{
133146
case PhotonRealtimeTransport photonRealtimeTransport:
134147
photonRealtimeTransport.RoomName = roomName;
148+
PhotonAppSettings.Instance.AppSettings.FixedRegion = region;
135149
break;
136150
default:
137151
throw new Exception($"unhandled relay transport {chosenTransport.GetType()}");
138152
}
139153

140154
ConnectClient(portal);
155+
156+
failMessage = String.Empty;
157+
return true;
141158
}
142159

143160
private static void ConnectClient(GameNetPortal portal)

Assets/BossRoom/Scripts/Client/UI/MainMenuUI.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void Start()
3737

3838
public void OnHostClicked()
3939
{
40-
m_ResponsePopup.SetupEnterGameDisplay(true, "Host Game", "Input the IP to host on", "Input the room name to host on", "iphost", "Confirm",
40+
m_ResponsePopup.SetupEnterGameDisplay(true, "Host Game", "Input the IP to host on", "", "iphost", "Confirm",
4141
(string connectInput, int connectPort, string playerName, OnlineMode onlineMode) =>
4242
{
4343
m_GameNetPortal.PlayerName = playerName;
@@ -64,7 +64,11 @@ public void OnConnectClicked()
6464
switch (onlineMode)
6565
{
6666
case OnlineMode.Relay:
67-
ClientGameNetPortal.StartClientRelayMode(m_GameNetPortal, connectInput);
67+
if (ClientGameNetPortal.StartClientRelayMode(m_GameNetPortal, connectInput, out string failMessage) == false)
68+
{
69+
m_ResponsePopup.SetupNotifierDisplay("Connection Failed", failMessage, false, true);
70+
return;
71+
}
6872
break;
6973

7074
case OnlineMode.IpHost:

Assets/BossRoom/Scripts/Client/UI/PopupPanel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ private void OnCancelClick()
135135
/// </summary>
136136
private void OnOnlineModeDropdownChanged(int value)
137137
{
138+
// activate this so that it is always activated unless entering as relay host
139+
m_InputField.gameObject.SetActive(true);
140+
138141
if (value == 0)
139142
{
140143
// Ip host
@@ -170,6 +173,7 @@ private void OnOnlineModeDropdownChanged(int value)
170173
if (m_EnterAsHost)
171174
{
172175
m_InputField.text = GenerateRandomRoomKey();
176+
m_InputField.gameObject.SetActive(false);
173177
}
174178
else
175179
{

Assets/BossRoom/Scripts/Client/UI/RoomNameBox.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MLAPI;
22
using MLAPI.Transports;
33
using MLAPI.Transports.PhotonRealtime;
4+
using Photon.Realtime;
45
using UnityEngine;
56
using UnityEngine.Assertions;
67
using UnityEngine.UI;
@@ -11,6 +12,8 @@ public class RoomNameBox : MonoBehaviour
1112
[SerializeField]
1213
Text m_RoomNameText;
1314

15+
bool m_ConnectionFinished = false;
16+
1417
void Awake()
1518
{
1619
Assert.IsNotNull(m_RoomNameText, $"{nameof(m_RoomNameText)} not assigned!");
@@ -20,12 +23,32 @@ void Awake()
2023
switch (transport)
2124
{
2225
case PhotonRealtimeTransport realtimeTransport:
23-
m_RoomNameText.text = $"Room Name: {realtimeTransport.RoomName}";
26+
m_RoomNameText.text = $"Loading room key...";
2427
break;
2528
default:
2629
// RoomName should only be displayed when using relay.
2730
Destroy(gameObject);
2831
break;
2932
}
3033
}
34+
35+
// This update loop exists because there is currently a bug in MLAPI which runs client connected callbacks before the transport has
36+
// fully finished the asynchronous connection. That's why are loading the character select screen too early and need this update loop to
37+
// update the room key once we are fully connected to the Photon cloud.
38+
void Update()
39+
{
40+
if (m_ConnectionFinished == false)
41+
{
42+
var transport = NetworkManager.Singleton.NetworkConfig.NetworkTransport;
43+
44+
if (transport is PhotonRealtimeTransport realtimeTransport && string.IsNullOrEmpty(realtimeTransport.Client.CloudRegion) == false)
45+
{
46+
string roomName = $"{realtimeTransport.Client.CloudRegion.ToUpper()}_{realtimeTransport.RoomName}";
47+
m_RoomNameText.text = $"Room Name: {roomName}";
48+
m_ConnectionFinished = true;
49+
}
50+
}
51+
52+
53+
}
3154
}

Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
3+
"com.mlapi.contrib.transport.photon-realtime": "https://github.com/Unity-Technologies/mlapi-community-contributions.git?path=/Transports/com.mlapi.contrib.transport.photon-realtime#a918826c5dcb90f861bb9f60c6d203ac4aac3377",
34
"com.mlapi.contrib.transport.litenetlib": "https://github.com/Unity-Technologies/mlapi-community-contributions.git?path=/Transports/com.mlapi.contrib.transport.litenetlib",
4-
"com.mlapi.contrib.transport.photon-realtime": "https://github.com/Unity-Technologies/mlapi-community-contributions.git?path=/Transports/com.mlapi.contrib.transport.photon-realtime#feature/photon-realtime-transport",
55
"com.unity.2d.sprite": "1.0.0",
66
"com.unity.ai.navigation.components": "https://github.com/Unity-Technologies/NavMeshComponents.git#package",
77
"com.unity.cinemachine": "2.6.3",

Packages/packages-lock.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"hash": "398432637744f5101f79dd50f4f45d2fea8de8c0"
99
},
1010
"com.mlapi.contrib.transport.photon-realtime": {
11-
"version": "https://github.com/Unity-Technologies/mlapi-community-contributions.git?path=/Transports/com.mlapi.contrib.transport.photon-realtime#feature/photon-realtime-transport",
11+
"version": "https://github.com/Unity-Technologies/mlapi-community-contributions.git?path=/Transports/com.mlapi.contrib.transport.photon-realtime#a918826c5dcb90f861bb9f60c6d203ac4aac3377",
1212
"depth": 0,
1313
"source": "git",
1414
"dependencies": {},
15-
"hash": "49fe3c77fae1e3d326cb6dc0a175a7342ec39600"
15+
"hash": "a918826c5dcb90f861bb9f60c6d203ac4aac3377"
1616
},
1717
"com.unity.2d.sprite": {
1818
"version": "1.0.0",

0 commit comments

Comments
 (0)