好友列表

好友列表是提高玩家社交能力的一个出色功能。 它们易于使用,并且可以使排行榜更适合你的用户。

先决条件

SDK:Unity

  • 游戏 ID 在 PlayFabSharedSettings 对象中设置。
  • 该项目可成功登录一个用户。
  • 游戏至少有两个注册用户。

关于好友

游戏中的任何玩家都可能是游戏中任何其他玩家的好友。 值得注意的是,PlayFab 中的好友关系是单向的。

如果Albert添加Bob为好友,则Bob没有审批过程。 实际上,Bob可能并不知情。

Bob 必须单独添加 Albert 才能相互 成为好友。 如果希望具有相关规则,则游戏有责任在必要时使用自定义游戏服务器或 CloudScript 逻辑强制实施这些条件。

在玩家已关联其 Steam、Facebook 或 Xbox Live 帐户的情况下,如果这些好友也玩您的游戏,则还能够显示他们的平台特定好友。

广交朋友

示例代码使用函数 DisplayFriends()DisplayError(string error) 作为应用 UI 的代理。 将这些内容粘贴到编辑器中,使其在无需任何额外工作的情况下正常工作,或将调用替换为你自己的代码。

void DisplayFriends(List<FriendInfo> friendsCache) { friendsCache.ForEach(f => Debug.Log(f.FriendPlayFabId)); }
void DisplayPlayFabError(PlayFabError error) { Debug.Log(error.GenerateErrorReport()); }
void DisplayError(string error) { Debug.LogError(error); }
  1. 玩家登录后,他们可以访问好友的 UI。 该功能通常至少包括添加、删除和显示好友。
  2. 要获取玩家的当前好友列表,请使用 GetFriendsList API 调用。
List<FriendInfo> _friends = null;

void GetFriends() {
    PlayFabClientAPI.GetFriendsList(new GetFriendsListRequest {
        IncludeSteamFriends = false,
        IncludeFacebookFriends = false,
        XboxToken = null
    }, result => {
        _friends = result.Friends;
        DisplayFriends(_friends); // triggers your UI
    }, DisplayPlayFabError);
}

GetFriendsList 结果包含一个参数 friends,这是 FriendInfo 对象的列表。

  1. 要将好友添加到玩家的好友列表,请使用 AddFriend API 调用。
enum FriendIdType { PlayFabId, Username, Email, DisplayName };

void AddFriend(FriendIdType idType, string friendId) {
    var request = new AddFriendRequest();
    switch (idType) {
        case FriendIdType.PlayFabId:
            request.FriendPlayFabId = friendId;
            break;
        case FriendIdType.Username:
            request.FriendUsername = friendId;
            break;
        case FriendIdType.Email:
            request.FriendEmail = friendId;
            break;
        case FriendIdType.DisplayName:
            request.FriendTitleDisplayName = friendId;
            break;
    }
    // Execute request and update friends when we are done
    PlayFabClientAPI.AddFriend(request, result => {
        Debug.Log("Friend added successfully!");
    }, DisplayPlayFabError);
}
  1. 要从玩家的好友列表中移除一名玩家,请使用 RemoveFriend API 调用。
// unlike AddFriend, RemoveFriend only takes a PlayFab ID
// you can get this from the FriendInfo object under FriendPlayFabId
void RemoveFriend(FriendInfo friendInfo) {
    PlayFabClientAPI.RemoveFriend(new RemoveFriendRequest {
        FriendPlayFabId = friendInfo.FriendPlayFabId
    }, result => {
        _friends.Remove(friendInfo);
    }, DisplayPlayFabError);
}

深入探索

除了添加、移除和显示外,还可以对好友执行其他操作。

标记好友

GetFriendsList 检索的 FriendInfo 对象包括好友的标记列表。 更新列表时,需要添加并从此列表中删除列表,并将其包含在 API 调用中(如下所示)。

// this REPLACES the list of tags on the server
// for updates, make sure this includes the original tag list
void SetFriendTags(FriendInfo friend, List<string> newTags)
{
    // update the tags with the edited list
    PlayFabClientAPI.SetFriendTags(new SetFriendTagsRequest
    {
        FriendPlayFabId = friend.FriendPlayFabId,
        Tags = newTags
    }, tagresult => {
        // Make sure to save new tags locally. That way you do not have to hard-update friendlist
        friend.Tags = newTags;
    }, DisplayPlayFabError);
}

可使用标记来告知匹配(例如,玩家不喜欢在困难难度下与标记为 2tuff 的好友一起玩),实现好友组 - 或者只是使用它们来存储与所需关系相关联的任何元数据。

务必注意,PlayFab 当前没有以任何方式索引这些标记。 GetFriendsList 无法根据它们进行筛选,因此必须在本地执行。

在考虑由此系统产生的任何性能影响时,请注意这一点。