单位:Google Play服务玩家不会看到对方吗?

时间:2018-08-08 13:30:03

标签: unity3d service multiplayer playback

下午好,谢谢您来这里! 那就是我的问题!

[在此处输入图片描述] [1]

如上图所示,玩家通常会发现彼此,并且通常会创建一个新房间。

[在此处输入图片描述] [1]

但是问题出现在第二张照片中。创建摄像机后,玩家不会看到对方!

这是我的脚本!

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using System.Collections.Generic;
using GooglePlayGames.BasicApi.Multiplayer;

public class PlayerController : MonoBehaviour, RealTimeMultiplayerListener
{
    public Transform Prefab = null;
    public Transform Player = null;
    float Timer = 0f;
    static bool Authentication;

    void Update()
    {
        if (Authentication != true)
        {
            Authenticate();
        }
        else
        {
            if (Player != null)
            {
                if (Timer > 0f)
                {
                    Timer -= Time.deltaTime;
                }
                else
                {
                    Timer = 0.035f;
                    bool reliability = true;
                    string data = "Position:" + Player.position.x + "" + Player.position.y + "" + Player.position.z;
                    byte[] bytedata = System.Text.ASCIIEncoding.Default.GetBytes(data);
                    PlayGamesPlatform.Instance.RealTime.SendMessageToAll(reliability, bytedata);
                }
            }
        }
    }

    public void Authenticate()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
        .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
        PlayGamesPlatform.Instance.Authenticate((bool success) =>
        {
            if (success)
            {
                CreateQuickGame();
                Authentication = true;
            }
            else
            {
                //Application.LoadLevel(Application.loadedLevel);
            }
        });
    }

    void CreateQuickGame()
    {
        const int MinOpponents = 1, MaxOpponents = 2;
        const int GameVariant = 0;
        PlayGamesPlatform.Instance.RealTime.CreateQuickGame(MinOpponents, MaxOpponents,
                    GameVariant, this);
    }

    private bool isRoomSetuped = false;
    public void OnRoomSetupProgress(float percent)
    {
        if(percent >= 20f)
        {
            isRoomSetuped = true;
            PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();
        }
    }

    private bool connected = false;
    public void OnRoomConnected(bool success)
    {
        if (success)
        {
            connected = true;

            Player = Instantiate(Prefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
            Player.name = PlayGamesPlatform.Instance.RealTime.GetSelf().ParticipantId;
            bool reliability = true;
            string data = "Instantiate:0:1:2";
            byte[] bytedata = System.Text.ASCIIEncoding.Default.GetBytes(data);
            PlayGamesPlatform.Instance.RealTime.SendMessageToAll(reliability, bytedata);
        }
        else
        {
            connected = false;
            CreateQuickGame();
        }
    }

    public void OnLeftRoom()
    {
        connected = false;
    }

    public void OnParticipantLeft(Participant participant)
    {
        //
    }

    public void OnPeersConnected(string[] participantIds)
    {
        //
    }

    public void OnPeersDisconnected(string[] participantIds)
    {
        //
    }

    public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
    {
        if (!PlayGamesPlatform.Instance.RealTime.GetSelf().ParticipantId.Equals(senderId))
        {
            string rawdata = System.Text.ASCIIEncoding.Default.GetString(data);
            string[] sliced = rawdata.Split(new string[] { ":" }, System.StringSplitOptions.RemoveEmptyEntries);

            if (sliced[0].Contains("Instantiate"))
            {
                Transform naming = Instantiate(Prefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
                naming.name = senderId;
                naming.GetChild(0).gameObject.SetActive(false);
            }
            else if (sliced[0].Contains("Position"))
            {
                Transform target = GameObject.Find(senderId).transform;

                if (target == null)
                {
                    return;
                }

                Vector3 newpos = new Vector3
                (
                System.Convert.ToSingle(sliced[1]),
                System.Convert.ToSingle(sliced[2]),
                System.Convert.ToSingle(sliced[3])
                );
                target.position = newpos;
            }
        }
    }
}

这个脚本就是玩家的动作!

using UnityEngine;

public class Player : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 mousePosition_1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
            Vector3 mousePosition_2 = Camera.main.ScreenToWorldPoint(mousePosition_1);
            transform.position = mousePosition_2;
        }
    }
}

如果您至少能帮助我,我将非常感谢,谢谢!

0 个答案:

没有答案