Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using ExitGames.Client.Photon;
  2. using Photon.Pun;
  3. using Photon.Realtime;
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9.  
  10. public class NetworkManager : MonoBehaviourPunCallbacks
  11. {
  12.     string region = "Temp";
  13.  
  14.     void Awake()
  15.     {
  16.         DontDestroyOnLoad(this);
  17.  
  18.         PhotonNetwork.AutomaticallySyncScene = true;
  19.         PhotonNetwork.SendRate = 30;
  20.         PhotonNetwork.SerializationRate = 20;
  21.  
  22.         if (MainMenu.getInstance != null)
  23.             MainMenu.getInstance.SetNicknameText(PhotonNetwork.NickName);
  24.  
  25.         SceneManager.sceneLoaded += OnSceneFinishedLoaded;
  26.     }
  27.  
  28.     public override void OnConnectedToMaster()
  29.     {
  30.         Debug.Log("Connected to master");
  31.         PhotonNetwork.LoadLevel("Lobby");
  32.         PhotonNetwork.JoinLobby(new TypedLobby(region, LobbyType.SqlLobby));
  33.     }
  34.     public override void OnRoomListUpdate(List<RoomInfo> roomList)
  35.     {
  36.         Debug.Log("room list updated");
  37.  
  38.         if (Lobby.getInstance != null)
  39.             Lobby.getInstance.UpdateRoomList(roomList);
  40.     }
  41.     public override void OnLeftLobby()
  42.     {
  43.     }
  44.     public override void OnCreateRoomFailed(short returnCode, string message)
  45.     {
  46.         Debug.Log("Error on creating room: " + message);
  47.     }
  48.     public override void OnJoinRoomFailed(short returnCode, string message)
  49.     {
  50.         Debug.Log("Error on joining room:" + message);
  51.     }
  52.     public override void OnJoinRandomFailed(short returnCode, string message)
  53.     {
  54.         string roomName = "Room " + UnityEngine.Random.Range(1000, 10000);
  55.  
  56.         RoomOptions options = new RoomOptions { MaxPlayers = 8 };
  57.  
  58.         PhotonNetwork.CreateRoom(roomName, options, null);
  59.     }
  60.     public override void OnJoinedRoom()
  61.     {
  62.        PhotonNetwork.LoadLevel("Room");
  63.     }
  64.     public override void OnPlayerEnteredRoom(Photon.Realtime.Player newPlayer)
  65.     {
  66.         GameManager.getInstance.SpawnPlayer(newPlayer.ActorNumber, newPlayer.NickName, false);
  67.         Debug.Log("player entered!");
  68.     }
  69.     public override void OnPlayerLeftRoom(Photon.Realtime.Player otherPlayer)
  70.     {
  71.         GameManager.getInstance.PlayerLeft(otherPlayer.ActorNumber);
  72.     }
  73.     public override void OnLeftRoom()
  74.     {
  75.     }
  76.     void OnSceneFinishedLoaded(Scene scene, LoadSceneMode mode)
  77.     {
  78.         if (scene.name == "Room")
  79.             GameManager.getInstance.SpawnPlayer(0, PhotonNetwork.NickName, true);
  80.     }
  81.  
  82.     public void Connect(string region)
  83.     {
  84.         PhotonNetwork.PhotonServerSettings.AppSettings.FixedRegion = region;
  85.  
  86.         if (!PhotonNetwork.IsConnected)
  87.             PhotonNetwork.ConnectUsingSettings();
  88.  
  89.         this.region = region;
  90.     }
  91. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement