Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active January 3, 2016 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayuki5160/8489056 to your computer and use it in GitHub Desktop.
Save masayuki5160/8489056 to your computer and use it in GitHub Desktop.
Photon Unity Networking Free内のソースよんだので覚え書きいろいろ。 Photon Unity Networking/Demos/MacroPolo
using UnityEngine;
using System.Collections;
// プレイヤーIDをプレイヤーの上に表示させる処理がメイン
public class GameLogic : MonoBehaviour
{
public static int playerWhoIsIt = 0;
private static PhotonView ScenePhotonView;
// Use this for initialization
public void Start()
{
ScenePhotonView = this.GetComponent<PhotonView>();
}
public void OnJoinedRoom()
{
// game logic: if this is the only player, we're "it"
if (PhotonNetwork.playerList.Length == 1)
{
playerWhoIsIt = PhotonNetwork.player.ID;
}
Debug.Log("playerWhoIsIt: " + playerWhoIsIt);
}
public void OnPhotonPlayerConnected(PhotonPlayer player)
{
Debug.Log("OnPhotonPlayerConnected: " + player);
// when new players join, we send "who's it" to let them know
// only one player will do this: the "master"
if (PhotonNetwork.isMasterClient)
{
TagPlayer(playerWhoIsIt);
}
}
public static void TagPlayer(int playerID)
{
Debug.Log("TagPlayer: " + playerID);
ScenePhotonView.RPC("TaggedPlayer", PhotonTargets.All, playerID);
}
[RPC]
public void TaggedPlayer(int playerID)
{
playerWhoIsIt = playerID;
Debug.Log("TaggedPlayer: " + playerID);
}
public void OnPhotonPlayerDisconnected(PhotonPlayer player)
{
Debug.Log("OnPhotonPlayerDisconnected: " + player);
if (PhotonNetwork.isMasterClient)
{
if (player.ID == playerWhoIsIt)
{
// if the player who left was "it", the "master" is the new "it"
TagPlayer(PhotonNetwork.player.ID);
}
}
}
public void OnMasterClientSwitched()
{
Debug.Log("OnMasterClientSwitched");
}
}
using UnityEngine;
// プレイヤーの動作をクライアント間でどうきさせる処理いろいろ
public class NetworkCharacter : Photon.MonoBehaviour
{
private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
// Update is called once per frame
void Update()
{
// 自分以外のプレイヤーの場合はデータ補正(Vector3.Lerp、Quaternion.Lerpを使うとなめらかに動作してくれる)
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
}
}
// プレイヤーのデータを送受信
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// データの送信
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
myThirdPersonController myC = GetComponent<myThirdPersonController>();
stream.SendNext((int)myC._characterState);
}
else
{
// データの受信
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
myThirdPersonController myC = GetComponent<myThirdPersonController>();
myC._characterState = (CharacterState)stream.ReceiveNext();
}
}
}
using UnityEngine;
public class RandomMatchmaker : Photon.MonoBehaviour
{
private PhotonView myPhotonView;
// Use this for initialization
void Start()
{
// Wizardで設定ししたAppID,リージョンでロビーに接続する
// 接続が完了するとOnJoinedLobby()をコール
PhotonNetwork.ConnectUsingSettings("0.1");
}
// PhotonNetwork.ConnectUsingSettings()でロビー接続が完了したときによばれるメソッド
void OnJoinedLobby()
{
Debug.Log("JoinRandom");
// ランダムにルームへ接続
// ルームがない場合はOnPhotonRandomJoinFailed()をコール
PhotonNetwork.JoinRandomRoom();
}
// PhotonNetwork.JoinRandomRoom()でルームがなかったときにコールされるメソッド
void OnPhotonRandomJoinFailed()
{
// ルームがなかったのでルームを作成
// ただ今回はランダムでルームへjoinしているだけなのでルーム名を指定して作成していない。
PhotonNetwork.CreateRoom(null);
}
// ルームへの接続が完了するとコールされるメソッド
// ここにゲームをはじめるにあたって必要な処理を記述
void OnJoinedRoom()
{
// プレイヤーのインスタンス化
GameObject monster = PhotonNetwork.Instantiate("monsterprefab", Vector3.zero, Quaternion.identity, 0);
monster.GetComponent<myThirdPersonController>().isControllable = true;
myPhotonView = monster.GetComponent<PhotonView>();
}
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
if (PhotonNetwork.connectionStateDetailed == PeerState.Joined)
{
bool shoutMarco = GameLogic.playerWhoIsIt == PhotonNetwork.player.ID;
if (shoutMarco && GUILayout.Button("Marco!"))
{
myPhotonView.RPC("Marco", PhotonTargets.All);
}
if (!shoutMarco && GUILayout.Button("Polo!"))
{
myPhotonView.RPC("Polo", PhotonTargets.All);
}
}
}
}
@masayuki5160
Copy link
Author

【参考】
Unity 4.3 x Photon Cloud 2Dオンラインアプリチュートリアル(前編)
http://dev.classmethod.jp/etc/unity-photon-2d-physics-online-game-01/

Unity 4.3 x Photon Cloud 2Dオンラインアプリチュートリアル(後編)
http://dev.classmethod.jp/etc/unity-photon-2d-physics-online-game-02/

@masayuki5160
Copy link
Author

【参考】
[Unity3D]NetworkViewを使って通信対戦 その6
http://terasur.blog.fc2.com/blog-entry-72.html

NetworkViewの話だけどRPCはこちらが参考になりました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment