【发布时间】:2021-11-29 08:31:35
【问题描述】:
基本上在标题中说...似乎无法弄清楚为什么播放器属性返回 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^
【问题讨论】:
-
我在你的代码中有很多问题...首先
hash是干什么用的?您从不使用它...为什么不使用字典作为图标?然后你设置customProperties["icon"] = "icon";.. 你可能是说customProperties["icon"] = icon;为了不将它设置为"icon"而是参数icon你传递给方法..? ^^ -
另外你总是创建一个全新的
Hashtable,所以你检查它是否包含某个项目是完全多余的..相反,你可能想重用你的播放器已经存在的自定义属性并填充那里的项目? -
如前所述,要么使用字典,要么至少使用
switch稍后检查图标值 -
最后你为什么要为每个不同的精灵使用不同的精灵渲染器......?那么为什么不直接在 Inspector 中分配精灵呢?
-
返回
null到底是什么?你能说清楚这是指哪一行吗?