【问题标题】:Unity Android Google Play Services - Sending A String MessageUnity Android Google Play Services - 发送字符串消息
【发布时间】:2017-06-20 18:36:56
【问题描述】:

我使用 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);    
    }

此代码来自 https://www.raywenderlich.com/87042/creating-cross-platform-multi-player-game-unity-part-2

【问题讨论】:

    标签: c# android google-play-games


    【解决方案1】:

    您应该记住添加转换器应该找到字节范围的索引

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

    【讨论】:

    • 感谢您的贡献,但这样的回复不被视为答案,而是评论。如果您想做出有意义的贡献,请阅读How to answer a question。如果你继续为这个网站做贡献,你最终将有足够的声誉离开像这个这样的 cmets。 See this explanation on reputation.
    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多