我是 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,但是當其他人加入房間時它可以作業,然后他們可以看到該資料。
uj5u.com熱心網友回復:
我沒有你的完整代碼,所以我只能懷疑會發生什么,但我的猜測如下:
密鑰肯定會添加到房間屬性中,否則它不會回傳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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/448002.html
