从Google Play服务,Unity插件获取基于所有回合制的匹配信息

时间:2015-02-18 11:28:58

标签: c# unity3d google-play-games

我正在使用TicTacToss代码作为Unity中基于回合制游戏的基础,使用:https://github.com/playgameservices/play-games-plugin-for-unity

一切都运行良好,但没有任何功能可以返回当前用户参与,活动和完成的游戏列表。我需要这个用于主UI,所以我可以向用户显示他们正在玩什么游戏,对谁等等。没有,这个API似乎很缺乏。

有没有人能够实现这一目标?在我看来,可能的功能在那里,因为有一个名为GetAllTurnbasedMatches()的函数,但我的C#不够强大,无法理解它是如何工作的。 (参见https://github.com/playgameservices/play-games-plugin-for-unity/blob/master/source/PluginDev/Assets/GooglePlayGames/Platforms/Native/PInvoke/TurnBasedManager.cs

1 个答案:

答案 0 :(得分:1)

(更多信息@ https://github.com/playgameservices/play-games-plugin-for-unity/issues/414

我设法让这项工作将以下内容添加到NativeTurnBasedMultiplayerClient.cs中:

public void GetAllTBMatches(Action<TurnBasedMatch, TurnBasedMatch.MatchTurnStatus> callback)
{
    mTurnBasedManager.GetAllTurnbasedMatches(allMatches =>
    {
        foreach (var match in allMatches.MatchesMyTurn())
        {
            var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
            Logger.d("MY turn : Passing converted match to user callback:" + converted);
            callback(converted, TurnBasedMatch.MatchTurnStatus.MyTurn);
        }
        foreach (var match in allMatches.MatchesTheirTurn())
        {
            var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
            Logger.d("Their Turn : Passing converted match to user callback:" + converted);
            callback(converted, TurnBasedMatch.MatchTurnStatus.TheirTurn);
        }
        foreach (var match in allMatches.MatchesCompleted())
        {
            var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
            Logger.d("Completed : Passing converted match to user callback:" + converted);
            callback(converted, TurnBasedMatch.MatchTurnStatus.Complete);
        }

        callback(null, TurnBasedMatch.MatchTurnStatus.Unknown);
    });
}

public void GetAllTBInvitations(Action<Invitation> callback)
{
    mTurnBasedManager.GetAllTurnbasedMatches(allMatches =>
    {
        foreach (var invitation in allMatches.Invitations())
        {
            var converted = invitation.AsInvitation();
            Logger.d("Passing converted invitation to user callback:" + converted);
            callback(converted);
        }

        callback(null);
    });
}

您还需要在TurnBasedManager.cs中编辑以下内容

internal class TurnBasedMatchesResponse : BaseReferenceHolder {
    internal TurnBasedMatchesResponse(IntPtr selfPointer) : base(selfPointer) {
    }

    protected override void CallDispose(HandleRef selfPointer) {
        C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_Dispose(SelfPtr());
    }

    internal CommonErrorStatus.MultiplayerStatus Status() {
        return C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetStatus(SelfPtr());
    }

    internal IEnumerable<MultiplayerInvitation> Invitations()
    {
        return PInvokeUtilities.ToEnumerable(
            C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetInvitations_Length(SelfPtr()),
            index => new MultiplayerInvitation(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetInvitations_GetElement(SelfPtr(), index)));
    }

    internal IEnumerable<NativeTurnBasedMatch> MatchesMyTurn()
    {
        return PInvokeUtilities.ToEnumerable(
            C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetMyTurnMatches_Length(SelfPtr()),
            index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetMyTurnMatches_GetElement(SelfPtr(), index)));
    }

    internal IEnumerable<NativeTurnBasedMatch> MatchesTheirTurn()
    {
        return PInvokeUtilities.ToEnumerable(
            C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetTheirTurnMatches_Length(SelfPtr()),
            index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetTheirTurnMatches_GetElement(SelfPtr(), index)));
    }

    internal IEnumerable<NativeTurnBasedMatch> MatchesCompleted()
    {
        return PInvokeUtilities.ToEnumerable(
            C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetCompletedMatches_Length(SelfPtr()),
            index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetCompletedMatches_GetElement(SelfPtr(), index)));
    }

    internal static TurnBasedMatchesResponse FromPointer(IntPtr pointer)
    {
        if (PInvokeUtilities.IsNull(pointer)) {
            return null;
        }

        return new TurnBasedMatchesResponse(pointer);
    }
}