从 Google Play 服务、Unity 插件获取所有回合制比赛信息

Posted

技术标签:

【中文标题】从 Google Play 服务、Unity 插件获取所有回合制比赛信息【英文标题】:Get All TurnBased Matches Info from GooglePlayServices, Unity Plugin 【发布时间】:2015-04-19 09:19:33 【问题描述】:

我使用 TicTacToss 代码作为 Unity 中基于 android 的回合制游戏的基础,使用以下代码: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】:

(更多信息@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);
    

【讨论】:

以上是关于从 Google Play 服务、Unity 插件获取所有回合制比赛信息的主要内容,如果未能解决你的问题,请参考以下文章

用于 Unity 的最新 Google Play 游戏服务插件无法正常工作

Unity Google Play 游戏插件:错误未授权

Unity Android 游戏可以使用 Google Play 游戏插件登录,但无法从排行榜加载分数

Unity Google Play 游戏插件:错误未授权代码 13

Google Play 游戏和 GoogleMobileAds 在 Unity 5 中无法协同工作

Unity AdMob 插件 2.3.1 版和 Unity Google Play 游戏插件 0.9.30 不能一起使用