Use this github example to modify:
Connection process:
Mainly after matching two players into the room through UGS MatchMaker,
The room will actually create two Network objects,
Respectively a Server and a Client,
Indicates that the player has been matched into the room,
So when I create a Network object,
Let both Server and Client create the same session through Fusion,
Server and Client connect through Fusion.
(The Network object mentioned in the content, here refers to the NO of UGS)
It is feasible to test the current process locally,
But after the server is placed in UGS and started,
Also make sure Fusiton is running correctly on UGS,
For unknown reasons, currently the Server and Client cannot connect with Fusion.
I suspect it is the firewall settings,
Does UGS need to set up a firewall?
FusionCode
Photon_Manager.cs
public void ServerMode()
{
StartServer();
//StartSimulation("TestSession", "jp");
Debug.Log("Server start");
}
public void ClientMode()
{
StartClient();
Debug.Log("Client start");
}
async void StartServer()
{
_runner = gameObject.AddComponent<NetworkRunner>();
_runner.ProvideInput = true;
Debug.Log("open TestSession");
var photonSettings = PhotonAppSettings.Instance.AppSettings.GetCopy();
photonSettings.FixedRegion = "ASIA".ToLower();
Debug.Log(photonSettings.FixedRegion + "Regional connection");
var result = await _runner.StartGame(new StartGameArgs()
{
GameMode = GameMode.Server,
SessionName = "TestSession",
//Scene = SceneManager.GetActiveScene().buildIndex,
SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>(),
CustomPhotonAppSettings = photonSettings,
});
if (result.Ok == false) { Debug.LogWarning("FusionServer fail: "+ result.ShutdownReason); }
else { Debug.Log("FusionServer success"); }
}
async void StartClient()
{
_runner = gameObject.AddComponent<NetworkRunner>();
_runner.ProvideInput = true;
Debug.Log("open TestSession");
var result = await _runner.StartGame(new StartGameArgs()
{
GameMode = GameMode.Client,
SessionName = "TestSession",
//Scene = SceneManager.GetActiveScene().buildIndex,
SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>(),
});
if (result.Ok == false) { Debug.LogWarning("FusionClient fail: " + result.ShutdownReason); }
else { Debug.Log("FusionClient success"); }
}
MatchMakerCode
using Matchplay.Client;
using Matchplay.Networking;
using Matchplay.Shared;
using Matchplay.Shared.Tools;
using Unity.Netcode;
using UnityEngine;
namespace Matchplay.Server
{
/// <summary>
/// Currently there is no control for moving the player around, only the server does.
/// The NetworkManager spawns this in automatically, as it is on the designated player object.
/// </summary>
public class Matchplayer : NetworkBehaviour
{
[HideInInspector]
public NetworkVariable<Color> PlayerColor = new NetworkVariable<Color>();
[HideInInspector]
public NetworkVariable<NetworkString> PlayerName = new NetworkVariable<NetworkString>();
[SerializeField]
RendererColorer m_ColorSwitcher;
public override void OnNetworkSpawn()
{
if (IsServer && !IsHost)
{
Photon_Manager.Instance.ServerMode(); //PhotonServer connect
}
if (IsServer && !IsHost)
return;
if (IsOwner && IsClient && !IsHost)
{
Photon_Manager.Instance.ClientMode(); //PhotonClient connect
}
SetColor(Color.black, PlayerColor.Value);
PlayerColor.OnValueChanged += SetColor;
ClientSingleton.Instance.Manager.AddMatchPlayer(this);
}
void SetColor(Color oldColor, Color newColor)
{
if (oldColor == newColor)
return;
m_ColorSwitcher.SetColor(newColor);
}
public override void OnNetworkDespawn()
{
if (IsServer && !IsHost)
return;
if (ApplicationData.IsServerUnitTest)
return;
ClientSingleton.Instance.Manager.RemoveMatchPlayer(this);
}
}
}