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の使い方(データストアオブジェクトのCRUD操作)

Last updated at Posted at 2024-03-06

ニフクラ 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"
    }
});

データの保存

データを保存する際には SaveAsync を使います。これでTaskが返ってきます。

ParseObject gameScore = new ParseObject("GameScore");
gameScore["score"] = 1337;
gameScore["playerName"] = "Sean Plott";
Task saveTask = gameScore.SaveAsync();

データの取得

objectIdが分かっている場合は、 FetchAsync メソッドでデータを取得できます。

Task<ParseObject> fetchTask = myObject.FetchAsync();

フィールドデータの取得

フィールドのデータを取得する際には、 Get メソッドを使います。 <型> で型を指定します。

int number = myObject.Get<int>("myNumber");
string str = myObject.Get<string>("myString");
DateTime date = myObject.Get<DateTime>("myDate");
byte[] data = myObject.Get<byte[]>("myData");
IList<object> list = myObject.Get<List<object>>("myList");
IDictionary<string, object> dictionary = myObject.Get<Dictionary<string, object>>("myDictionary");
Debug.Log ("Number: " + number);
Debug.Log ("String: " + str);
Debug.Log ("Date: " + date);
string dataString = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
Debug.Log ("Data: " + dataString);
foreach (var item in list) {
    Debug.Log ("Item: " + item.ToString());
}
foreach (var key in dictionary.Keys) {
    Debug.Log ("Key: " + key + " Value: " + dictionary[key].ToString());
}

データの更新

データの更新も SaveAsync メソッドを使います。

// オブジェクトの保存
var gameScore = new ParseObject("GameScore")
{
    { "score", 1337 },
    { "playerName", "Sean Plott" },
    { "cheatMode", false },
    { "skills", new List<string> { "pwnage", "flying" } },
};
gameScore.SaveAsync().ContinueWith(t =>
{
    // 更新処理
    gameScore["cheatMode"] = true;
    gameScore["score"] = 1338;
    gameScore.SaveAsync();
});

データの削除

データの削除は DeleteAsync メソッドを使います。

Task deleteTask = myObject.DeleteAsync();

まとめ

Parse ServerとNCMBのデータストア操作は、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?