Unity/photon/pun2/helloworldのソースを表示
←
Unity/photon/pun2/helloworld
ナビゲーションに移動
検索に移動
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
LobbyScene.cs <pre> using UnityEngine; using UnityEngine.UI; public class LobbyScene : MonoBehaviour { PhotonManager photonManager; void Start() { GameObject.Find("InputField").GetComponent<InputField>().text = "room1"; GameObject.Find("Button").GetComponent<Button>().onClick.AddListener(OnClick); } void OnResponseConnect(PhotonManager.ResponseType responseType) { if (responseType.Equals(PhotonManager.ResponseType.JoinedRoom)) { // SceneManager.LoadScene("RpcViewScene"); } } void OnClick() { string roomId = GameObject.Find("InputField").GetComponent<InputField>().text; photonManager = PhotonManager.Instance; photonManager.AddCallback(OnResponseConnect); photonManager.roomId = roomId; photonManager.userId = "user" + Random.RandomRange(1, 1000); photonManager.ConnectStart(); } } </pre> PhotonManager.cs <pre> using System.Collections.Generic; using Photon.Pun; using Photon.Realtime; using UnityEngine; using UnityEngine.SceneManagement; using System.Linq; public class PhotonManager : MonoBehaviourPunCallbacks { public string roomId = "room1"; public string userId = "user1"; 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; } } public enum ResponseType { JoinedRoom, } public delegate void PhotonCallback(ResponseType responseType); private PhotonCallback photonCallbacks; public void AddCallback(PhotonCallback callback) { photonCallbacks += callback; } public void ConnectStart() { Debug.Log("ConnectStart"); if (PhotonNetwork.IsConnected == false) { PhotonNetwork.ConnectUsingSettings(); PhotonNetwork.GameVersion = "v1.0"; } } // マスターサーバーに接続した時 public override void OnConnectedToMaster() { Debug.Log("OnConnectedToMaster"); if (PhotonNetwork.IsConnected) { PhotonNetwork.JoinLobby(); } } public override void OnJoinedLobby() { Debug.Log("OnJoinedLobby"); // PhotonNetwork.JoinRandomRoom(); CreateRoom(); } public void CreateRoom() { string userName = "ユーザ" + userId; 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); } public void JoinPrivateRoom(string roomName) { PhotonNetwork.JoinRoom("p" + roomName); JoinPrivateRoomName = roomName; } // JoinRoom()呼び出し、失敗時 public override void OnJoinRoomFailed(short returnCode, string message) { Debug.Log("PhotonManager OnPhotonJoinRoomFailed " + returnCode + " " + message); } // ルーム入室した時 public override void OnJoinedRoom() { Debug.Log("OnJoinedRoom"); RoomInfo r = PhotonNetwork.CurrentRoom; Debug.Log("roomname=" + r.Name); // 部屋名 Debug.Log("PlayerCount=" + r.PlayerCount); // 現在の人数 Debug.Log("maxPlayer=" + r.MaxPlayers); // 最大人数 Debug.Log("userId=" + userId); SceneManager.LoadScene("RpcViewScene"); } public override void OnRoomListUpdate(List<RoomInfo> roomList) { Debug.Log("OnRoomListUpdate"); List<RoomInfo> tmpRoomList = roomList.Where(s => s.IsOpen).ToList(); if (tmpRoomList.Count == 0) { Debug.Log("ルームが一つもありません"); } else { foreach (RoomInfo room in tmpRoomList) { Debug.Log("RoomName:" + room.Name); Debug.Log("PlayerName:" + room.CustomProperties["PlayerName"]); } } } public override void OnJoinRandomFailed(short returnCode, string message) { string userName = "ユーザ" + userId; 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 = 2; // 部屋の最大人数 roomOptions.IsOpen = true; // 入室許可する roomOptions.IsVisible = true; // ロビーから見えるようにする // roomnameをnullとすると7d60e607-263f-4360-8bf9-51b3d4ae2348な感じのnameになる PhotonNetwork.CreateRoom(null, roomOptions, null); } // 自分以外が入室 public override void OnPlayerEnteredRoom(Player player) { Debug.Log("OnPlayerEnteredRoom " + player.NickName + " is joined."); // PhotonNetwork.PlayerListOthers } // 自分でないメンバーを一人返す public string OtherMemberName() { foreach (var p in PhotonNetwork.PlayerListOthers) { if (!PhotonNetwork.NickName.Equals(p.NickName)) { return p.NickName; } } return ""; } public void Disconnect() { if (PhotonNetwork.IsConnected) { PhotonNetwork.LeaveRoom(); } PhotonNetwork.Disconnect(); } } </pre> ==画面遷移時でも使えるように== DontDestroyOnLoadを追加する <pre> GameObject go = new GameObject("PhotonManager"); mInstance = go.AddComponent<PhotonManager>(); DontDestroyOnLoad(go); </pre> ==その他== 公式マニュアル:https://doc-api.photonengine.com/ja-jp/pun/current/class_photon_1_1_pun_1_1_mono_behaviour_pun_callbacks.html OnCreatedRoom() このクライアントがルームを作成して入室すると呼び出されます。 OnJoinedRoom ()も呼び出されます
Unity/photon/pun2/helloworld
に戻る。
ナビゲーション メニュー
個人用ツール
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
ソースを閲覧
履歴表示
その他
検索
案内
プログラムメモ
php
flutter
java
android
kotlin
ios
unity
unrealengine
javascript
mysql
sqlite
postgresql
oracle
mroonga
mongodb
flash
electron
cocos2dx
titanium
cpp
ruby
perl
python
accessメモ
rss
html
monaca
cordova
golang
blender
セキュリティ
テストツール
サーバメモ
linux
dotnet
apacheメモ
htaccessメモ
subversion
git
仮想サーバ
ansible
sendgrid
xampp
cacti
mecab
faces
flashpolicyd
fcs
jenkins
運用
デザインメモ
css
ユーザビリティ
ux
サービスメモ
twitter
facebook
instagram
mixi
セカンドライフ
通信ログ横取り
google
ustream
aws
gcp
plesk
azure
vps
AI
その他サービス
便利系メモ
SEO
モバイル
抽象変数名
DDD
クライアント
firefox
chrome
pgp
windows
mac
jmetar
Thunderbird
excel
libreoffice
vpnclient
doxygen
VisualStudioCode
fastlane
metaquest
cmsメモ
mediawiki
pukiwiki
wordpress
その他
資格
IT用語
pvを稼ぐ方法
将棋プログラム
その他
ログイン
ページ内
メインページ
最近の更新
人気のページ
問い合わせ
ツール
リンク元
関連ページの更新状況
ページ情報