在 PUN2 中加入房间之前,如何通过网络从大厅中的客户端获取或共享一些数据给其他人?
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有你的完整代码,所以我只能怀疑会发生什么,但我的猜测如下:
密钥肯定已添加到房间属性中,否则它不会返回
0
而是抛出KeyNotFoundException
。因此,从您给定的代码来看,我猜您在某个地方有一个预定义的
并且现在已经设置了
,当
您希望该值也在
customRoomProps
中更新时。但是
int
是一个值类型!您存储在customRoomProps
中的值是您添加该值时当前存储在betAmount
中的值的按值复制。customRoomProps
中存储的值与betAmount
不再有任何关系!您需要在需要时设置该值,
基本上就是您注释掉的行。
然后在
OnRoomListUpdate
中你应该能够得到它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 aKeyNotFoundException
.So from your given code I guess you somewhere have a predefined
and somewhere already set
now when doing
you expect that the value is also updated within the
customRoomProps
.BUT
int
is a value-type! The value you stored in thecustomRoomProps
is a copy by value of whatever value was currently stored inbetAmount
the moment you added it.The value stored in
customRoomProps
is in no way related to thebetAmount
anymore!You rather need to set the value the moment you need it
basically the line you have commented out.
And then in
OnRoomListUpdate
you should be able to get it like