-
Notifications
You must be signed in to change notification settings - Fork 59
/
ColocationTestBootStrapper.cs
121 lines (95 loc) · 3.92 KB
/
ColocationTestBootStrapper.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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Discover.Networking;
using Fusion;
using Fusion.Sockets;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
namespace Discover.Colocation.Test
{
public class ColocationTestBootStrapper : MonoBehaviour, INetworkRunnerCallbacks
{
[SerializeField] private NetworkRunner m_networkRunner;
[SerializeField] private NetworkSceneManagerDefault m_sceneManager;
[SerializeField] private NetworkObject m_colocationPrefab;
public UnityEvent OnConnectionStarted;
public UnityEvent<string> OnNetworkEvent;
private void Awake()
{
m_networkRunner.AddCallbacks(this);
m_networkRunner.ProvideInput = true;
}
public void StartHost()
{
StartConnection(true);
}
public void StartClient()
{
StartConnection(false);
}
public async void StartConnection(bool isHost)
{
OnConnectionStarted?.Invoke();
OnNetworkEvent?.Invoke("Connecting to Photon...");
ColocationDriverNetObj.OnColocationCompletedCallback += OnColocationReady;
await Connect(isHost);
}
private async Task Connect(bool isHost)
{
var args = new StartGameArgs()
{
GameMode = GameMode.Shared,
SessionName = "ColocationTest",
Scene = SceneManager.GetActiveScene().buildIndex,
SceneManager = m_sceneManager
};
_ = await m_networkRunner.StartGame(args);
}
private void OnColocationReady(bool success)
{
if (success)
{
OnNetworkEvent?.Invoke("Colocation Ready");
}
else
{
OnNetworkEvent?.Invoke("Joined Remotely");
}
}
#region INetworkRunnerCallbacks
public void OnPlayerJoined(NetworkRunner runner, PlayerRef player)
{
Debug.Log($"OnPlayerJoined playerRef: {player}");
}
public void OnPlayerLeft(NetworkRunner runner, PlayerRef player)
{
Debug.Log($"OnPlayerLeft playerRef: {player}");
}
public void OnInput(NetworkRunner runner, NetworkInput input) { }
public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input) { }
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason) { }
public void OnConnectedToServer(NetworkRunner runner)
{
OnNetworkEvent?.Invoke("Connected To Photon");
if (m_networkRunner.IsMasterClient())
{
Debug.Log("Spawn Colocation Prefab");
_ = m_networkRunner.Spawn(m_colocationPrefab);
}
}
public void OnDisconnectedFromServer(NetworkRunner runner) { }
public void OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request, byte[] token) { }
public void OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason) { }
public void OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message) { }
public void OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList) { }
public void OnCustomAuthenticationResponse(NetworkRunner runner, Dictionary<string, object> data) { }
public void OnHostMigration(NetworkRunner runner, HostMigrationToken hostMigrationToken) { }
public void OnReliableDataReceived(NetworkRunner runner, PlayerRef player, ArraySegment<byte> data) { }
public void OnSceneLoadDone(NetworkRunner runner) { }
public void OnSceneLoadStart(NetworkRunner runner) { }
#endregion // INetworkRunnerCallbacks
}
}