Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created July 6, 2013 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/5938624 to your computer and use it in GitHub Desktop.
Save tsubaki/5938624 to your computer and use it in GitHub Desktop.
簡易FSMでPhoton Cloudを管理してみた感じ。 イベント駆動で管理しやすくなったが、ちょっと冗長
using UnityEngine;
using System.Collections;
public abstract class NetworkBase : MonoBehaviour
{
/// <summary>
/// FSMのステータスを切り替える
/// </summary>
/// <typeparam name='T'>
/// The 1st type parameter.
/// </typeparam>
public void ChangeFSM<T> () where T : NetworkBase
{
NetworkBase[] b = GetComponents<NetworkBase> ();
foreach (NetworkBase c in b) {
c.enabled = (c is T);
}
}
}
using UnityEngine;
using System.Collections;
public class NetworkConnect : MonoBehaviour
{
void OnEnable ()
{
// Photon Cloud サーバー接続
PhotonNetwork.ConnectUsingSettings ("0.1");
}
void OnJoinedLobby ()
{
// 状態”ロビー”をenableに
GetComponent<NetworkLoby> ().enabled = true;
}
}
using UnityEngine;
using System.Collections;
public class NetworkLoby : NetworkBase
{
void OnGUI ()
{
// 部屋を作る
if (GUILayout.Button ("create room", GUILayout.Height (60))) {
PhotonNetwork.CreateRoom (
null, // ユニークな部屋名
true, // リストに表示可能かどうか
true, // 入場可能か
4, // 最大人数
new Hashtable (){{"isGamePlay", false} }, // ルーム共有プロパティ
new string[] { "isGamePlay"}); // ロビーで取得可能なプロパティ一覧
}
// 部屋一覧
foreach (RoomInfo room in PhotonNetwork.GetRoomList()) {
// 部屋情報
GUILayout.Label (string.Format ("{0} player({1}/{2}) isGamePlay({3})",
room.name, //部屋名
room.playerCount, //部屋の入場人数
room.maxPlayers, //最大人数
room.customProperties ["isGamePlay"])); // 部屋のステータス
// 入室
if (room.open && GUILayout.Button ("join")) {
PhotonNetwork.JoinRoom (room);
}
}
}
void OnJoinedRoom ()
{
// 状態を”部屋”に変更
ChangeFSM<NetworkRoom> ();
}
}
using UnityEngine;
using System.Collections;
public class NetworkRoom : NetworkBase
{
public Room room{get; private set;}
void OnEnable()
{
room = PhotonNetwork.room;
}
void OnGUI ()
{
GUILayout.Label ("room name..." + room.name);
GUILayout.Label ("playercount..." + room.playerCount);
// ルーム共有プロパティを更新
if (GUILayout.Button ("isGamePlay..." + room.customProperties ["isGamePlay"])) {
Hashtable hash = room.customProperties;
hash ["isGamePlay"] = !(bool)hash ["isGamePlay"];
room.SetCustomProperties (hash);
}
// 入室可能か
if (GUILayout.Button ("isLock..." + !room.open)) {
room.open = !room.open;
}
// 部屋から抜ける
if (GUILayout.Button ("leave room")) {
PhotonNetwork.LeaveRoom ();
}
}
void OnLeftRoom ()
{
// 状態を”ロビー”に変更
ChangeFSM<NetworkLoby> ();
room = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment