【问题标题】:Unity Photon Network CustomPropeties not updating after loading new sceneUnity Photon Network CustomPropeties 在加载新场景后未更新
【发布时间】:2021-07-11 15:29:51
【问题描述】:

我正在使用 Photon PUN 在 Unity 上开发在线多人游戏,但房间 CustomProperties 出现问题。

在原始大厅屏幕中,当我更改房间属性时,它会为所有玩家更新,但是当我开始游戏,加载新关卡时,我想使用 CustomProperties 以便 MasterClient 可以为所有玩家设置相同的开始时间.我正在从附加到 DoNotDestroyOnLoad GameObject 的 Game Settings 类和以下 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 对象?

【问题讨论】:

    标签: c# unity3d photon


    【解决方案1】:

    更改表后,您必须通过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);
    }
    

    【讨论】:

    • 啊,非常感谢!看来我是个白痴,在几次提交之前我在测试其他东西时不小心删除了该行。也感谢哈希表技巧!
    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多