LoginSignup
4
4

More than 5 years have passed since last update.

【PUN】ログイン

Last updated at Posted at 2017-12-23

Photon Unity Networkの基本機能メモ

【PUN】ログイン
【PUN】アバター生成・同期
【PUN】ゲーム終了判定


公式
https://www.photonengine.com/ja-JP/Photon

基本説明
http://doc-api.photonengine.com/ja-jp/pun/current/general.html

Class List
https://doc-api.photonengine.com/ja-jp/pun/current/annotated.html

201 1 PhotonEngineとは
こちらの資料をベースに進める

【Unity】僕もPhotonを使いたい - うら干物書き
こちらのシリーズも勉強になる


ログイン

  • ロビーに接続
  • ロビーに入室
  • ルームを作成
  • ルームに入室

Alt + Pで[PUN Wizard]を表示
[Locate PhotonServerSettings]から
[Auto-Join Lobby]にチェックを入れてロビーまで自動で入室

以下スクリプトを、シーン中の任意のゲームオブジェクトにアタッチする

PhotonLogin.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhotonLogin : MonoBehaviour {

    private const string ROOM_NAME = "VR-Room";
    private const string GAME_VERSION = "v1.0";

    void Start () {
        Debug.Log ("PhotonManager: ロビーに接続");
        PhotonNetwork.ConnectUsingSettings (GAME_VERSION);
    }

    void OnJoinedLobby() {
        Debug.Log ("PhotonManager: ロビーに入室成功");
        RoomOptions roomOptions = new RoomOptions () {
            MaxPlayers = 20,
            IsOpen = true,
            IsVisible = true,
        };
        PhotonNetwork.JoinOrCreateRoom (ROOM_NAME, roomOptions, null);
    }

    void OnJoinedRoom() {
        Room room = PhotonNetwork.room;
        PhotonPlayer player = PhotonNetwork.player;
        Debug.Log ("PhotonManager: ルーム入室成功 部屋名:" + room.Name + ", プレイヤーID:" + player.ID);
        Debug.Log ("PhotonManager: 部屋情報:" + room + ", ルームマスター?:" + player.IsMasterClient);
    }

    void OnPhotonJoinRoomFailed() {
        Debug.Log("PhotonManager: ルーム入室失敗");
    }

    void OnPhotonCreateRoomFailed() {
        Debug.Log("PhotonManager: ルーム作成失敗");
    }
}

詳細情報を表示

PhotonNetwork.connectionStateDetailed

以下スクリプトを、シーン中の任意のゲームオブジェクトにアタッチする

PhotonStatus.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhotonStatus : MonoBehaviour {

    void OnGUI() {
        string status = "Photon: " + PhotonNetwork.connectionStateDetailed.ToString() + "\n";
        if(PhotonNetwork.inRoom){
            status += "---------------------------------------------------";
            status += "Room Name: " + PhotonNetwork.room.Name + "\n";
            status += "Player Num: " + PhotonNetwork.room.PlayerCount + "\n";
            status += "---------------------------------------------------";
            status += "Player Id: " + PhotonNetwork.player.ID + "\n";
            status += "IsMasterClient: " + PhotonNetwork.isMasterClient + "\n";
        }
        GUI.TextField (new Rect (10, 10, 220, 120), status);
    }

}
4
4
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
4
4