「Unity/photon/pun1/helloworld」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→ロビーと部屋に入る) |
|||
行3: | 行3: | ||
#AutoJoinLobbyにチェックを入れる | #AutoJoinLobbyにチェックを入れる | ||
#ヒエラルキーにGameObjectを作成して | #ヒエラルキーにGameObjectを作成して | ||
− | # | + | #以下LobbyScript.csを貼り付ける |
+ | #以下PhotonManager.csを作成 | ||
+ | LobbyScript.cs | ||
+ | using System.Collections; | ||
+ | using System.Collections.Generic; | ||
+ | using UnityEngine; | ||
+ | public class LobbyScript : MonoBehaviour { | ||
+ | PhotonManager manager; | ||
+ | // Use this for initialization | ||
+ | void Start () { | ||
+ | manager = PhotonManager.Instance; | ||
+ | manager.Start (); | ||
+ | } | ||
+ | // Update is called once per frame | ||
+ | void Update () { | ||
+ | } | ||
+ | } | ||
− | + | PhotonManager.cs | |
using System.Collections; | using System.Collections; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
using UnityEngine; | using UnityEngine; | ||
− | public class | + | public class PhotonManager : Photon.MonoBehaviour { |
+ | private static PhotonManager mInstance; | ||
+ | private PhotonManager () { | ||
+ | } | ||
+ | public static PhotonManager Instance { | ||
+ | get { | ||
+ | if (mInstance == null) { | ||
+ | GameObject go = new GameObject("PhotonManager"); | ||
+ | mInstance = go.AddComponent<PhotonManager>(); | ||
+ | } | ||
+ | return mInstance; | ||
+ | } | ||
+ | } | ||
// Use this for initialization | // Use this for initialization | ||
void Start () { | void Start () { |
2017年10月20日 (金) 10:57時点における版
ロビーと部屋に入る
- importしたプロジェクト内のPhotonUnityNetwork/Resource/PhotonServerSettingのInspectorを開く
- AutoJoinLobbyにチェックを入れる
- ヒエラルキーにGameObjectを作成して
- 以下LobbyScript.csを貼り付ける
- 以下PhotonManager.csを作成
LobbyScript.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LobbyScript : MonoBehaviour { PhotonManager manager; // Use this for initialization void Start () { manager = PhotonManager.Instance; manager.Start (); } // Update is called once per frame void Update () { } }
PhotonManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PhotonManager : Photon.MonoBehaviour { private static PhotonManager mInstance; private PhotonManager () { } public static PhotonManager Instance { get { if (mInstance == null) { GameObject go = new GameObject("PhotonManager"); mInstance = go.AddComponent<PhotonManager>(); } return mInstance; } } // Use this for initialization void Start () { PhotonNetwork.ConnectUsingSettings("v1.0"); } // Update is called once per frame void Update () { } void OnJoinedLobby () { Debug.Log ("OnJoinedLobby"); CreateRoom (); } public void CreateRoom(){ string userName = "ユーザ1"; string userId = "user1"; string roomId = "room1"; PhotonNetwork.autoCleanUpPlayerObjects = true; // 離脱後自動破棄するかどうか ExitGames.Client.Photon.Hashtable customProp = new ExitGames.Client.Photon.Hashtable(); customProp.Add ("userName", userName); customProp.Add ("userId", userId); PhotonNetwork.SetPlayerCustomProperties(customProp); RoomOptions roomOptions = new RoomOptions (); roomOptions.customRoomProperties = customProp; roomOptions.customRoomPropertiesForLobby = new string[]{ "userName","userId"}; roomOptions.maxPlayers = 5; // 部屋の最大人数 roomOptions.isOpen = true; // 入室許可する roomOptions.isVisible = true; // ロビーから見えるようにする PhotonNetwork.JoinOrCreateRoom (roomId, roomOptions, null); } // ルーム入室した時 void OnJoinedRoom() { Debug.Log ("OnJoinedRoom"); } // ルーム一覧が取れた時 void OnReceivedRoomListUpdate(){ RoomInfo[] rooms = PhotonNetwork.GetRoomList(); // 注意:OnReceivedRoomListUpdate()が呼ばれた以後でしか取得できない if (rooms.Length == 0) { Debug.Log ("ルームが一つもありません"); } else { foreach (RoomInfo room in rooms) { Debug.Log ("RoomName:" + room.name); Debug.Log ("userName:" + room.customProperties["userName"]); Debug.Log ("userId:" + room.customProperties["userId"]); } } } }
接続を検証するために1つはプレビュー版、もう一つはアプリにビルドしたものを動作させておく。
部屋に入りたいだけ
PhotonNetwork.JoinRoom("room1");