在 PUN2 中,如何在其他人加入房间之前通过网络从大厅中的客户获取或共享一些数据?

How to get or share some data from a client in a lobby to others over a network before they join the room in PUN2?

我是 PUN2 的新手。我面临一个问题,我想描述一些加入我的房间的投注金额,这是我在创建房间时定义的。问题是我希望当其他人在大厅时必须向他们展示该金额,他们可以从那里决定是否 join/not 支付该金额。为此我正在做: 我有 1 个 MenuManager.cs 脚本,其中

public InputField amount;
[SerializeField] Transform _content; //container of list
[SerializeField] RoomListing _roomListing;
List<RoomListing> _listings = new List<RoomListing>();

public int SetBetAmount()
{
    if (string.IsNullOrEmpty(amount.text))
    {

        return -1;
    }
    else
        return (int.Parse(amount.text));

}

// 创建房间按钮时运行的脚本如下:

public void OnCreateRoomBtnClicked()
    {
        string roomName = "Room " + Random.Range(1, 800);
        int maxPlayersInt = SetMaxPlayers();
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers = (byte)maxPlayersInt;
        string[] roomPropertiesInLobbby = { "betAmount" };
        betAmount = SetBetAmount();
        //customRoomProps["betAmount"] = (byte)SetBetAmount();
        Debug.Log("Bet Amount Updated" + customRoomProps["betAmount"]);
        SetLaps();
        roomOptions.CustomRoomPropertiesForLobby = roomPropertiesInLobbby;
        roomOptions.CustomRoomProperties = customRoomProps;
        PhotonNetwork.CreateRoom(roomName, roomOptions);
    }

OnRoomListUpdate 回调适用于信息数据,但发送的不是正确的 betAmount,而是垃圾值 0;

 public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        foreach (RoomInfo info in roomList)
        {
            if (info.RemovedFromList)
            {
                int index = _listings.FindIndex(x => x.RoomInfo.Name == info.Name);
                if (index != -1)
                {
                    Destroy(_listings[index].gameObject);
                    _listings.RemoveAt(index);
                }
            }

        else
        {
            RoomListing listing = Instantiate(_roomListing, _content);
            if (listing != null)
            {
                listing.GetComponent<RoomListing>().bettingAmt = -3; //there I tried betAmount but it sends 0
                listing.SetRoomInfo(info);
                _listings.Add(listing);
            }
        }
    }
}

我也试过这个但不知道怎么做

 public override void OnJoinedLobby()
    {
        //if (customRoomProps.ContainsKey("betAmount"))
        //{
        //    //Debug.Log("Call From Master");
        //    object _amount;
        //    if (customRoomProps.TryGetValue("betAmount", out _amount))
        //    {

        //        Debug.Log("Bet Amount" + _amount.ToString());
        //        betAmountS = (int)_amount;
        //        Debug.Log("BetAmount  " + betAmount);
        //    }
        //}

        //else
        //{
        //    Debug.Log("Call From local");
        //}
    }

此外,我也尝试过 PUNRPC,但当其他人加入房间时它会起作用,然后他们就可以看到该数据。

我没有你的完整代码,所以我只能怀疑会发生什么,但我的猜测如下:

钥匙肯定会添加到房间属性中,否则它不会 return 0 而是抛出一个 KeyNotFoundException.

所以根据你给出的代码,我猜你在某个地方有一个预定义的

private Hashtable customRoomProps = new Hashtable ();
private int betAmount;

并且某处已经设置好

customRoomProps ["betAmount"] = betAmount;

现在做的时候

betAmount = SetBetAmount();

您希望值也在 customRoomProps 内更新。

但是int是一个value-type!您存储在 customRoomProps 中的值是 按值 复制当前存储在 betAmount 中的任何值。

存储在 customRoomProps 中的值与 betAmount 没有任何关系了!

您更愿意在需要时设置值

betAmount = SetBetAmount();
customRoomProps["betAmount"] = betAmount;

基本上是你注释掉的行。


然后在OnRoomListUpdate你应该可以得到它

if(info.CustomProperties.TryGetValue("betAmount", out var betAmount))
{
    listing.GetComponent<RoomListing>().bettingAmt = (int) betAmount;
}