简体   繁体   中英

Unity3D: PhotonNetwork.GetRoomList() cannot get any RoomInfo

I am trying to create a room UI. I built a copy to test if I can see any room. However, it does not received any room. Here is my code:

public class NetworkManager : Photon.MonoBehaviour
{
    public GameObject ScrollViewContent;
    public GameObject RoomListItem;
    public Text RoomNameInputField;
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings("0.1");
    }

    void OnReceivedRoomListUpdate()
    {
        Debug.Log("OnReceivedRoomListUpdate");
    }

    void OnGUI()
    {
        Debug.Log("OnGUI");
        GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
        if (ScrollViewContent != null)
        {
            Debug.Log("ScrollViewContent");
            foreach (Transform child in ScrollViewContent.transform)
            {
                Debug.Log("Destroy");
                Destroy(child.gameObject);
            }
            foreach (RoomInfo game in PhotonNetwork.GetRoomList())
            {
                Debug.Log("RoomInfo");
                GameObject room = Instantiate(RoomListItem) as GameObject;
                room.GetComponentInChildren<Text>().text = game.name;
                room.transform.SetParent(ScrollViewContent.transform);
            }
            Debug.Log("ScrollViewContentEnd");
        }
    }


    public void CreateRoom()
    {
        if (!string.IsNullOrEmpty(RoomNameInputField.text))
            PhotonNetwork.CreateRoom(RoomNameInputField.text);
        //PhotonNetwork.CreateRoom(RoomNameInputField.text, new RoomOptions() { maxPlayers = 2,isVisible=true }, null);
    }
}

All of the public variables are set through inspector and console shows "ScrollViewContent" and "ScrollViewContentEnd" which means there should be no exception between them.

I have tried two ways to create room (both ways can create room successfully) and putting OnGUI() code in OnReceivedRoomListUpdate(). However, nothing show up, not even "RoomInfo" in console.

Is there something missed something in code?

I find out what the problem is. The default setting only join the server without join default lobby. Therefore, there is no room list.

Solution is to either create a lobby and join it or join the default lobby.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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