当用户点击谷歌玩游戏实时多人自动匹配用户界面时该怎么办?
Posted
技术标签:
【中文标题】当用户点击谷歌玩游戏实时多人自动匹配用户界面时该怎么办?【英文标题】:What to do when user clicks back on google play games realtime multiplayer auto-match ui? 【发布时间】:2015-10-12 10:33:15 【问题描述】:我创建了一个“播放按钮”,它启动自动匹配 UI 并开始搜索玩家。当 UI 出现时,当我按下 自动 UI 后退按钮 时,它会返回回到“播放按钮”....但是当我再次按下它时它什么也不做。 当我在自动匹配 UI 中按 quit 然后再次按 play 时,它会起作用
当有人在 ui 中按下后退按钮时,我应该在代码中做什么(我正在使用 Google 玩游戏 Unity 插件)?
我正在使用 Google play 游戏服务统一插件
【问题讨论】:
当你按下返回按钮时,你需要调用PlayGamesPlatform.Instance.RealTime.LeaveRoom();
,你可以这样称呼它:当按下返回按钮时if(onRoomConnected) PlayGamesPlatform.Instance.RealTime.LeaveRoom();
onRoomConnected 是一个布尔值,默认为假,你需要将它设置为当 OnRoomConnected(bool success) 方法返回成功时为 true。
【参考方案1】:
我不确定您的代码是什么样的,但这是我创建实时游戏的最小示例。我认为您的问题是每当您“返回”播放按钮时,都需要调用 LeaveRoom() 来重置多人房间状态。
这是基于 GPGS Unity 插件项目 (https://github.com/playgameservices/play-games-plugin-for-unity) 中的 SmokeTest 示例。
所有代码(包括此示例都包含在Apache 2 license 中。
using UnityEngine;
using GooglePlayGames.BasicApi.Multiplayer;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
class SampleRTMP : MonoBehaviour, RealTimeMultiplayerListener
bool canStartPlaying = true;
bool playingGame = false;
void Start()
PlayGamesClientConfiguration config =
new PlayGamesClientConfiguration.Builder()
// registers a callback to handle game invitations
// received while the game is not running.
.WithInvitationDelegate(OnInvitation)
.Build();
PlayGamesPlatform.InitializeInstance(config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate();
// Auto login
Social.localUser.Authenticate((bool success) =>
// handle success or failure
);
void OnGUI()
if (playingGame)
// don't show the buttons during game play.
return;
if (canStartPlaying)
if (GUI.Button(new Rect(10, Screen.height / 4f, Screen.height / 8f, Screen.width / 3f), "Play"))
canStartPlaying = false;
ulong bitmask = 0L; // no special bitmask for players
uint minOpponents = 1; // so the smallest is a 2 player game
uint maxOpponents = 1; // so the largest is a 2 player game
PlayGamesPlatform.Instance.RealTime.CreateQuickGame(minOpponents,
maxOpponents, 0, bitmask, this);
else
if (GUI.Button(new Rect(10, Screen.height / 4f, Screen.height / 12f, 100), "Leave Room"))
PlayGamesPlatform.Instance.RealTime.LeaveRoom();
if (GUI.Button(new Rect(350, Screen.height / 4f, Screen.height / 12f, 100), "Show Waiting Room"))
PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();
public void OnInvitation(Invitation invitation, bool shouldAutoAccept)
// handle the invitation
if (shouldAutoAccept)
PlayGamesPlatform.Instance.RealTime.AcceptInvitation(invitation.InvitationId, this);
else
// do some other stuff.
// Callbacks for Room setup
public void OnRoomSetupProgress(float percent)
// called once initially, then each time a player joins or leaves.
// or if the waiting room activity returns.
Debug.Log("Setting up room (" + ((int)percent) + "%)");
public void OnRoomConnected(bool success)
Debug.Log("Connected to room: " + success);
if (success)
// Start playing the game!
playingGame = success;
else
// Handle error here, then leave the room.
PlayGamesPlatform.Instance.RealTime.LeaveRoom();
public void OnLeftRoom()
Debug.Log("Left the room");
canStartPlaying = true;
public void OnParticipantLeft(Participant participant)
public void OnPeersConnected(string[] participantIds)
public void OnPeersDisconnected(string[] participantIds)
//Do stuff
public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
//Do stuff
【讨论】:
当正在寻找其他玩家开始游戏的玩家按下返回按钮时调用哪个方法?以上是关于当用户点击谷歌玩游戏实时多人自动匹配用户界面时该怎么办?的主要内容,如果未能解决你的问题,请参考以下文章
来自谷歌服务的实时多人游戏问题(为您的房间配置添加专属位掩码)