客户端如何在Unity Multiplayer中将信息发送到主机

时间:2018-06-25 12:36:04

标签: unity3d

我想将播放器名称和其他详细信息从客户端发送到主机设备。我已经设置了最多两个玩家连接的游戏。

以下是我多次尝试并进行了不同更改的代码:

    void Start ()
 {
     spriteRenderer = GetComponent<SpriteRenderer> ();
     circleCollder = GetComponent<CircleCollider2D> ();
     //        StartCoroutine (ColorSwitcher ());
     InitialTasks ();
 }

 private void InitialTasks ()
 {
     isAlive = true;
     GameManager.Instance.IsPlayerWin = false;
     int selectedTheme = Random.Range (0, ballSprites.Length);
     spriteRenderer.sprite = ballSprites [selectedTheme];
     trailRenderer.material = trailMaterials [selectedTheme];
     destroyParticleObj.GetComponent<ParticleSystemRenderer> ().material = trailMaterials [selectedTheme];

     hidePlayerName += OnTouchHidePlayerName;

     if (!isLocalPlayer) {
         // remote player
         gameObject.tag = GameConstants.TAG_REMOTE_PLAYER;
     } else {
         // local player
         Debug.Log ("locally set player name: " + DataCollection.localPlayer.PlayerName);
         playerNameText.text = DataCollection.localPlayer.PlayerName;
         StartCoroutine (SomeDelayForSendingPlayerDetails ());
     }


//        if (isServer)
//            RpcSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
//        else
//            CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
 }

 IEnumerator SomeDelayForSendingPlayerDetails ()
 {
     yield return new WaitForSeconds (0.5f);
     CmdSetRemotePlayerName (DataCollection.localPlayer.PlayerName);
 }

 [ClientRpc]
 private void RpcSetRemotePlayerName (string remotePlayerName)
 {
     playerNameText.text = remotePlayerName + "@";
     Debug.Log ("rpc remote player name: " + remotePlayerName);
 }

 [Command]
 private void CmdSetRemotePlayerName (string remotePlayerName)
 {
     RpcSetRemotePlayerName(remotePlayerName);
         Debug.Log ("cmd remote player name: " + remotePlayerName);
 }

基本上,我想在2人多人游戏中交换两个人的名字。所以请给我一些建议。

0 个答案:

没有答案