简体   繁体   English

Unity Google Play游戏实时消息未发送

[英]Unity google play games realtime message not send

So i'm currently working on a game with unity, it's an air hockey. 因此,我目前正在开发一款具有统一性的游戏,这是一款曲棍球。 And i would like to add a multiplayer mode (1 vs 1). 我想添加一个多人游戏模式(1比1)。

So after the player have a match with an opponent, they are connected in a room where the game begins. 因此,在玩家与对手进行比赛之后,他们将在游戏开始的房间中被连接。

But i'm having issue when i want to receive the opponent message. 但是当我想接收对方的消息时遇到问题。

In the player script movement, i add this on the void update() 在播放器脚本移动中,我将此添加到void update()上

Multiplayer.Instance.SendMyUpdate(positionX, PositionZ);

and in the multiplayer script, i add this: 在多人脚本中,我添加以下内容:

public void SendMyUpdate(float posX, float posZ) {

    string PlayerUserNameString = PlayGamesPlatform.Instance.RealTime.GetSelf ().DisplayName;
    char[] characters = PlayerUserNameString.ToCharArray ();
    string data = characters + ":" + posX + ":" + posZ;
    byte[] bytedata = System.Text.ASCIIEncoding.Default.GetBytes (data);
    PlayGamesPlatform.Instance.RealTime.SendMessageToAll (false, bytedata);
}

And on the method OnRealTimeMessageReceived: 并在方法OnRealTimeMessageReceived上:

string rawdata = System.Text.ASCIIEncoding.Default.GetString (data);
string[] cut = rawdata.Split (new string[] { ":" }, System.StringSplitOptions.RemoveEmptyEntries);

OpponentUserName = System.Convert.ToSingle (cut[1]).ToString();

Transform target = GameObject.Find ("mallet Opponent").transform;

        Vector3 newpos = new Vector3
        (
                System.Convert.ToSingle(cut[2]),
                0,
                System.Convert.ToSingle(cut[3])
        );

After i wrote this and build it on two devices, when the room is connected and the game begins, the opponent player doesn't move at all, and i don't know where the issue. 在我编写并在两个设备上构建它之后,当房间连接起来并且游戏开始时,对手玩家根本不会动,而且我也不知道问题出在哪里。

Any ideas? 有任何想法吗?

Arrays are zero based so opponent user name should be cut[0] and: 数组是从零开始的,因此对手用户名应该被切[0]并:

System.Convert.ToSingle(cut[3])

cut[3] will look for the 4th split result and there are only three so an exception will occur. cut [3]将查找第4个分割结果,并且只有三个,因此将发生异常。 That is enough to prevent the method working and the position updating. 这足以防止方法工作和位置更新。 If so there should also be an error in the console log. 如果是这样,控制台日志中也应该有一个错误。

However perhaps the OnRealTimeMessageReceived is not even being called. 但是,也许甚至没有调用OnRealTimeMessageReceived。 You need to throw in some breakpoints and debug, or add some debug logs to see how far things are getting. 您需要抛出一些断点并进行调试,或者添加一些调试日志以查看进展情况。 If it isn't even getting to a method you expect then the question can be rephrased "why is method X not being called" 如果还没有达到您期望的方法,则可以将问题改写为“为什么未调用方法X”

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

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