LoginSignup
1
7

More than 3 years have passed since last update.

Photonに接続する(PUN2編)

Posted at

はじめに

以前にPUNを使っての接続方法を投稿しましたが、PUN2でもなんとかできるようになったので記事を書きます。前回の内容と被る部分のあるのでそちらを参照してください。PUNをインポート後AppIDを入力するところまでは、PUN2をインポートする以外は同じになります。

Photonに接続する
https://qiita.com/uroshinse/items/004b47562e202be10b72

PUN2(Photon Unity Network 2)をインポートする

image.png

PhotonServerSettings

Settingsはインポート後AppID入力がうまくいけば、App Id Realtimeに自動的に反映されているので確認してください。入力しなかった場合は必ず入力してください。

image.png

部屋の作成から接続までのスクリプト

ヒエラキー上にPhoton(空のGameObject)を作成して、以下のスクリプトを作成後Photon(空のGameObject)にスクリプトをアタッチする。

using UnityEngine;
using Photon.Pun;
using Photon.Realtime;


namespace PhotonPractice{
public class PhotonLogin : MonoBehaviourPunCallbacks {

    //ゲームバージョン指定(設定しないと警告が出る)
    string GameVersion = "Ver1.0";

    //ルームオプションのプロパティー
    static RoomOptions RoomOPS = new RoomOptions()
    {
        MaxPlayers = 2, //0だと人数制限なし
        IsOpen = true, //部屋に参加できるか
        IsVisible = true, //この部屋がロビーにリストされるか
    };



    // Use this for initialization
    void Start () {

        //PhotonCloudに接続
        Debug.Log("PhotonLoing");
        //ゲームバージョン設定
        PhotonNetwork.GameVersion = GameVersion; 
        //PhotonServerSettingsファイルで構成されたPhotonに接続。
        PhotonNetwork.ConnectUsingSettings();

    }

    //クライアントがマスターサーバーに接続されたときに呼び出される。
    public override void OnConnectedToMaster()
    {

        //ルームへの参加 or 新規作成
        PhotonNetwork.JoinOrCreateRoom("Photonroom",RoomOPS, null); //("ルームの名前",ルームオプションの変数,新規ルームを一覧したいロビー。nullで無視)

    }

    //ルーム作成して入室に成功したときに呼び出される。
    public override void OnJoinedRoom()
    {
        //Room型とPlayer型の指定。
        Room myroom = PhotonNetwork.CurrentRoom; //myroom変数にPhotonnetworkの部屋の現在状況を入れる。
        Photon.Realtime.Player player = PhotonNetwork.LocalPlayer; //playerをphotonnetworkのローカルプレイヤーとする
        Debug.Log("ルーム名:" + myroom.Name);
        Debug.Log("PlayerNo" + player.ActorNumber);
        Debug.Log("プレイヤーID" + player.UserId);


        //この部分はニックネームを決めるためのもので、入力は不要です。
        if(player.ActorNumber  == 1)
        {
            player.NickName = "わたしは1です";
        }

        Debug.Log("プレイヤー名" + player.NickName);
        Debug.Log("ルームマスター" + player.IsMasterClient); //ルームマスターならTrur。最初に部屋を作成した場合は、基本的にルームマスターなはず。

    }

    //入室失敗したときに呼び出される動作。
    public override void OnJoinRandomFailed(short returnCode, string message) {

        Debug.Log("入室失敗");
        //ルームを作成する。
        PhotonNetwork.CreateRoom(null, RoomOPS); //JoinOrCreateroomと同じ引数が使用可能。nullはルーム名を作成したくない場合roomNameを勝手に割り当てる。
    }

    //ルーム作成失敗したときの動作。
    public override void OnCreateRoomFailed(short returnCode, string message) {

        Debug.Log("作成失敗");

    }

}

}

再生ボタンを押して以下の画像が表示されれば接続成功です。

image.png

おわりに

今回は接続のみでしたが、次回はプレイヤー操作も含めた記事を書く予定です。

参考

VR-studies:https://github.com/yumemi-inc/vr-studies/wiki
ドキュメント:https://doc-api.photonengine.com/en/pun/v2/index.html

1
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
7