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"
    }
});

ユーザー登録

ユーザー登録の基本はユーザーIDとパスワードです。以下のコードで登録できます。

var user = new ParseUser()
{
    Username = "my name",
    Password = "my pass",
    Email = "email@example.com"
};

// 他のフィールドも指定できます
user["phone"] = "415-392-0202";

Task signUpTask = user.SignUpAsync();

ログイン

ログインは以下のコードで行います。

ParseUser.LogInAsync("myname", "mypass").ContinueWith(t =>
{
    if (t.IsFaulted || t.IsCanceled)
    {
        // ログイン失敗
    }
    else
    {
        // ログイン成功
    }
})

メールアドレス認証

メールアドレスを使う場合には、 emailVerified というフィールドを追加して、独自に実装する必要があるようです。

ログイン判定

ログイン判定は以下のコードで行います。

if (ParseUser.CurrentUser != null)
{
    // ログイン中
}
else
{
    // 未ログイン
}

ログアウト

ログアウトは以下のコードで行います。

ParseUser.LogOut();

匿名認証

NCMBでいう匿名認証も、Parse Serverに用意されています。ただし、SDK側には専用のメソッドはありません。

ソーシャル認証

ソーシャル認証用に、以下のクラスが用意されています。

  • ParseFacebookUtils

他の認証方法については専用のクラスがなかったので、 ParseUser.logInWithInBackground を使って実装する必要があります。こちらは同期処理用のメソッドがありませんでした。

Task<ParseUser> logInTask = ParseFacebookUtils.LogInAsync(userId, accessToken, tokenExpiration);

まとめ

Parse ServerとNCMBの認証周りは、Unity SDK同士では大きくは異なりません。載せ替える際には、大きなコードにはならなそうです。ただし基本の認証は問題なさそうですが、メールアドレスログインがないこと、Facebook以外のソーシャルログインがないのが気になりました。

データの管理方法などは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?