繁体   English   中英

为什么我的光子双关代码返回“null”?

[英]Why is my photon pun code returning "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也不会在客户端直接生效。 您需要等待服务器设置它们并使用要同步的事件“确认”。 加入同一个房间的所有客户的更新。 在这里阅读更多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM