1
1
using System ;
2
2
using System . Threading . Tasks ;
3
+ using Unity . Multiplayer . Samples . BossRoom . Client ;
4
+ using Unity . Multiplayer . Samples . BossRoom . Shared ;
3
5
using Unity . Multiplayer . Samples . BossRoom . Shared . Net . UnityServices . Lobbies ;
4
6
using Unity . Multiplayer . Samples . Utilities ;
5
7
using Unity . Netcode ;
6
8
using Unity . Netcode . Transports . UTP ;
9
+ using Unity . Services . Authentication ;
10
+ using Unity . Services . Core ;
7
11
using UnityEngine ;
8
12
using UnityEngine . SceneManagement ;
9
13
10
14
namespace Unity . Multiplayer . Samples . BossRoom
11
15
{
12
16
public class OfflineConnectionState : ConnectionState
13
17
{
14
- LobbyServiceFacade m_LobbyServiceFacade ;
15
- LocalLobby m_LocalLobby ;
18
+ protected LobbyServiceFacade m_LobbyServiceFacade ;
19
+ protected LocalLobby m_LocalLobby ;
20
+ ProfileManager m_ProfileManager ;
16
21
17
22
/// <summary>
18
23
/// How many connections we create a Unity relay allocation for
19
24
/// </summary>
20
25
private const int k_MaxUnityRelayConnections = 8 ;
21
26
22
- public OfflineConnectionState ( ConnectionManager connectionManager , LobbyServiceFacade lobbyServiceFacade , LocalLobby localLobby )
27
+ public OfflineConnectionState ( ConnectionManager connectionManager , LobbyServiceFacade lobbyServiceFacade ,
28
+ LocalLobby localLobby , ProfileManager profileManager )
23
29
: base ( connectionManager )
24
30
{
25
31
m_LobbyServiceFacade = lobbyServiceFacade ;
26
32
m_LocalLobby = localLobby ;
33
+ m_ProfileManager = profileManager ;
27
34
}
28
35
29
36
public override void Enter ( ) { }
30
37
31
38
public override void Exit ( ) { }
32
39
33
- public override void StartClientIP ( string playerId , string playerName , string ipaddress , int port )
40
+ public override void StartClientIP ( string playerName , string ipaddress , int port )
34
41
{
35
42
var utp = ( UnityTransport ) m_ConnectionManager . NetworkManager . NetworkConfig . NetworkTransport ;
36
43
utp . SetConnectionData ( ipaddress , ( ushort ) port ) ;
37
- ConnectClient ( playerId , playerName ) ;
44
+ ConnectClient ( GetPlayerId ( ) , playerName ) ;
38
45
m_ConnectionManager . ChangeState ( ConnectionStateType . Connecting ) ;
39
46
}
40
47
41
- public override async Task StartClientLobbyAsync ( string playerName , string playerId , Action < string > onFailure )
48
+ public override async Task StartClientLobbyAsync ( string playerName , Action < string > onFailure )
49
+ {
50
+ if ( await JoinRelayServerAsync ( onFailure ) ) return ; //not re-throwing, but still not allowing to connect
51
+
52
+ ConnectClient ( GetPlayerId ( ) , playerName ) ;
53
+ m_ConnectionManager . ChangeState ( ConnectionStateType . Connecting ) ;
54
+ }
55
+
56
+ protected async Task < bool > JoinRelayServerAsync ( Action < string > onFailure )
42
57
{
43
58
Debug . Log ( $ "Setting Unity Relay client with join code { m_LocalLobby . RelayJoinCode } ") ;
44
59
@@ -48,25 +63,24 @@ public override async Task StartClientLobbyAsync(string playerName, string playe
48
63
await UnityRelayUtilities . JoinRelayServerFromJoinCode ( m_LocalLobby . RelayJoinCode ) ;
49
64
50
65
await m_LobbyServiceFacade . UpdatePlayerRelayInfoAsync ( allocationIdBytes . ToString ( ) , m_LocalLobby . RelayJoinCode ) ;
51
- var utp = ( UnityTransport ) m_ConnectionManager . NetworkManager . NetworkConfig . NetworkTransport ;
66
+ var utp = ( UnityTransport ) m_ConnectionManager . NetworkManager . NetworkConfig . NetworkTransport ;
52
67
utp . SetClientRelayData ( ipv4Address , port , allocationIdBytes , key , connectionData , hostConnectionData , isSecure : true ) ;
53
68
}
54
69
catch ( Exception e )
55
70
{
56
71
onFailure ? . Invoke ( e . Message ) ;
57
- return ; //not re-throwing, but still not allowing to connect
72
+ return true ;
58
73
}
59
74
60
- ConnectClient ( playerId , playerName ) ;
61
- m_ConnectionManager . ChangeState ( ConnectionStateType . Connecting ) ;
75
+ return false ;
62
76
}
63
77
64
- public override bool StartHostIP ( string playerId , string playerName , string ipaddress , int port )
78
+ public override bool StartHostIP ( string playerName , string ipaddress , int port )
65
79
{
66
80
var utp = ( UnityTransport ) NetworkManager . Singleton . NetworkConfig . NetworkTransport ;
67
81
utp . SetConnectionData ( ipaddress , ( ushort ) port ) ;
68
82
69
- var success = StartHost ( playerId , playerName ) ;
83
+ var success = StartHost ( GetPlayerId ( ) , playerName ) ;
70
84
if ( success )
71
85
{
72
86
m_ConnectionManager . ChangeState ( ConnectionStateType . Hosting ) ;
@@ -75,7 +89,7 @@ public override bool StartHostIP(string playerId, string playerName, string ipad
75
89
return success ;
76
90
}
77
91
78
- public override async Task StartHostLobbyAsync ( string playerId , string playerName )
92
+ public override async Task StartHostLobbyAsync ( string playerName )
79
93
{
80
94
Debug . Log ( "Setting up Unity Relay host" ) ;
81
95
@@ -99,7 +113,7 @@ public override async Task StartHostLobbyAsync(string playerId, string playerNam
99
113
throw ;
100
114
}
101
115
102
- if ( StartHost ( playerId , playerName ) )
116
+ if ( StartHost ( GetPlayerId ( ) , playerName ) )
103
117
{
104
118
m_ConnectionManager . ChangeState ( ConnectionStateType . Hosting ) ;
105
119
}
@@ -149,5 +163,15 @@ bool StartHost(string playerId, string playerName)
149
163
SetConnectionPayload ( playerId , playerName ) ;
150
164
return m_ConnectionManager . NetworkManager . StartHost ( ) ;
151
165
}
166
+
167
+ string GetPlayerId ( )
168
+ {
169
+ if ( UnityServices . State != ServicesInitializationState . Initialized )
170
+ {
171
+ return ClientPrefs . GetGuid ( ) + m_ProfileManager . Profile ;
172
+ }
173
+
174
+ return AuthenticationService . Instance . IsSignedIn ? AuthenticationService . Instance . PlayerId : ClientPrefs . GetGuid ( ) + m_ProfileManager . Profile ;
175
+ }
152
176
}
153
177
}
0 commit comments