0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Parse Server Unity SDKの使い方(位置情報オブジェクトの使い方)

Posted at

ニフクラ mobile backendは3月末で終了します

ニフクラ mobile backendからの移行先として、お勧めしているのがParse Serverです。設計思想が近く、変更するコード量が少なく済むのではないかと思います。

Parse ServerではUnity向けにSDKを提供していますが、ドキュメントは英語のみとなっています。そこで、各機能の使い方を解説します。今回は位置情報オブジェクトの使い方を解説します。

Unity SDKのインストール

Unity SDKのインストールは、NuGetを使う方法が紹介されています。Unity側であらかじめ設定が必要なので、下記記事を参考にNuGetの設定を追加します。

Unity で NuGet パッケージを利用する #Unity - Qiita

そして、ParseのSDKは下記URLにて公開されています。

NuGet Gallery | parse 2.0.0-develop-1

制限

Unity SDKは、Unity 5.2.x以降が必須です。

link.xmlの作成

アセットフォルダの中に link.xml を作成し、下記内容を記述します。

<linker>
  <assembly fullname="UnityEngine">
    <type fullname="UnityEngine.iOS.NotificationServices" preserve="all"/>
    <type fullname="UnityEngine.iOS.RemoteNotification" preserve="all"/>
    <type fullname="UnityEngine.AndroidJavaClass" preserve="all"/>
    <type fullname="UnityEngine.AndroidJavaObject" preserve="all"/>
  </assembly>

  <assembly fullname="Parse.Unity">
    <namespace fullname="Parse" preserve="all"/>
    <namespace fullname="Parse.Internal" preserve="all"/>
  </assembly>
</linker>

初期化

まず Parse を読み込みます。

using Parse;

初期化は2つの方法があります。 2 の方法はマスターキーを指定もできるので、 C#としてデスクトップアプリやASP.NETの中で使う際に利用できる方法になります。

// 1. キーを指定して初期化
ParseClient client = new ParseClient("Your Application ID", "The Parse Server Instance Host URI", "Your .NET Key");

// 2. 詳細を指定して初期化
ParseClient client = new ParseClient(new ServerConnectionData
{
    ApplicationID = "Your Application ID",
    ServerURI = "The Parse Server Instance Host URI",
    Key = "Your .NET Key", // This is unnecessary if a value for MasterKey is specified.
    MasterKey = "Your Master Key",
    Headers = new Dictionary<string, string>
    {
        ["X-Extra-Header"] = "Some Value"
    }
});

位置情報オブジェクトの作成

Parseの位置情報オブジェクトの作成は ParseGeoPoint になります。1つ目の引数が緯度、二つ目が経度になります。

var point = new ParseGeoPoint(40.0, -30.0);
placeObject["location"] = point;

取得する際には Get<ParseGeoPoint> メソッドを使います。

placeObject.Get<ParseGeoPoint>("location")

位置情報検索

位置情報検索は複数のメソッドがありますが、基本は ParseObject.GetQuery を使った方法で変わりません。

// ユーザーの位置情報を取得
var userGeoPoint = ParseUser.CurrentUser.Get<ParseGeoPoint>("location");
// クエリーを作成
var query = ParseObject.GetQuery("PlaceObject");
// ユーザーの位置情報から近いデータを取得
query = query.WhereNear("location", userGeoPoint);
// 検索実行
query.FindAsync().ContinueWith(t =>
{
    IEnumerable<ParseObject> nearbyPlaces = t.Result;
});

二つの位置情報を指定する、 WhereWithinGeoBox メソッドなども用意されています。

var southwestOfSF = new ParseGeoPoint(37.708813, -122.526398);
var northeastOfSF = new ParseGeoPoint(37.822802, -122.373962);
var query = ParseObject.GetQuery("PlaceObject");
query.WhereWithinGeoBox("location", southwestOfSF, northeastOfSF)
query.FindAsync().ContinueWith(t =>
{
    IEnumerable<ParseObject> nearbyPlaces = t.Result;
});

他、位置情報検索系は以下のようなメソッドがあります。

  • WhereNear
  • WhereWithinDistance
  • WhereWithinGeoBox

まとめ

Parse Serverの位置情報オブジェクトは、NCMBと変わらず利用できます。なお、Parseにはポリゴンという複数の位置情報を利用したオブジェクトもありますが、Unity SDKでは利用できないようです。

データの管理方法などはParse ServerとNCMBで似ています。他のmBaaSと比べると、全体の修正量はそこまで多くないと思われます。載せ替え先として検討に挙げてください。

Unity Developers Guide | Parse

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?