AdminLobby.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class AdminLobby : MonoBehaviourPunCallbacks, ILobbyCallbacks
  8. {
  9. public GameObject listItemPrefab;
  10. public Transform parent;
  11. public static AdminLobby instance;
  12. void Awake()
  13. {
  14. if(instance != null) { Destroy(gameObject); return; }
  15. instance = this;
  16. DontDestroyOnLoad(gameObject);
  17. PhotonNetwork.Disconnect();
  18. PhotonNetwork.ConnectUsingSettings();
  19. }
  20. public override void OnConnectedToMaster()
  21. {
  22. base.OnConnectedToMaster();
  23. Debug.Log("Connected to photon");
  24. PhotonNetwork.JoinLobby();
  25. }
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. }
  30. public override void OnRoomListUpdate(List<RoomInfo> roomList)
  31. {
  32. base.OnRoomListUpdate(roomList);
  33. //PURGE
  34. foreach(Transform child in parent.GetComponentsInChildren<Transform>())
  35. {
  36. if(child == parent) { continue; }
  37. Destroy(child.gameObject);
  38. }
  39. RoomInfo _room;
  40. foreach(RoomInfo room in roomList)
  41. {
  42. GameObject newItem = Instantiate(listItemPrefab, parent);
  43. _room = room;
  44. newItem.GetComponentInChildren<Text>().text = room.Name;
  45. newItem.GetComponentInChildren<Button>().onClick.AddListener(() => JoinRoom(_room.Name));
  46. }
  47. }
  48. void JoinRoom(string roomName)
  49. {
  50. PhotonNetwork.JoinRoom(roomName);
  51. PhotonNetwork.LoadLevel("ADMIN_CHAT");
  52. //PhotonNetwork.Instantiate("Communicator", Vector3.zero, Quaternion.identity);
  53. }
  54. public override void OnJoinedRoom()
  55. {
  56. base.OnJoinedRoom();
  57. Debug.Log("Joined success");
  58. }
  59. }