Unity Photon Network CustomPropeties 在加载新场景后未更新

时间:2021-04-16 15:27:56

标签: c# unity3d photon

我正在使用 Photon PUN 在 Unity 上开发在线多人游戏,但我遇到了房间自定义属性的问题。

在原始大厅屏幕中,当我更改房间属性时,它会为所有玩家更新,但是当我开始游戏、加载新关卡时,我想使用 CustomProperties 以便 MasterClient 可以为所有玩家设置相同的开始时间.我正在从附加到 DoNotDestroyOnLoad 游戏对象的游戏设置类和以下 getter/setter 中设置/获取自定义属性:

private static T GetProperty<T>(string key, T deafult)
{
    if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(key, out object value))
    {
        return (T)value;
    }
    else return deafult;
}

private static void SetProperty(string key, object value)
{
    ExitGames.Client.Photon.Hashtable table = PhotonNetwork.CurrentRoom.CustomProperties;
    if (table.ContainsKey(key))
    {
        table[key] = value;
    }
    else
    {
        table.Add(key, value);
    }
}

当我加载新场景时,之前的 CustomProperties 保持不变,新的变化出现在本地,但不会出现在其他玩家身上。我曾尝试使用 OnRoomPropertiesUpdate() 但它在加载新场景后没有激活。是否还有其他需要附加到 DoNotDestroyOnLoad 对象的内容?

1 个答案:

答案 0 :(得分:0)

更改表后,您必须通过 SetCustomProperties 重新分配它,否则您只是在更改本地副本。

private static void SetProperty(string key, object value)
{
    ExitGames.Client.Photon.Hashtable table = PhotonNetwork.CurrentRoom.CustomProperties;
    
    // No need to check Contains
    // This will either Add a new key or overwrite an existing one
    // -> This is more efficient and already behaves exactly the same ;)
    table[key] = value;

    PhotonNetwork.CurrentRoom.SetCustomProperties(table);
}