光子网络Unity AllProperties没有设置

时间:2017-08-31 10:27:14

标签: c# unity3d photon

我开始使用Photon Networking for Unity,但我遇到了一个问题。我想添加到播放器中的CustomProperties,然后我想调试结果。但是调试打印“Null”。我在创建房间后这样做。

有趣的是OnPhotonPlayerPropertiesChanged()它打印“已更改”,只有当我执行SetPlayerPosition()时才这样做。

但是,如果我然后检查customproperties里面的密钥是不包含它所以它不打印“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
    }

2 个答案:

答案 0 :(得分:2)

根据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");
    }
}

答案 1 :(得分:0)

实际上,awnser是键必须是字符串!