Created
January 25, 2018 10:53
-
-
Save nickyeh97/22fed7b86b7814438ace3ec655d2928e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Awake()//載入場景 | |
{ | |
if (instance != null) | |
{ | |
DestroyImmediate(gameObject); | |
return; | |
} | |
DontDestroyOnLoad(gameObject); | |
instance = this; | |
// 讓 PUN 能自動同步載入場景, 可以避免在載入場景初始化時發生一些網路問題. | |
// PhotonNetwork.LoadLevel() 的時候Host與Client能同时進入新的場景 | |
PhotonNetwork.automaticallySyncScene = true; | |
} | |
private void Start()//連上Photon Cloud | |
{ | |
//UserName | |
SetPlayerName(); | |
//將PUN連上Photon Cloud | |
PhotonNetwork.ConnectUsingSettings("BattleGround_" + _gameVersion); | |
} | |
/// <summary> | |
/// 連接大廳失败 | |
/// </summary> | |
/// <param name="error"></param> | |
private void OnFailedToConnect(NetworkConnectionError error) | |
{ | |
Debug.Log("fail to Connect"); | |
} | |
private void SetPlayerName() | |
{ | |
PhotonNetwork.player.NickName = PlayerPrefs.GetString("Username", "我愛PUBG"); | |
UserNameField.text = PhotonNetwork.player.NickName; | |
if (UserNameField.text == "我愛PUBG" || string.IsNullOrWhiteSpace(UserNameField.text)) | |
{ | |
UserNameField.text = "我愛PUBG"; | |
} | |
else | |
{ | |
UserNameField.gameObject.SetActive(false); | |
UserName.gameObject.SetActive(true); | |
UserName.text = PhotonNetwork.player.NickName; | |
} | |
} | |
private void OnDestroy() | |
{ | |
PlayerPrefs.SetString("Username", UserNameField.text); | |
} | |
private void OnGUI() | |
{ | |
int ping = PhotonNetwork.GetPing(); | |
GUI.Label(new Rect(10, 10, 200, 30), PhotonNetwork.connectionStateDetailed.ToString() + "," + ping + " ms"); | |
} | |
/// <summary> | |
/// Called after the connection to the master is established and authenticated | |
/// but only when PhotonNetwork.autoJoinLobby is false.If you set PhotonNetwork.autoJoinLobby to true, | |
/// OnJoinedLobby() will be called instead of this. | |
/// </summary> | |
public override void OnConnectedToMaster() | |
{ | |
// 連上 MasterServer 後, Button (UI) 就可以顯示或是執行其它後續動作. | |
CreateBtn.SetActive(true); | |
Debug.Log("已連上 Master Server"); | |
// AUTO Join Room for testing! | |
JoinGameRoom(); | |
} | |
/// <summary> | |
/// 創建房间 | |
/// </summary> | |
public void CreateARoom() | |
{ | |
if (PhotonNetwork.connected) | |
{ | |
//創建房间成功 | |
if (PhotonNetwork.CreateRoom(UserNameField.text + " 的對戰室 ", new RoomOptions { MaxPlayers = 16 }, null)) | |
{ | |
Debug.Log("PhotonManager.CreateARoom 成功"); | |
} | |
} | |
} | |
/// <summary> | |
/// 加入已知房間房间 | |
/// </summary> | |
public void JoinGameRoom() | |
{ | |
RoomOptions options = new RoomOptions(); | |
options.MaxPlayers = 16; | |
PhotonNetwork.JoinOrCreateRoom(UserNameField.text + "'s Fighting Room", options, TypedLobby.Default); | |
} | |
public override void OnJoinedRoom() | |
{ | |
Debug.Log(PhotonNetwork.isMasterClient); | |
Debug.Log("您已進入遊戲室!!"); | |
PhotonNetwork.player.NickName = UserNameField.text; | |
//如果是Master Client, 即可建立/ 初始化,與載入遊戲場景 | |
if (PhotonNetwork.isMasterClient) | |
{ | |
SceneManager.sceneLoaded += this.OnLevelFinishedLoading; | |
PhotonNetwork.LoadLevel("GameRoomScene"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment