调用 PgosSDK 接口
PgosSDK
提供了一组用于访问 PGOS 服务的 API。某些 API 需要在其他 API 之前调用,也就是说,您需要先调用 CreatePGOS
、InitConfig
、LoginPGOS
,这些将为其他 API 调用准备工作环境。详情请点击编程快速入门。
在调用
LoginPGOS
之前可以调用FakeAccount
接口 API,FakeAccount
接口用于 FAS 登录。完成本教程后,您将了解:
- 如何调用
PgosSDK
API。
1. 获取服务接口
所有服务接口都可以在 PgosClientSDK.cs
和 PgosServerSDK.cs
中找到:
// PgosClientSDK.cs
// Client API
public class PgosClientSDK
{
/** Get PgosClientSDK singleton instance */
public static PgosClientSDK Get();
/** Create and Destroy PGOS in the main thread */
public void CreatePGOS();
public void DestroyPGOS();
public Client.ClientSDK GetClientSDKAPI(); // Client global interface
public Client.Economy GetEconomyAPI(); // Economy interface
public Client.FakeAccount GetFakeAccountAPI(); // FakeAccount interface
public Client.PlayerAuth GetPlayerAuthAPI(); // PlayerAuth interface
...
}
// PgosServerSDK.cs
// Server API
public class PgosClientSDK
{
/** Get PgosServerSDK singleton instance */
public static PgosServerSDK Get();
/** Create and Destroy PGOS in the main thread */
public void CreatePGOS();
public void DestroyPGOS();
public Server.ServerSDK GetServerSDKAPI(); // Server global interface
public Server.Battle GetBattleAPI(); // Battle interface
public Server.Economy GetEconomyAPI(); // Economy interface
public Server.Hosting GetHostingAPI(); // Hosting interface
...
}
要获取相应的服务实例,只需调用相应的Get*API()
函数。例如,以下代码将获取FakeAccount
的服务实例:
var clientAPI = PgosClientSDK.Get().GetClientSDKAPI();
获取服务实例后,您可以调用其所有服务 API,这些 API 可以在文件 "BusinessModules/Pgos{ServiceName}API.cs"
中找到。
2. 调用API
PgosSDK
中包含同步和异步API,通常来说,带有回调参数的API为异步,不带回调参数的API为同步。异步API的回调会在游戏线程中执行。以下是FAS登录功能的API声明示例:
public delegate void OnLogin(PgosResult ret, FasLoginInfo data);
/**
* Log in with the specified account.
* The FAS account does not need to be registered in advance, it can be any string without line breaks ('\r', '\n') and is managed by the game.
*
* @param account FAS account, if the account is empty, then will use a local historical account.
* @param callback The result callback after the API execution ends, and it will be called in the GAME THREAD. For lifetime safety, it is recommended to use the CreateWeakCallback provided in PgosSDKCpp.h to create a lambda bound to an UObject.
*/
public void Login(string account, OnLogin callback)
{
...
}
3. 监听事件
当发生某些情况时,PgosSDK
会触发特定事件,例如玩家会话状态变更、玩家加入/离开组队等。这些事件在游戏线程中执行。开发者可以监听这些事件并采取相应的操作(如更新UI)。以PlayerAuth
服务的OnPlayerSessionChanged
事件为例,您可以参考以下代码来监听PgosSDK
事件:
void MonitorPlayerSessionEvt()
{
var playerAuthAPI = PgosClientSDK.Get().GetPlayerAuthAPI();
playerAuthAPI.SetOnPlayerSessionChanged((Pgos.Client.PlayerSessionChangedEvt pgos_event) =>
{
Debug.Log("pgos player session has changed.");
});
}
❗ 注意
- 为了避免遗漏事件,建议在事件触发前就开始监听。
- 异步 API 回调和触发的事件都在游戏线程中执行,因此不建议在其中进行任何耗时操作。