关于 PhotonNetwork.CurrentRoom.CustomProperties 的问题
Question about PhotonNetwork.CurrentRoom.CustomProperties
当多人游戏开始时,主客户端发送一个 PunRPC 让所有客户端 运行 执行一个函数。此函数尝试获取房间属性以查看游戏是否处于活动状态,如果是,它会执行某些操作。由于某种原因,客户端会收到空引用错误,但主客户端不会。奇怪的是,房间属性的散列 table 的调试是可见的,但我无法在其中获取特定项目。
尝试调试散列 table 以确保在代码 运行 时设置了密钥。它是。 "(System.String)ag=(System.Boolean)True ag=activeGame" 这显示在 Debug.Log(hash);
但是 (bool)hash[rpk.activeGame]
得到空引用错误。但仅在客户端而不是主客户端。所以密钥也有效。
// Call all the clients to set up the room settings in the sub menu.
[PunRPC]
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
{
GameObject gameMenu = GameObject.Find ("GameMenu");
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
hash = PhotonNetwork.CurrentRoom.CustomProperties;
Debug.Log(hash);
if ((bool)hash[rpk.activeGame]) // Error on this line on client but not on master client. Says null reference.
{
GameObject.Find ("SoundManager").GetComponent<SoundManagerScript> ().PlayBackgroundTwo ();
GameObject.Find ("GameMenu").GetComponent<SubMenu> ().ChangeSubMenuActive (false);
}
}
我正在尝试 运行 作为客户端的 if 语句,但出现错误。
感谢您选择光子!
要获得自定义 属性,我建议您使用 TryGetValue
方法如下:
hash = PhotonNetwork.CurrentRoom.CustomProperties;
object temp;
string key = rpk.activeGame;
if (hash.TryGetValue(key, out temp))
{
if (temp is bool)
{
bool activeGame = (bool)temp;
}
else
{
// unexpected custom property value type
}
}
else
{
// custom property not found
}
如果自定义属性还没有,等待回调IInRoomCallbacks.OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
(reference API).
其他说明和建议:
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
不确定它是否受支持,或者将多个参数传递给 PUN RPC 方法是否是个好主意。
要调试 Dictionary
或 Hashtable
日志,您可以使用 SupportClass.DictionaryToString()
方法。
所以而不是
Debug.Log(hash);
使用
Debug.Log(SupportClass.DictionaryToString(hash));
避免调用昂贵的方法,如 GameObject.Find
:
GameObject gameMenu = GameObject.Find ("GameMenu");
这里还有对gameMenu.GetComponent<SubMenu>()
的重复调用,至少调用一次并缓存找到的组件结果。
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
不应使用 ==
运算符比较字符串。至少使用 Equal 方法和正确的 StringComparison 类型。阅读“How to compare strings in C#”。
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
另外,为什么要用昵称来判断是玩家一还是玩家二?可能使用 ActorNumber 或自定义播放器索引。或者,如果房间仅供 2 位玩家使用,则使用玩家人数。
当多人游戏开始时,主客户端发送一个 PunRPC 让所有客户端 运行 执行一个函数。此函数尝试获取房间属性以查看游戏是否处于活动状态,如果是,它会执行某些操作。由于某种原因,客户端会收到空引用错误,但主客户端不会。奇怪的是,房间属性的散列 table 的调试是可见的,但我无法在其中获取特定项目。
尝试调试散列 table 以确保在代码 运行 时设置了密钥。它是。 "(System.String)ag=(System.Boolean)True ag=activeGame" 这显示在 Debug.Log(hash);
但是 (bool)hash[rpk.activeGame]
得到空引用错误。但仅在客户端而不是主客户端。所以密钥也有效。
// Call all the clients to set up the room settings in the sub menu.
[PunRPC]
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
{
GameObject gameMenu = GameObject.Find ("GameMenu");
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
hash = PhotonNetwork.CurrentRoom.CustomProperties;
Debug.Log(hash);
if ((bool)hash[rpk.activeGame]) // Error on this line on client but not on master client. Says null reference.
{
GameObject.Find ("SoundManager").GetComponent<SoundManagerScript> ().PlayBackgroundTwo ();
GameObject.Find ("GameMenu").GetComponent<SubMenu> ().ChangeSubMenuActive (false);
}
}
我正在尝试 运行 作为客户端的 if 语句,但出现错误。
感谢您选择光子!
要获得自定义 属性,我建议您使用 TryGetValue
方法如下:
hash = PhotonNetwork.CurrentRoom.CustomProperties;
object temp;
string key = rpk.activeGame;
if (hash.TryGetValue(key, out temp))
{
if (temp is bool)
{
bool activeGame = (bool)temp;
}
else
{
// unexpected custom property value type
}
}
else
{
// custom property not found
}
如果自定义属性还没有,等待回调IInRoomCallbacks.OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
(reference API).
其他说明和建议:
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
不确定它是否受支持,或者将多个参数传递给 PUN RPC 方法是否是个好主意。
要调试 Dictionary
或 Hashtable
日志,您可以使用 SupportClass.DictionaryToString()
方法。
所以而不是
Debug.Log(hash);
使用
Debug.Log(SupportClass.DictionaryToString(hash));
避免调用昂贵的方法,如 GameObject.Find
:
GameObject gameMenu = GameObject.Find ("GameMenu");
这里还有对gameMenu.GetComponent<SubMenu>()
的重复调用,至少调用一次并缓存找到的组件结果。
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
不应使用 ==
运算符比较字符串。至少使用 Equal 方法和正确的 StringComparison 类型。阅读“How to compare strings in C#”。
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
另外,为什么要用昵称来判断是玩家一还是玩家二?可能使用 ActorNumber 或自定义播放器索引。或者,如果房间仅供 2 位玩家使用,则使用玩家人数。