Play 游戏好友功能让玩家能够创建和维护一个跨游戏的好友列表。您可以请求访问此好友列表,以帮助玩家与他们的好友一起玩您的游戏。如需详细了解好友系统,请参阅好友概念页面。
开始之前
有关实现这些 API 的最佳方式的说明,请参阅最佳实践指南。
启用好友功能
要启用好友功能,请使用以下函数:
查看好友:请求访问玩家的好友列表,以便您可以将他们的 Play 游戏好友添加到您的游戏内好友列表中。
查看玩家资料:让玩家查看另一个玩家的 Play 游戏资料。这对于玩家了解他们的朋友是谁,以及在您的游戏中与其他 Play 游戏玩家建立联系至关重要。这需要绑定到 UI 元素以触发弹出窗口。有关详细信息,请参阅好友指南。
查看好友
有两种加载好友的方法,一种是使用 ISocial
框架,另一种是直接使用 PlayGamesPlatform
。
使用 ISocial 框架加载好友
Social.localUser.LoadFriends((success) => {
Debug.Log("Friends loaded OK: " + ok));
foreach(IUserProfile p in Social.localUser.friends) {
Debug.Log(p.userName + " is a friend");
}
但是,如果当前玩家尚未授予游戏访问此信息的权限,则此调用将失败。使用 GetLastLoadFriendsStatus
检查 LoadFriends
是否因缺少同意而失败。
PlayGamesPlatform.Instance.GetLastLoadFriendsStatus((status) => {
// Check for consent
if (status == LoadFriendsStatus.ResolutionRequired) {
// Ask for resolution.
}
});
游戏可以通过调用 AskForLoadFriendsResolution
请求当前玩家分享好友列表。
PlayGamesPlatform.Instance.AskForLoadFriendsResolution((result) => {
if (result == UIStatus.Valid) {
// User agreed to share friends with the game. Reload friends.
} else {
// User doesn’t agree to share the friends list.
}
});
此函数将显示适当的平台特定好友分享 UI。此 UI 会询问玩家是否要将他们的好友分享给游戏。
使用 PlayGamesPlatform 加载好友
另一种加载好友的方法是使用 LoadFriends
和 LoadMoreFriends
PlayGamesPlatform.Instance.LoadFriends(pageSize, forceReload, (status) => {
// Check if the call is successful and if there are more friends to load.
});
PlayGamesPlatform.Instance.LoadMoreFriends(pageSize, (status) => {
// Check if there are more friends to load.
});
pageSize
参数表示此页面请求的条目数量。请注意,如果缓存数据已存在,则返回的缓冲区可能包含比此大小更多的条目。如果集合包含足够的记录,则缓冲区保证至少包含这么多条目。如果 forceReload
设置为 true
,则此调用将清除任何本地缓存的数据并尝试从服务器获取最新数据。这通常用于用户发起刷新等操作。通常,应将其设置为 false
以获得数据缓存的优势。
如果回调返回 LoadFriendsStatus.LoadMore
,则有更多好友可供加载。LoadFriendsStatus.ResolutionRequired
表示用户尚未分享好友列表,您可以直接调用 PlayGamesPlatform.Instance.AskForLoadFriendsResolution
。
确定好友列表可见性
使用 PlayGamesPlatform.Instance.GetFriendsListVisibility
检查用户是否已与游戏分享好友列表。可能的返回状态是:
FriendsListVisibilityStatus.RequestRequired
表示您必须请求同意。FriendsListVisibilityStatus.Visible
表示加载好友列表应该成功。FriendsListVisibilityStatus.Unknown
通常不应该发生。您可以将forceReload
设置为 true 以刷新数据。
PlayGamesPlatform.Instance.GetFriendsListVisibility(forceReload, (friendsListVisibilityStatus) => {});
查看玩家资料
要添加或删除好友,请使用显示和比较资料函数。此函数会触发一个底部对话框,显示用户的 Play 游戏资料;使用请求玩家的玩家 ID 调用该函数。如果玩家和好友有游戏内昵称,请在调用中使用它们,以便为资料 UI 添加更多上下文。
PlayGamesPlatform.Instance.ShowCompareProfileWithAlternativeNameHintsUI(
mFirstFriendId, /* otherPlayerInGameName= */ null, /* currentPlayerInGameName= */ null,
(result) => {
// Profile comparison view has closed.
});