简体   繁体   中英

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).

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()

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:

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:

System.Convert.ToSingle(cut[3])

cut[3] will look for the 4th split result and there are only three so an exception will occur. 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. 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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