FC2ブログ
    11 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.» 01

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【ハルシオンブログ】Photonでサーバに「ユーザの情報」「部屋の情報」を保存、取得する方法。 

    こんにちは、坂内でっす。

    本日はPhotonのお話。

    Photonサーバ側に変数って持てるんですよね。

    例えば「自分のID」だったり「自分のアイテム所持数」だったり「ゲームの残り時間」だったり。

    あえて"自分の"と書いたのは、ID・アイテム所持数と"ゲームの"残り時間って性質が違うものなのかなと。

    ID・所持数はユーザ事の情報。
    ゲームの残り時間はその部屋の情報。

    って感じになりますよね。

    Photonでもそれぞれユーザの情報と、カレントの部屋の情報って別に持たせられます。

    【ユーザの情報】
    保存する時。


    ExitGames.Client.Photon.Hashtable properties = new ExitGames.Client.Photon.Hashtable();
    properties.Add("playerid", photonView.OwnerActorNr);
    properties.Add("itemcount", 10);
    PhotonNetwork.LocalPlayer.SetCustomProperties(properties);



    こんな感じで自分のユーザ(LocalPlayer)の情報をサーバ側に保存できます。

    もちろん永続的なものではなくて、その部屋が終わるまでなのかな。

    取得する時。


    int playerid;
    int itemcount;
    if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue("playerid", out object idObject)) {
    playerid = (int)idObject;
    }
    if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue("itemcount", out object countObject)) {
    itemcount = (int)countObject;
    }



    こんな感じで取ることができます。

    また、自分以外のユーザの情報はこんな感じで取れますよ。


    foreach(Player player in PhotonNetwork.PlayerList) {
    if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue("playerid", out object scoreObject)) {
    int playerid = (int)scoreObject;
    ~~~
    }
    }



    ここのユーザがSetCustomPropertiesで保存したデータを取得できます。

    【部屋の情報】
    書き込む時

    ExitGames.Client.Photon.Hashtable customRoomProperties = new ExitGames.Client.Photon.Hashtable();
    customRoomProperties.Add("roomtimer", 60);
    PhotonNetwork.CurrentRoom.SetCustomProperties(customRoomProperties);



    読み込む時

    int roomtimer;
    if(PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue("roomtimer", out object roomtimerObject)){
    roomtimer = (int)roomtimerObject;
    }



    こんな感じで部屋の情報を設定・取得ができます。


    って感じで、本日のPhotonの基礎的なお話は終わりです。

    では、あでゅ~ノシ

    Category: 開発日記(Unity)

    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/629-f35cb11d
    この記事にトラックバックする(FC2ブログユーザー)