简体   繁体   English

Unity Android Google Play服务-发送字符串消息

[英]Unity Android Google Play Services - Sending A String Message

I make a game for Android using Unity Engine and i use google play game services for multiplayer. 我使用Unity Engine为Android制作游戏,并为多人游戏使用Google Play游戏服务。 I have a problem when i sending a string message to other player. 向其他播放器发送字符串消息时出现问题。 I can send it but other player cant recived it. 我可以发送,但其他玩家无法接收。

Send Code: 发送代码:

    public void SendMyUpdate(float posX, float posZ, Vector3 velocity, float rotY, float weapon, string opponentId)
{
    DebugConsole.Log("OppenentSend " + opponentId);
    bytesize = opponentId.Length * sizeof(byte);
    _updateMessageLength = 26 + bytesize;
    _updateMessage.Clear();
    _updateMessage.Add(_protocolVersion);
    _updateMessage.Add((byte)'U');
    _updateMessage.AddRange(BitConverter.GetBytes(posX));
    _updateMessage.AddRange(BitConverter.GetBytes(posZ));
    _updateMessage.AddRange(BitConverter.GetBytes(velocity.x));
    _updateMessage.AddRange(BitConverter.GetBytes(velocity.z));
    _updateMessage.AddRange(BitConverter.GetBytes(rotY));
    _updateMessage.AddRange(BitConverter.GetBytes(weapon));
    //_updateMessage.AddRange(BitConverter.GetBytes(opponentHp));
    _updateMessage.AddRange(System.Text.Encoding.UTF8.GetBytes(opponentId));    
    byte[] messageToSend = _updateMessage.ToArray();
    PlayGamesPlatform.Instance.RealTime.SendMessageToAll(false, messageToSend);
}

Recived Code: 接收的代码:

    public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
{
    // We'll be doing more with this later...
    byte messageVersion = (byte)data[0];
    // Let's figure out what type of message this is.
    char messageType = (char)data[1];
    if (messageType == 'U' && data.Length == _updateMessageLength)
    {
        float posX = BitConverter.ToSingle(data, 2);
        float posZ = BitConverter.ToSingle(data, 6);
        float velX = BitConverter.ToSingle(data, 10);
        float velZ = BitConverter.ToSingle(data, 14);
        float rotY = BitConverter.ToSingle(data, 18);
        float weapon = BitConverter.ToSingle(data, 22);
        //int opponentHp = BitConverter.ToInt16(data, 26);
        string opponentId = System.Text.Encoding.UTF8.GetString(data);

        // We'd better tell our GameController about this.
        if (updateListener != null)
        {
            updateListener.UpdateReceived(senderId, posX, posZ, velX, velZ, rotY, weapon, opponentId);
            DebugConsole.Log("OppenentRecived " + opponentId);
        }
    }
    else if (messageType == 'F' && data.Length == _finishMessageLength)
    {
        // We received a final time!
        float finalTime = System.BitConverter.ToSingle(data, 2);
        //Debug.Log ("Player " + senderId + " has finished with a time of " + finalTime);    
    }

This got this code from https://www.raywenderlich.com/87042/creating-cross-platform-multi-player-game-unity-part-2 这是从https://www.raywenderlich.com/87042/creating-cross-platform-multi-player-game-unity-part-2获得的代码

您应该记住添加一个索引,以指示转换器应在何处找到字节范围

ie:  string opponentId = System.Text.Encoding.UTF8.GetString(data, int index);

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

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