PUN 2 Not Working
I'm developing a game with Unity and I´'m using Photon's PUN 2 To manage the game's online mode, before, the online connect and list the rooms of the lobby very well, but, I don't know why it isn't working now. Now I can't joina room by searching it with JoinRoom(RoomName) or JoinRandomRoom(), it just searches a room, then, marks the current state to "Joining" and goes back to the lobby. Also, need to tell that the rooms in lobby aren´t listing, even waiting 5 miutes or more in a room. Here's my code, thanks:
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; }The list rooms code:
<pre class="CodeBlock"><code> 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; 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); } } } }Need to say that in the list rooms code, the "Update" message yes prints, and theres no errors or warnings in the console.
Add comment
0
Comments
-
Hi @SebGM2018,
in terms of the OnRoomListUpdate callback: the client has to join a lobby before he gets the room list from that lobby. Usually rooms are created in the default lobby. The client can usePhotonNetwork.JoinLobby();
to join that default lobby. When he has joined the lobby, he will receive an up-to-date room list andOnRoomListUpdate
gets called whenever he receives updates related to the room list.
Joining a room by name doesn't require joining a lobby before as far as I know. The same applies forPhotonNetwork.JoinRandomRoom();
.0