Je crée un jeu pour Android avec Unity Engine et j'utilise les services de jeu google play pour le multijoueur. J'ai un problème quand j'envoie un message de chaîne à un autre joueur. Je peux l'envoyer mais l'autre joueur ne peut pas le recived.Unity Android Services Google Play - Envoi d'un message de chaîne
Envoyer le 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);
}
Code de desiderata:
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);
}
Ceci a obtenu ce code de https://www.raywenderlich.com/87042/creating-cross-platform-multi-player-game-unity-part-2
Merci de votre désir de contribuer, mais une réponse comme celle-ci n'est pas considérée comme une réponse mais plutôt comme un commentaire. Veuillez lire [Comment répondre à une question] (https://stackoverflow.com/help/how-to-answer) si vous souhaitez apporter une contribution significative. Si vous continuez à contribuer à ce site, vous aurez finalement assez de réputation pour laisser des commentaires comme celui-ci. [Voir cette explication sur la réputation] (https://stackoverflow.com/help/whats-reputation). – Nik