Photon Networking Unity AllProperties 未设置

Photon Networking Unity AllProperties not setting

我开始使用 Photon Networking for Unity,但 运行 遇到了问题。我想添加到播放器中的 CustomProperties,然后我想调试结果。但是调试打印 "Null"。我在创建房间后执行此操作。

有趣的是 OnPhotonPlayerPropertiesChanged() 它打印 "changed" 并且只在我执行 SetPlayerPosition() 时打印。

但是,如果我随后检查自定义属性中的密钥是否不包含它,那么它不会打印“10”?

    void Awake()
    {
        SetPlayerPosition();
    }

    public override void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
    {
        Debug.Log("changed");
        if (PhotonNetwork.player.CustomProperties.ContainsKey("1"))
        {
            Debug.Log("it works");
        }
    }

    void SetPlayerPosition()
    {
        ExitGames.Client.Photon.Hashtable xyzPos = new ExitGames.Client.Photon.Hashtable();
        xyzPos.Add(1, "10");
        xyzPos.Add(2, "5");
        xyzPos.Add(3, "10");
        PhotonNetwork.player.SetCustomProperties(xyzPos);
        // PhotonNetwork.player.SetCustomProperties(xyzPos, null, true); doesnt work either
    }

根据 PUN's doc-api 你应该这样做:

void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) 
{
    PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer;
    Hashtable props = playerAndUpdatedProps[1] as Hashtable;

    Debug.Log(string.Format("Properties {0} updated for player {1}", SupportClass.DictionaryToString(props), player);
    if (player.CustomProperties.ContainsKey("1"))
    {
        Debug.Log("it works 1");
    }
    if (props.ContainsKey("1"))
    {
        Debug.Log("it works 2");
    }
}

其实答案是键需要是字符串!