public void RegisterMatchDelegate(MatchDelegate del)
        {
            mRegisteredMatchDelegate = del;

            // Report pending matches if any.
            if (mRegisteredMatchDelegate != null && mPendingMatchDelegates != null && mPendingMatchDelegates.Count > 0)
            {
                RuntimeHelper.RunOnMainThread(() =>
                {
                    while (mPendingMatchDelegates.Count > 0)
                    {
                        var pendingDel = mPendingMatchDelegates.Dequeue();
                        mRegisteredMatchDelegate(TurnBasedMatch.FromGPGSTurnBasedMatch(pendingDel.Match), pendingDel.ShouldAutoLaunch, false);      // playerWantsToQuit doesn't occur on GPG.
                    }

                    mPendingMatchDelegates = null;
                });
            }

            // Register the delegate with GPGS.
            PlayGamesPlatform.Instance.TurnBased.RegisterMatchDelegate((match, flag) =>
            {
                if (del == null)
                {
                    return;
                }

                del(TurnBasedMatch.FromGPGSTurnBasedMatch(match), flag, false);     // playerWantsToQuit doesn't occur on GPG.
            }
                                                                       );
        }
        public void ShowMatchesUI()
        {
            PlayGamesPlatform.Instance.TurnBased.AcceptFromInbox((flag, match) =>
            {
                if (mRegisteredMatchDelegate == null)
                {
                    Debug.LogWarning("The MatchDelegate has not been registered.");
                    return;
                }

                if (match != null)
                {
                    mRegisteredMatchDelegate(TurnBasedMatch.FromGPGSTurnBasedMatch(match), true, false);       // use has accepted the invitation (wants to play), 'shouldAutoLaunch' should be true; playerWantsToQuit doesn't occur on GPG.
                }
            });
        }
        public void GetAllMatches(Action <TurnBasedMatch[]> callback)
        {
            PlayGamesPlatform.Instance.TurnBased.GetAllMatches(matches =>
            {
                if (callback == null || matches == null)
                {
                    return;
                }

                /// GPG invokes most of its callbacks on main thread, except for this one,
                /// so we have to move it to main thread on our own.
                RuntimeHelper.RunOnMainThread(() =>
                                              callback(matches.Select(match => TurnBasedMatch.FromGPGSTurnBasedMatch(match)).ToArray()));
            }
                                                               );
        }
        public void Rematch(TurnBasedMatch match, Action <bool, TurnBasedMatch> callback)
        {
            if (match == null)
            {
                throw new ArgumentNullException();
            }

            PlayGamesPlatform.Instance.TurnBased.Rematch(match.GPGS_TurnBasedMatch, (flag, param) =>
            {
                if (callback == null)
                {
                    return;
                }

                callback(flag, TurnBasedMatch.FromGPGSTurnBasedMatch(param));
            }
                                                         );
        }
        public void AcceptInvitation(Invitation invitation, Action <bool, TurnBasedMatch> callback)
        {
            if (invitation == null)
            {
                throw new ArgumentNullException();
            }

            PlayGamesPlatform.Instance.TurnBased.AcceptInvitation(
                invitation.GPGS_Invitation.InvitationId,
                (flag, match) =>
            {
                if (callback == null)
                {
                    return;
                }

                callback(flag, TurnBasedMatch.FromGPGSTurnBasedMatch(match));
            }
                );
        }
        public void CreateWithMatchmakerUI(MatchRequest request, Action cancelCallback, Action <string> errorCallback)
        {
            if (request == null)
            {
                throw new ArgumentNullException("Request can't be null.");
            }

            PlayGamesPlatform.Instance.TurnBased.CreateWithInvitationScreen(
                request.MinPlayers - 1,
                request.MaxPlayers - 1,
                (uint)GPGTypeConverter.ToGPGSVariant(request.Variant, MatchType.TurnBased),
                delegate(UIStatus uiBased, GPGS_TurnBasedMatch match)
            {
                if (uiBased == UIStatus.Valid)
                {
                    if (mRegisteredMatchDelegate == null)
                    {
                        Debug.LogWarning("The MatchDelegate has not been registered.");
                        return;
                    }

                    mRegisteredMatchDelegate(TurnBasedMatch.FromGPGSTurnBasedMatch(match), true, false);        // playerWantsToQuit doesn't occur on GPG.
                    return;
                }

                if (uiBased == UIStatus.UserClosedUI)
                {
                    if (cancelCallback != null)
                    {
                        cancelCallback();
                    }
                    return;
                }

                if (errorCallback != null)
                {
                    errorCallback(GetMessageFromUIStatus(uiBased));
                }
            }
                );
        }
        public void CreateQuickMatch(MatchRequest request, Action <bool, TurnBasedMatch> callback)
        {
            if (request == null)
            {
                throw new ArgumentNullException("Request can't be null.");
            }

            PlayGamesPlatform.Instance.TurnBased.CreateQuickMatch(
                request.MinPlayers - 1,
                request.MaxPlayers - 1,
                (uint)GPGTypeConverter.ToGPGSVariant(request.Variant, MatchType.TurnBased),
                (flag, match) =>
            {
                if (callback == null)
                {
                    return;
                }

                callback(flag, TurnBasedMatch.FromGPGSTurnBasedMatch(match));
            }
                );
        }