为什么我的光子双关语代码返回"null"?



基本上是在标题中说的…似乎无法弄清楚为什么玩家属性返回null…我使用Unity 2019.24和Photon PUN…我试图让它从数组中返回一个随机字符串,我试图让它到没有人有相同的字符串…


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PropertyAssigner : MonoBehaviour
{
public SpriteRenderer Donkey = new SpriteRenderer();
public SpriteRenderer Elephant = new SpriteRenderer();
public SpriteRenderer Porcupine = new SpriteRenderer();
public SpriteRenderer Beaver = new SpriteRenderer();
public SpriteRenderer Eagle = new SpriteRenderer();
public SpriteRenderer Bird = new SpriteRenderer();
public SpriteRenderer Dinosaur = new SpriteRenderer();
public SpriteRenderer Rhino = new SpriteRenderer();
public Sprite DonkeySp;
public Sprite ElephantSp;
public Sprite PorcupineSp;
public Sprite BeaverSp;
public Sprite EagleSp;
public Sprite BirdSp;
public Sprite DinosaurSp;
public Sprite RhinoSp;

List<string> icons = new List<string>();
List<string> shuffledIcons = new List<string>();
bool b;
ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();

// Start is called before the first frame update
void Start() 
{
var hash = new Hashtable();
icons.Add("Donkey");
icons.Add("Elephant");
icons.Add("Porcupine");
icons.Add("Beaver");
icons.Add("Eagle");
icons.Add("Bird");
icons.Add("Dinosaur");
icons.Add("Rhino");
playerAnimalAssign();
}

// Update is called once per frame
void Update()
{
}

void SetCustomProperties(string icon)
{
Debug.Log("setting properties");
ExitGames.Client.Photon.Hashtable customProperties = new ExitGames.Client.Photon.Hashtable();

if (!customProperties.ContainsKey("icon"))
{
Debug.Log("adding properties");
customProperties.Add("icon", "icon");
}

customProperties["icon"] = "icon";
Debug.Log(customProperties.ToStringFull());
PhotonNetwork.LocalPlayer.SetCustomProperties(customProperties);
Debug.Log(customProperties.ToStringFull());
foreach (var item in PhotonNetwork.PlayerList)
{
if (item.CustomProperties.ContainsKey("icon"))
{
Debug.Log(item.CustomProperties["icon"]);
}
}
}

void playerAnimalAssign()
{
for (int i = 0; i < icons.Count; i++)
{
Debug.Log(icons.Count);
string temp = icons[i];
int randomIndex = Random.Range(i, icons.Count);
icons[i] = icons[randomIndex];
icons[randomIndex] = temp;
}
string s = icons[1];
playerProperties.Add("icon", s);
playerProperties["icon"] = s;

Debug.Log(playerProperties.ToStringFull());

SetCustomProperties(s);
icons.Remove(icons[1].ToString());

Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);
Debug.Log(icons.ToStringFull<string>());
Debug.Log(PhotonNetwork.PlayerList.Length);
Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);

foreach (var item in PhotonNetwork.PlayerList)
if (item.CustomProperties["icon"] == "Donkey")
{
Donkey.sprite = DonkeySp;

Debug.Log("Donkey Sprite");
}
else if (item.CustomProperties["icon"] == "Elephant")
{
Elephant.sprite = ElephantSp;

Debug.Log("Elephant Sprite");
}
else if (item.CustomProperties["icon"] == "Porcupine")
{
Porcupine.sprite = PorcupineSp;

Debug.Log("Porcupine Sprite");
}
else if (item.CustomProperties["icon"] == "Beaver")
{
Beaver.sprite = BeaverSp;

Debug.Log("Beaver Sprite");
}
else if (item.CustomProperties["icon"] == "Eagle")
{
Eagle.sprite = EagleSp;

Debug.Log("Eagle Sprite");
}
else if (item.CustomProperties["icon"] == "Bird")
{
Bird.sprite = BirdSp;

Debug.Log("Bird Sprite");
}
else if (item.CustomProperties["icon"] == "Dinosaur")
{
Dinosaur.sprite = DinosaurSp;

Debug.Log("Dinosaur Sprite");
}
else if (item.CustomProperties["icon"] == "Rhino")
{
Rhino.sprite = RhinoSp;

Debug.Log("Rhino Sprite");
}
}
}

提前感谢您的帮助!!如果你看到任何乱七八糟的代码,请告诉我^w^

设置属性是异步的,即使它返回true,也不会在调用方法后直接对客户端生效。您需要等待服务器设置它们并"返回"。有一个要同步的事件。所有加入同一房间的客户端的更新。点击这里阅读更多内容。