RPC以便在客户端游戏Unity上将变量从服务器发送到客户端

问题描述 投票:0回答:1

我的网络脚本运行得很好,除了我想在其他位置生成播放器时。我的位置基于当前连接的玩家数量。现在,变量正在服务器上更新,这样很好。但是,当客户端加入游戏时,他的玩家人数为0。我想将其设置为从服务器变量连接的当前玩家人数。我在搞RPC函数,但是运气不好。

所以这是我到目前为止所拥有的。

    var playerCount : int = 0;

    //player and jump master spawn componets
    var playerPrefab : GameObject;
    //spawn points
   var sp1: Transform;
    var sp2: Transform;
   var sp3: Transform;
   var sp4: Transform;

   var sp : Transform;


   function OnServerInitialized()
   {
 Debug.Log("Server initilized");
 playerCount++;
 spawnPlayer(sp1);

   }
   function OnPlayerConnected(networkPlayer:NetworkPlayer):void
   {
     Debug.Log (networkPlayer.guid + " connected");
     playerCount++;

     //spawnPlayer(sp2);
    //spawning player here does not work! Just a black screen no cam perhaps
   }

   function OnConnectedToServer()
   {
  //loop though every object in the game
    for(var go : GameObject in FindObjectsOfType(GameObject))
    {
        go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
    }
    Debug.Log("Connected to server");
    Debug.Log("Player Count: " + playerCount);
    if(playerCount ==0)
    {
        sp = sp1;

    }
    if(playerCount ==1)
    {
        sp = sp2;

    }
    if(playerCount ==2)
    {
        sp = sp3;

    }
    if(playerCount ==4)
    {
        sp = sp4;

    }
    spawnPlayer(sp);

      }


    //spawn player prefab
    function spawnPlayer(SP : Transform)
    {
  Network.Instantiate(playerPrefab, SP.position, Quaternion.identity, 0);
}

    @RPC
   function SendPlayerCount(player : NetworkPlayer )
   {
  //send playercount to client from server
  networkView.RPC("assignvariableRPC",player,playerCount);
   }
    @RPC
    function RecivePlayerCount(count : int)
   {
//get playercount from server
//playerCount = playerCount;
playerCount = count;
    }
networking unity3d rpc spawning
1个回答
1
投票

使用NetworkView + OnSerializeNetworkView创建一个全局不可见对象

这就是您想要的吗?

© www.soinside.com 2019 - 2024. All rights reserved.