c# - PUN2:没有 OnPhotonSerializeView 调用更新数组值?
问题描述
似乎 PUN 用于确定观察到的脚本的属性是否已更改的任何逻辑都不会查看数组中的值。更改所有其他属性的值会生成自动 OnPhotonSerializeView 调用,但更改数组中的值不会。有没有办法让 PUN 检查数组内容,或者我应该创建一个无意义的 bool 来在我想强制更新时翻转?
注意:我将 PhotonView 设置为 Unreliable On Change。我试过 byte[],Photon似乎表明它是受支持的,我也试过 bool[]。
解决方案
首先:我真的不是光子专家,所以我必须相信我的谷歌能力:P
一般来说:确保GameObject
你的组件实际上在PhotonView
“观察”列表中,否则它根本不会被OnPhotonSerializeView
调用。
我猜:数组是引用类型。Photon 可能不会一直遍历整个数组以跟踪任何更改,而只会检查引用本身是否更改。
作为替代方案,您可以在更改后简单地手动发送数组,例如
public bool[] myBools;
...
photonView.RPC("SetArrayRPC", PhotonTargets.All, (object)myBools);
...
[PunRPC]
private void SetArrayRPC(bool[] array)
{
myBools = array;
}
但是afaik这实际上也应该这样做
public class Example : Photon.MonoBehaviour
{
public bool[] myBools = new bool[5];
// Hit "Test" in the context menu in order to invert all values
// Then everywhere you should see in the Inspector the updated array
[ContextMenu(nameof(Test))]
private void Test()
{
for(var i = 0; i < myBools.Length; i++)
{
myBools[i] = !myBools[i];
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
// We own this player: send the others our data
stream.SendNext(myBools);
}
else
{
// Network player, receive data
myBools = (bool[])stream.ReceiveNext();
}
}
}
推荐阅读
- excel - 在 Excel VBA 中取消选择图表
- javascript - 睡着时移动设备上的 window.speechSynthesis
- javascript - 未捕获的 TypeError:无法在 Chrome 中的 Javascript 中设置属性“innerHTML”为 null
- kubernetes - kunernetes Service 和 Ingress 的区别
- javascript - react-select 2 设置值/标签
- ios - 从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController
- python - Keras Tuner 抛出 InvalidArgumentError
- python - Python Setuptools:添加没有“main”功能的脚本作为“console_scripts”入口点的快速方法
- angular - 无法从 httpclient 获得响应,使用 get 方法
- python - 来自列名的 Seaborn 线条样式