如何更改 Photon 中特定播放器的设置?
How to change settings for a specific player in Photon?
我正在制作一个简单的 Word Builder 类型的游戏(其中一个玩家输入一个单词,另一个玩家输入一个以第一个玩家单词的最后一个字母开头的单词),我试图获得这样 MasterClient 通过输入第一个单词开始游戏,然后他的 InputField 被禁用,第二个玩家的 InputField 被启用,他输入一个单词,反之亦然。
我尝试了多种方式,都是基于我对Unity和C#的基础知识。这是一个我认为肯定会起作用但没有起作用的例子。
public void ChangeTurn ()
{
if (photonView.IsMine)
{
inputField.enabled = false;
}
else
{
inputField.enabled = true;
}
}
简而言之,有没有办法做类似 PhotonNetwork.PlayerList[0].inputField.enabled = false;
的事情?请告诉我是否有办法解决这个问题?我将永远感激不已。
您可以存储当前活动的Player.ActorNumber
and on the Master use Player.GetNextFor
Gets a Player's next Player, as sorted by ActorNumber (Player.ID). Wraps around.
Useful when you pass something to the next player. For example: passing the turn to the next player.
例如
private int currentID;
...
currentID = Player.GetNextFor(currentID).ActorNumber;
...
然后转发给大家通过 RPC
...
photonView.RPC(nameof(ChangeTurn), RpcTarget.All, currentID);
...
// This will be executed on all clients
[PunRPC]
private void ChangeTurn(int newID)
{
// Simply check if the given newID matches your own ActorNumber
inputField.enabled = PhotonNetwork.LocalPlayer.ActorNumber == newID;
}
所以所有在一起,例如像
// Store the ID of the currently active player
private int currentID;
// This is what a client has to call when he is done with his turn
private void EndTurn()
{
// Tell only the masterClient that we are done
// and he shall change the turn to the next player
photonView.RPC(nameof(ChangeTurnOnMaster), RpcTarget.MasterClient);
}
// This will be executed on the MasterClient
[PunRPC]
private void ChangeTurnOnMaster()
{
// Just to be really sure this is only done on the master client
if(!PhotoNetwork.isMasterClient) return;
// Get the next actor number
// This wraps around so after reaching the last player it will again start with the first one
currentID = Player.GetNextFor(currentID).ActorNumber;
// Tell everyone the new active player ID
photonView.RPC(nameof(ChangeTurn), RpcTarget.All, currentID);
}
// This will be executed on all clients
[PunRPC]
private void ChangeTurn(int newID)
{
// Just in case let everyone store the active ID
// this way you can also handle other things based on the ID later
// And also deal with the case that the masterClient switches for some reason
currentID = newID;
// Simply check if the given newID matches your own ActorNumber
inputField.enabled = PhotonNetwork.LocalPlayer.ActorNumber == newID;
}
我正在制作一个简单的 Word Builder 类型的游戏(其中一个玩家输入一个单词,另一个玩家输入一个以第一个玩家单词的最后一个字母开头的单词),我试图获得这样 MasterClient 通过输入第一个单词开始游戏,然后他的 InputField 被禁用,第二个玩家的 InputField 被启用,他输入一个单词,反之亦然。
我尝试了多种方式,都是基于我对Unity和C#的基础知识。这是一个我认为肯定会起作用但没有起作用的例子。
public void ChangeTurn ()
{
if (photonView.IsMine)
{
inputField.enabled = false;
}
else
{
inputField.enabled = true;
}
}
简而言之,有没有办法做类似 PhotonNetwork.PlayerList[0].inputField.enabled = false;
的事情?请告诉我是否有办法解决这个问题?我将永远感激不已。
您可以存储当前活动的Player.ActorNumber
and on the Master use Player.GetNextFor
Gets a Player's next Player, as sorted by ActorNumber (Player.ID). Wraps around.
Useful when you pass something to the next player. For example: passing the turn to the next player.
例如
private int currentID;
...
currentID = Player.GetNextFor(currentID).ActorNumber;
...
然后转发给大家通过 RPC
...
photonView.RPC(nameof(ChangeTurn), RpcTarget.All, currentID);
...
// This will be executed on all clients
[PunRPC]
private void ChangeTurn(int newID)
{
// Simply check if the given newID matches your own ActorNumber
inputField.enabled = PhotonNetwork.LocalPlayer.ActorNumber == newID;
}
所以所有在一起,例如像
// Store the ID of the currently active player
private int currentID;
// This is what a client has to call when he is done with his turn
private void EndTurn()
{
// Tell only the masterClient that we are done
// and he shall change the turn to the next player
photonView.RPC(nameof(ChangeTurnOnMaster), RpcTarget.MasterClient);
}
// This will be executed on the MasterClient
[PunRPC]
private void ChangeTurnOnMaster()
{
// Just to be really sure this is only done on the master client
if(!PhotoNetwork.isMasterClient) return;
// Get the next actor number
// This wraps around so after reaching the last player it will again start with the first one
currentID = Player.GetNextFor(currentID).ActorNumber;
// Tell everyone the new active player ID
photonView.RPC(nameof(ChangeTurn), RpcTarget.All, currentID);
}
// This will be executed on all clients
[PunRPC]
private void ChangeTurn(int newID)
{
// Just in case let everyone store the active ID
// this way you can also handle other things based on the ID later
// And also deal with the case that the masterClient switches for some reason
currentID = newID;
// Simply check if the given newID matches your own ActorNumber
inputField.enabled = PhotonNetwork.LocalPlayer.ActorNumber == newID;
}