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

发布于 2025-01-16 15:58:00 字数 2856 浏览 1 评论 0原文

我是 PUN2 的新手。我面临一个问题,我想描述一些加入我的房间的投注金额,这是我在创建房间时定义的。问题是,我希望当其他人在大厅时必须向他们展示该金额,以便他们可以通过支付该金额来决定是否加入。为此我正在做: 我有 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,但当其他人加入房间时,它可以工作,然后他们可以看到该数据。

I am new to PUN2. I am facing an issue that I want to describe some betting amount for joining my room which I define at the time of room creation. The issue is that I want that amount must be shown to others when they are in a lobby, from where they could decide whether they join/not by paying that amount. For this I am doing:
I have 1 MenuManager.cs script in which

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));

}

// The Script that runs when Create Room Button Clicks as follow:

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 Callback works fine for info data but not sending correct betAmount but the garbage value 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);
            }
        }
    }
}

I have also tried this but not sure how to do this

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

Plus, I have also tried PUNRPC but it works when others join the Room then they could see that data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-01-23 15:58:00

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

密钥肯定已添加到房间属性中,否则它不会返回 0 而是抛出 KeyNotFoundException

因此,从您给定的代码来看,我猜您在某个地方有一个预定义的

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

并且现在已经设置了

customRoomProps ["betAmount"] = betAmount;

,当

betAmount = SetBetAmount();

您希望该值也在 customRoomProps 中更新时。

但是 int 是一个值类型!您存储在 customRoomProps 中的值是您添加该值时当前存储在 betAmount 中的值的按值复制

customRoomProps 中存储的值与 betAmount 不再有任何关系!

您需要在需要时设置该值,

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

基本上就是您注释掉的行。


然后在 OnRoomListUpdate 中你应该能够得到它

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

I don't have your entire code so I can only suspect what happens but my guess is the following:

The key is definitely added to the room properties otherwise it wouldn't return 0 but throw a KeyNotFoundException.

So from your given code I guess you somewhere have a predefined

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

and somewhere already set

customRoomProps ["betAmount"] = betAmount;

now when doing

betAmount = SetBetAmount();

you expect that the value is also updated within the customRoomProps.

BUT int is a value-type! The value you stored in the customRoomProps is a copy by value of whatever value was currently stored in betAmount the moment you added it.

The value stored in customRoomProps is in no way related to the betAmount anymore!

You rather need to set the value the moment you need it

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

basically the line you have commented out.


And then in OnRoomListUpdate you should be able to get it like

if(info.CustomProperties.TryGetValue("betAmount", out var betAmount))
{
    listing.GetComponent<RoomListing>().bettingAmt = (int) betAmount;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文