【发布时间】:2021-08-18 06:55:38
【问题描述】:
所以我尝试使用 RPC 并发送一个 int 列表和一个 int 作为参数,但是,虽然函数正确发送了 int 列表,但它发送的 int 值是错误的,并且对于每个客户端来说都是不同的,即使我正在发送对所有人都具有相同的价值。
这是RPC方法;
[PunRPC]
void GetPlayerCards(int[] _cardListInt, int _gCard)
{
if(intPlayerCardlist.Count == 0)
{
intPlayerCardlist.AddRange(_cardListInt);
}
foreach (int cardInt in intPlayerCardlist)
{
if(MyCardlist.Contains(gameFullCardlist.cardList[cardInt])) continue;
else MyCardlist.Add(gameFullCardlist.cardList[cardInt]);
}
groundCard = gameFullCardlist.cardList[_gCard];
Debug.Log("value: " + _gCard + " card name: " + groundCard);
}
这是从属于主客户端的房间实例化对象中调用的调用方法:
int[] List1 = cardList1.ToArray();
int[] List2 = cardList2.ToArray();
int[] List3 = cardList3.ToArray();
int[] List4 = cardList4.ToArray();
int[] List5 = cardList5.ToArray();
int gCard = cardList5[0];
PhotonView PV_P1 = gamePlayersList[0].GetComponent<PhotonView>();
PhotonView PV_P2 = gamePlayersList[1].GetComponent<PhotonView>();
PhotonView PV_P3 = gamePlayersList[2].GetComponent<PhotonView>();
PhotonView PV_P4 = gamePlayersList[3].GetComponent<PhotonView>();
PV_P1.RPC("GetPlayerCards", PV_P1.Owner, List1, gCard);
PV_P2.RPC("GetPlayerCards", PV_P2.Owner, List2, gCard);
PV_P3.RPC("GetPlayerCards", PV_P3.Owner, List3, gCard);
PV_P4.RPC("GetPlayerCards", PV_P4.Owner, List4, gCard);
如果您发现我为所有客户端发送相同的值但我得到不同的结果,请参阅下面的日志:
游戏管理器和玩家正在使用以下脚本从每个客户端场景中的空游戏对象实例化;
private void Awake()
{
//Order of Instantiation is important
PhotonNetwork.InstantiateRoomObject(InstantiatedPlayersListGameobject.name, Vector3.zero, Quaternion.identity);
PhotonNetwork.InstantiateRoomObject(cardsDeck.name, Vector3.zero, Quaternion.identity);
PhotonNetwork.InstantiateRoomObject(GameplayManager.name, Vector3.zero, Quaternion.identity);
}
void Start()
{
if(PhotonNetwork.IsConnectedAndReady)
{
if (playerPrefab != null)
{
GameObject playerInstantiation = PhotonNetwork.Instantiate(playerPrefab.name, Vector3.zero, Quaternion.identity);
playerInstantiation.transform.SetParent(InstanceParent.transform, false);
playerInstantiation.transform.localScale = Vector3.one;
playerInstantiation.transform.localPosition = Vector3.zero;
// playerInstantiation.name = (playerInstantiation.GetComponent<PhotonView>().Owner.NickName + " - AN: " +
// playerInstantiation.GetComponent<PhotonView>().Owner.ActorNumber);
Debug.Log("player instantiated");
}
else
{
Debug.Log("Place player Prefab");
}
}
}
【问题讨论】:
-
嗯,从您的日志看来,它被调用了 4 次,每个都有不同的值 => RPC 在哪里/如何准确调用?在我看来,您的四个玩家中的每一个都将它发送给每个其他玩家......您可能只想这样做一次?例如。仅当您是 MasterClient 时?
-
@derHugo 从房间实例化对象调用 RPC,我调用 RPC 4 次,每个玩家一次,这就是它在调试日志中出现 4 次的原因
标签: c# unity3d rpc multiplayer photon