简体   繁体   English

Unity Google Play游戏创建带有邀请问题的游戏

[英]Unity Google Play Games Create game with invitation problem

I can create games with invites, I can invite and accept invitations, the thing mostly works fine. 我可以创建带有邀请的游戏,可以邀请和接受邀请,大多数情况下都可以正常工作。 The problem appears when I make a game for 3 or more people: 当我为3个或更多的人制作游戏时出现问题:

  • user A creates game with 3 people or 2 people at least 用户A创建的游戏人数至少为3个人或2个人
  • user A chooses to invite someone and then adds another person to auto-pick for the room 用户A选择邀请某人,然后添加另一个人以自动选择房间
  • User A invites user B 用户A邀请用户B
  • User B accepts 用户B接受
  • They both wait until the "Start now" button appears 他们俩都等到“立即开始”按钮出现
  • User B presses the start now button and OnRoomConnected() is called 3 times for some reason and the game doesn't start (the room was also never left as far as I can tell, because this user can't receive invitations anymore) 用户B按下“立即开始”按钮,由于某种原因,OnRoomConnected()被调用了3次,游戏也无法启动(据我所知,该房间也从未离开过,因为该用户不再能收到邀请了)
  • Nothing changes from the perspective of user A, he still waits for the game to start or search for another auto pick opponent 从用户A的角度来看,没有任何变化,他仍然等待游戏开始或搜索另一个自动选择对手

I made sure that the problem is not from my code. 我确保问题不是来自我的代码。 I created a separate simple project, that I used only for testing purposes and it does exactly the same thing. 我创建了一个单独的简单项目,该项目仅用于测试目的,并且功能完全相同。 So I was starting to think maybe it's not a problem from my side and I didn't see similar problems on the internet. 因此,我开始认为也许这不是我的问题,并且我在互联网上没有看到类似的问题。 So I decided to ask here. 所以我决定在这里问。 What should I do? 我该怎么办? What could be the problem? 可能是什么问题呢?

That's basically it. 基本上就是这样。 Even if I restrict the number of players to 3 or 4 (min and max number of players are equal, 3 or 4), it still lets me start the game prematurely and I have the same problem with OnRoomConnected() being called multiple times and the game doesn't start. 即使我将玩家数限制为3或4(最小和最大玩家数相等,即3或4),它仍然让我过早地开始游戏,并且我遇到了相同的问题,即多次调用OnRoomConnected()并游戏无法开始。

Thanks in advance. 提前致谢。 If you have a link or something that would help me solve this problem, it would be greatly appreciated. 如果您有链接或其他可以帮助我解决此问题的内容,将不胜感激。

Here's the basic code I used for logging in the game and creating a room. 这是我用于登录游戏并创建房间的基本代码。

public class GPGM : MonoBehaviour, RealTimeMultiplayerListener{

public static GPGM instance;
public static int target;

private void Awake()
{
    target = 60;
    QualitySettings.vSyncCount = 0;
    Application.targetFrameRate = target;

    if (instance == null)
    {
        DontDestroyOnLoad(gameObject);
        instance = this;
    }
    else if (instance != this)
    {
        Destroy(gameObject);
    }
}
// Use this for initialization
void Start () {
    Login();
}

// Update is called once per frame
void Update () {

}

public void Login()
{
    StartCoroutine(checkInternetConnection((isConnected) =>
    {
        LoginGPG();
    }));
}

IEnumerator checkInternetConnection(Action<bool> action)
{
    WWW www;
    www = new WWW("http://google.com");

    yield return www;

    if (!String.IsNullOrEmpty(www.error))
    {
        Debug.Log("DebugM | no internet connection");
        action(false);
    }
    else
    {
        Debug.Log("DebugM | There IS connection");
        action(true);
    }
}

public void LoginGPG()
{
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().WithInvitationDelegate(OnInvitationReceived).Build();
    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
    Debug.Log("DebugM | LoginGPG");
    Auth();

}

public void Auth()
{
    Debug.Log("DebugM | Auth");
    try
    {
        //doesn't work sometimes for some reason. It gives null data if success is false
        //reason for false success is unknown
        Social.localUser.Authenticate((bool succes) =>
        {
            if (succes)
            {
                Debug.Log("DebugM | Logged in");
            }else
            {
                Debug.Log("DebugM | authentication failed");
            }
        });
    }
    catch (Exception e)
    {
        Debug.Log("DebugM | Auth() has failed with error: " + e.Message);
    }
}

public void OnInvitationReceived(Invitation invitation, bool shouldAutoAccept)
{
    StartCoroutine(InvitationCo(invitation, shouldAutoAccept));
}

Invitation mIncomingInvitation;
IEnumerator InvitationCo(Invitation invitation, bool shouldAutoAccept)
{
    yield return new WaitUntil(() => SceneManager.GetActiveScene().name == "Lobby");

    Debug.Log("DebugM | Invitation has been received!!!");
    //StartCoroutine(LM.LoadingAnim());
    if (shouldAutoAccept)
    {
        Debug.Log("DebugM | Should auto accept: TRUE");
        PlayGamesPlatform.Instance.RealTime.AcceptInvitation(invitation.InvitationId, instance);

    }
    else
    {
        // The user has not yet indicated that they want to accept this invitation.
        // We should *not* automatically accept it. Rather we store it and 
        // display an in-game popup:
        Debug.Log("DebugM | Should auto accept: FALSE");
        Lobby LM = FindObjectOfType<Lobby>();
        LM.invPanel.SetActive(true);
        mIncomingInvitation = invitation;
    }
}

public void AcceptGoogleInv(GameObject panel)
{
    if (mIncomingInvitation != null)
    {
        // show the popup
        //string who = (mIncomingInvitation.Inviter != null &&
        //    mIncomingInvitation.Inviter.DisplayName != null) ?
        //        mIncomingInvitation.Inviter.DisplayName : "Someone";
        Debug.Log("DebugM | Invitation has been accepted");
        PlayGamesPlatform.Instance.RealTime.AcceptInvitation(mIncomingInvitation.InvitationId, instance);
        panel.SetActive(false);
    }
}

public void CreateQuickRoom()
{
                PlayGamesPlatform.Instance.RealTime.CreateWithInvitationScreen(1, 3, 1, instance );
}

public void OnRoomSetupProgress(float percent)
{
    Debug.Log("OnRoomSetupProgress()");
    PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();
}

public void OnRoomConnected(bool success)
{
    SceneManager.LoadScene("Game");
    Debug.Log("DebugM | Room conected");
}

public void OnLeftRoom()
{
    throw new NotImplementedException();
}

public void OnParticipantLeft(Participant participant)
{
    throw new NotImplementedException();
}

public void OnPeersConnected(string[] participantIds)
{
    throw new NotImplementedException();
}

public void OnPeersDisconnected(string[] participantIds)
{
    throw new NotImplementedException();
}

public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
{
    throw new NotImplementedException();
}}

Edit (pictures): This is when I wait for the last auto pick slot to fill (it works like this only when people were invited to the game) 编辑(图片): 这是当我等待最后一个自动选择插槽填满时(仅当邀请人们参加游戏时,它才这样工作)

The game goes to lobby for the person who pressed start and the others still wait for the last autopick even if 1 player practically left the room 游戏会进入按下开始按钮的人的大厅,而其他人仍在等待最后一次自动选择,即使实际上有一位玩家离开了房间

You can do it like: 您可以这样做:

public void OnRoomConnected (bool success)
   {
     if (success)
      {
        //Start the game here
        SceneManager.LoadScene("Game");
        Debug.Log("DebugM | Room conected");
      }
     else
      {
        //Do somthing else.
      }
   }

or the best way to do it by checking connected participans count. 或通过检查连接的参与数来做到这一点的最佳方法。

public void OnPeersConnected (string[] participantIds)
    {
      List<Participant> playerscount = PlayGamesPlatform.Instance.RealTime.GetConnectedParticipants();
      if (playerscount != null && playerscount.Count > 1)//this condition should be decided by you.
        {
          //Start the game here
          SceneManager.LoadScene("Game");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM