PUN 2未连接播放器

时间:2018-11-06 03:51:26

标签: c# unity3d photon

我正在使用Unity开发游戏,并且正在使用Photon的PUN2。要管理游戏的在线模式,以前需要在线连接并很好地列出大厅的房间,但是,我不知道为什么现在不起作用。现在,我无法通过使用JoinRoom(RoomName)或JoinRandomRoom()来搜索房间来加入房间,它仅搜索房间,然后将当前状态标记为“加入”并返回到大厅。另外,需要告知大厅的房间没有列出,甚至在房间里等待5分钟或更长时间。 这是我的代码,谢谢:

 private Button B;
    private Text T;
    private AudioSource Audio;
    public AudioClip OK, Fail;
    void Start ()
    {
        GameObject.Find("StartPanel").GetComponent<Animator>().SetBool("Show", true);
        Audio = GetComponent<AudioSource>();
        B = GetComponent<Button>();
        T = GameObject.Find("ConnecText").GetComponent<Text>();
        B.onClick.AddListener(Clicker);
    }
    void Clicker()
    {
        B.interactable = false;
        Retrying();
        T.text = "Connecting...";
        PhotonNetwork.ConnectUsingSettings();
    }
    public override void OnDisconnected(DisconnectCause cause)
    {
        Failed();
    }
    public override void OnConnectedToMaster()
    {
        StartCoroutine(Connected());
    }
    IEnumerator Connected()
    {
        T.text = "Connected!";
        Audio.clip = OK;
        Audio.Play();
        yield return new WaitForSeconds(1);
        GameObject.Find("MenuPanel").SetActive(true);
        GameObject.Find("MenuPanel").GetComponent<Animator>().SetBool("Show", true);
        B.interactable = true;
    }
    void Retrying()
    {
        GameObject.Find("FailText").GetComponent<Text>().color = new Color(0, 0, 0, 0);
        GameObject.Find("IFail").GetComponent<Image>().color = new Color(0, 0, 0, 0);
    }
    void Failed()
    {
        Color myRed = new Color();
        ColorUtility.TryParseHtmlString("#DB9191FF", out myRed);
        GameObject.Find("FailText").GetComponent<Text>().color = myRed;
        GameObject.Find("IFail").GetComponent<Image>().color = Color.white;
        T.text = "Retry";
        Audio.clip = Fail;
        Audio.Play();
        B.interactable = true;
    }

列表房代码:

public GameObject roomPrefab;
    public Sprite Four, Two, Three;
    private string RoomName;
    private int PlayerAmount;
    private int MaxPlayers;
    private Image I;
    private Vector2 RoomVector;
    private bool Lock = false;
    public GameObject Content;
    private List<RoomInfo> RoomList;
    private bool IsntNull = false;
    private Dictionary<string, RoomInfo> cachedRoomList;
    private Dictionary<string, GameObject> roomListEntries;
    private Dictionary<int, GameObject> playerListEntries;
    private GameObject Handle;
    public RooManager instance;

   private void Awake()
   {

            if (instance != null)
            {
                DestroyImmediate(gameObject);
                return;
            }
            DontDestroyOnLoad(gameObject);
            instance = this;
        cachedRoomList = new Dictionary<string, RoomInfo>();
        roomListEntries = new Dictionary<string, GameObject>();
   }
    void Start()
    {
        //Content = GameObject.Find("Content").GetComponent<GameObject>();
        RoomVector = new Vector2(450 /*370 */, this.transform.position.y);
    }
    private void ClearRoomListView()
    {
        foreach (GameObject entry in roomListEntries.Values)
        {
            Destroy(entry.gameObject);
        }
        roomListEntries.Clear();
    }
    public override void OnJoinedRoom()
    {

        if (playerListEntries == null)
        {
            playerListEntries = new Dictionary<int, GameObject>();
        }

        foreach (Player p in PhotonNetwork.PlayerList)
        {
            GameObject entry = Instantiate(roomPrefab);
            playerListEntries.Add(p.ActorNumber, entry);
        }
    }
    public override void OnLeftRoom()
    {
        foreach (GameObject entry in playerListEntries.Values)
        {
            Destroy(entry.gameObject);
        }

        playerListEntries.Clear();
        playerListEntries = null;
    }
    public override void OnLeftLobby()
    {
        cachedRoomList.Clear();

        ClearRoomListView();
    }
    private void Update()
    {
        print(PhotonNetwork.NetworkClientState);
    }
    private void UpdateRoomListView()
    {
        foreach (RoomInfo Item in cachedRoomList.Values)
        {
            RoomName = Item.Name;
            PlayerAmount = Item.PlayerCount;
            MaxPlayers = Item.MaxPlayers;
            RoomVector.y -= 100;
            GameObject RoomPrefab = Instantiate(roomPrefab, RoomVector, transform.rotation) as GameObject;
            if (Item.PlayerCount == 0)
            {
                Destroy(RoomPrefab);
            }
            print(PhotonNetwork.CurrentLobby.Name);
            RoomPrefab.transform.Find("RoomName").GetComponent<Text>().text = RoomName;
            if (Item.Name.Length == 10)
            {
                Vector2 AddFive = new Vector2(RoomPrefab.transform.Find("RoomName").transform.position.x + 10, RoomPrefab.transform.Find("RoomName").transform.position.y);
                RoomPrefab.transform.Find("RoomName").transform.position = AddFive;
            }
            if (Item.Name.Length >= 10)
            {
                Vector2 AddTen = new Vector2(RoomPrefab.transform.Find("RoomName").transform.position.x + 40, RoomPrefab.transform.Find("RoomName").transform.position.y + 20);
                RoomPrefab.transform.Find("RoomName").transform.position = AddTen;
                RoomPrefab.transform.Find("PlayerInt").GetComponent<Text>().fontSize = 47;
            }
            RoomPrefab.transform.Find("PlayerInt").GetComponent<Text>().text = PlayerAmount.ToString();
            if (Item.MaxPlayers == 4)
            {
                RoomPrefab.transform.Find("IPlayerA").GetComponent<Image>().sprite = Four;
            }
            else if (Item.MaxPlayers == 2)
            {
                RoomPrefab.transform.Find("IPlayerA").GetComponent<Image>().sprite = Two;
            }
            else if (Item.MaxPlayers == 3)
            {
                RoomPrefab.transform.Find("IPlayerA").GetComponent<Image>().sprite = Three;
            }
            RoomPrefab.transform.SetParent(Content.transform);
        }
    }

    public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        ClearRoomListView();
        UpdateCachedRoomList(roomList);
        UpdateRoomListView();
        print("Updated");
    }
    private void UpdateCachedRoomList(List<RoomInfo> roomList)
    {
        foreach (RoomInfo info in roomList)
        {
            // Remove room from cached room list if it got closed, became invisible or was marked as removed
            if (!info.IsOpen || !info.IsVisible || info.RemovedFromList)
            {
                if (cachedRoomList.ContainsKey(info.Name))
                {
                    cachedRoomList.Remove(info.Name);
                }

                continue;
            }

            // Update cached room info
            if (cachedRoomList.ContainsKey(info.Name))
            {
                cachedRoomList[info.Name] = info;
            }
            // Add new room info to cache
            else
            {
                cachedRoomList.Add(info.Name, info);
            }
        }
    }
}

需要说的是,在列表室代码中,将显示“更新”消息,并且控制台中没有错误或警告。

1 个答案:

答案 0 :(得分:0)

OnPhotonCreateRoomFailed 
OnPhotonJoinRoomFailed

您可能想通过覆盖更多回调进行调试

您可以找到所有光子回调here