①プロジェクトウィンドウに新規のC#スクリプトを作成し、[Launcher]という名前を付けます。
②[Launcher]を次のように編集します。
using System; using Photon.Pun; using UnityEngine; namespace Com.MyCompany.MyGame { public class Launcher : MonoBehaviour { #region Private Serializable Fields #endregion #region Private Fields /// <summary> /// This client`s version number. Users are separated from each other by gameVersion(which allows you to make breaking changers). /// </summary> string gameVersion = "1"; #endregion #region Monobihaviour CallBackes private void Awake() { //#Critical //this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically PhotonNetwork.AutomaticallySyncScene = true; } /// <summary> /// MonoBehaviour method called on GameObject by Unity during initialization /// </summary> void Start() { Connect(); } #endregion #region Public Methods /// <summary> /// Start the connection process. /// If already connected,we attempt joining a random room /// If not yet connected,Connect this application instance to Photon Cloud /// </summary> void Connect() { //we check if we are connected or not, we hoin if we are , else we initialize if (PhotonNetwork.IsConnected) { //#Critical we need at this point ot attempt hoining a Random Room. PhotonNetwork.JoinRandomRoom(); } else { //#Critical, we must first and foremost connect to Photon Online Serer PhotonNetwork.GemeVersion = gameVersion; PhotonNetwork.ConnectUsingSettings(); } } #endregion } }
●スクリプトの説明
namespace Com.MyCompany.MyGame { ... }
ネームスペースを付けることでこの[Launcher]コンポーネントが他の[Launcher]コンポーネントと同一に判断されず唯一のものに区別されます。
private void Awake() { PhotonNetwork.AutomaticallySyncScene = true; }
マスタークライアントでPhotonNetwork.LoadLevel()を使用し同じルームのクライアントが自動的に同期できるように設定します。
void Start() { Connect(); }
Start関数ではそのままConnectメソッドへ飛んでいます。
●Start関数とAwake関数の違い。
Start関数とAwake関数はどちらも最初に一回呼ばれるメソッドですが、二つを比較するとAwakeが先に呼ばれStart関数が呼ばれます。
呼ばれる順番に違いがあるようです
参考
void Connect() { //we check if we are connected or not, we hoin if we are , else we initialize if (PhotonNetwork.IsConnected) { //#Critical we need at this point ot attempt Joining a Random Room. PhotonNetwork.JoinRandomRoom(); } else { //#Critical, we must first and foremost connect to Photon Online Serer PhotonNetwork.GameVersion = gameVersion; PhotonNetwork.ConnectUsingSettings(); } }
Connectメソッドではその名の通りPhotonの接続を行います。
PhotonNetwork.JoinRandomRoom();
現在接続されている場合フォトンサーバーに接続します。
PhotonNetwork.GameVersion = gameVersion; PhotonNetwork.ConnectUsingSettings();
③前回作成した空のゲームオブジェクト[Launcher]オブジェクトにスクリプトをアタッチします。
④Unityの上部ワールドタブから[Window]→[Photon Unity Networking]→[Highlight Photon Server Settings]を開きます。
⑤[PUN Logging]を[Full]に指定します。
これでPhotonに接続する準備が完了しました。
⑥Unityエディタを実行します。
リージョンを検索し、JPであることを処理したログが表示されます。
PUN got region list. Going to ping minimum regions, based on this previous result summary:
Region Pinging Result: jp[5.8.70.143:5055]: 9ms
〇コールバックの実装
コールバックを実装するために[Launcher]スクリプトを開き[MonoBehaviour]を[MonoBehaviourPunCallbacks]にベースクラスを書き換えます。
public class Launcher : MonoBehaviourPunCallbacks {
[Photon.Realtime]を追加します。
using System; using Photon.Pun; using UnityEngine; using Photon.Realtime; namespace Com.MyCompany.MyGame { ...
次にコールバックの処理をクラスの最後に加えます。
#region MonoBehaviourPunCallbacks Callbacks public override void OnConnectedToMaster() { Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN"); } public override void OnDisconnected(DisconnectCause cause) { Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was Called by PUN with reason {0}",cause); } #endregion
この状態でUnityのエディタ上で実行することでPhotonのコールバックを取得することができます。
このコールバックはユーザーに接続状況を知らせることなど様々な場面で使用できます。
[OnConnectedToMaster()]にPhotonNetworkへの接続処理を加えます。
public override void OnConnectedToMaster() { Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN"); //#Critical: The first we try to do is to hoin a potential existing room.If there is, good, else, we'll be called back with OnJoinRandomFailed() PhotonNetwork.JoinRandomRoom(); }
ルームの参加に失敗した場合その旨を通知する必要があります。
次の文をクラスに追加します。
public override void OnJoinRandomFailed(short returnCode, string message) { Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom"); // #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room. PhotonNetwork.CreateRoom(null, new RoomOptions()); } public override void OnJoinedRoom() { Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room."); }
これをUnityエディタで実行します。
するとルームに接続できたログが表示されます。
これでルームに入るためのロビーが完成しました。