PUN2:否需要OnPhotonSerializeView调用来更新数组值?

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

似乎PUN用于确定所观察脚本的属性是否已更改的任何逻辑都不会查看数组中的值。更改所有其他属性的值会自动生成OnPhotonSerializeView调用,但不会更改数组中的值。有没有一种方法可以让PUN检查数组内容,或者我应该创建一个无意义的布尔值来翻转,只要我想强制进行更新?

注意:我的PhotonView设置为“更改时不可靠”。我尝试过字节[],光子seems to indicate is supported,也尝试过bool []。

c# unity3d photon
1个回答
1
投票

首先:我真的不是光子专家,所以我必须相信我的Google力量:P


[通常:确保带有组件的GameObject实际上位于PhotonView的“观察”列表中,否则它根本不会被调用OnPhotonSerializeView


我猜:数组是引用类型。光子可能不会一直遍历整个数组以跟踪任何更改,而只是检查引用本身是否已更改。

但是,您也可以在更改后简单地手动发送数组,例如,例如

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.