Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity Android Google Play服务-发送字符串消息_C#_Android_Google Play Games - Fatal编程技术网

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

C# Unity Android Google Play服务-发送字符串消息,c#,android,google-play-games,C#,Android,Google Play Games,我使用Unity Engine为Android制作了一个游戏,并使用google play游戏服务进行多人游戏。我向其他玩家发送字符串消息时出现问题。我可以发送,但其他玩家无法接收 发送代码: public void SendMyUpdate(float posX, float posZ, Vector3 velocity, float rotY, float weapon, string opponentId) { DebugConsole.Log("OppenentSend

我使用Unity Engine为Android制作了一个游戏,并使用google play游戏服务进行多人游戏。我向其他玩家发送字符串消息时出现问题。我可以发送,但其他玩家无法接收

发送代码:

    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);
}
接收代码:

    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);    
    }
这个密码是从

您应该记得添加一个索引,说明转换器应该在哪里找到字节范围

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

感谢您的贡献,但这样的回应不被视为回答,而是评论。如果你想做出有意义的贡献,请阅读。如果你继续为这个网站做贡献,你最终会有足够的声誉留下像这样的评论。