すべてを統合する - AccelByte マルチプレイヤーサーバー (AMS) による専用サーバー - (Unity モジュール)
What's on the menu
You will reuse the Play Online Menu from Introduction to Session, so you won't change any implementation related to the user interface (UI). Instead, you are going to add additional processes to make sure you are using the server with AccelByte Multiplayer Servers (AMS).
To add AMS implementation without changing the Multiplayer Menu scripts, a script has been prepared for you to modify called MultiplayerDSAMSHelper_Starter
that subscribes to events related to changes in the Multiplayer Menus.
- MultiplayerDSAMSHelper_Starter file:
/Assets/Resources/Modules/MultiplayerDSEssentials/Scripts/MultiplayerDSAMSHelper_Starter.cs
Here are some functionalities the file provides:
Reference to
MultiplayerDSAMSWrapper_Starter
that has been initialized.private MultiplayerDSAMSWrapper_Starter amsWrapper;
private void Start()
{
#if UNITY_SERVER
amsWrapper = TutorialModuleManager.Instance.GetModuleClass<MultiplayerDSAMSWrapper_Starter>();
#endif
}An event subscription from
GameManager
that will inform when the game server de-registers.GameManager.Instance.OnDeregisterServer += delegate {};
Connect server implementation With AMS
In this section, you will go you through the process of connecting and registering your game server with AMS. Notice that you will implement the functions in reverse order from the AMS implementation flow.
Open the
MultiplayerDSAMSHelper_Starter.cs
script.Create a function to connect to DS Hub and connect to its related events. As for the required
dsId
, you can get it from theamsWrapper
getter function for theDedicatedServerId
.public void HandleDSHubConnection()
{
string dsId = amsWrapper.DedicatedServerId;
amsWrapper.ConnectToDSHub(dsId);
amsWrapper.SubscribeDSHubEvents();
}Create a new function to register the server by sending the ready message to AMS. Upon registering the server, call
HandleDSHubConnection()
to handle everything related to DSHub.private void RegisterDSToAMS()
{
BytewarsLogger.Log("[AMS] Sending ready to AMS");
amsWrapper.SendReadyMessageToAMS();
HandleDSHubConnection();
}Create another function that will help us shut the server down when the on AMS connection drains.
private void ShutdownDS()
{
BytewarsLogger.Log($"Shutting down DS..");
#if UNITY_EDITOR
UnityEditor.EditorApplication.ExitPlaymode();
#else
Application.Quit();
#endif
}Create a new function to connect to AMS and bind the functions you made earlier with their respective events.
private void HandleAMS()
{
amsWrapper.OnAMSConnectionOpened += RegisterDSToAMS;
amsWrapper.OnAMSDrainReceived += ShutdownDS;
amsWrapper.SubscribeAMSEvents();
}Prepare a callback function to inform when the login process completes. If the server successfully logs in, call the
HandleAMS()
function to handle the AMS connection.private void OnLoginServerCompleted(Result result)
{
if (!result.IsError)
{
HandleAMS();
}
}Create a new function to call the
amsWrapper
login function and bind itsResultCallback
to the function you created.private void LoginServer()
{
amsWrapper.LoginWithClientCredentials(OnLoginServerCompleted);
}Call the
LoginServer()
function inside theStart()
function. When theGameManager
invokesOnDeregisterServer
, call theShutdownDS()
to fully close the server.private void Start()
{
#if UNITY_SERVER
...
GameManager.Instance.OnDeregisterServer += ShutdownDS;
LoginServer();
#endif
}
Resources
- GitHub Link to the files in the Unity Byte Wars repository: