LoginSignup
7
9

More than 5 years have passed since last update.

ニフティクラウドmobile backendのUnity SDKで匿名ログイン処理

Posted at

概要

NCMBのUnity SDKでは匿名認証に対応していないので、それっぽいものを作ってみました。

会員管理 (Unity) : 匿名認証 | ニフティクラウド mobile backend

といっても通常の認証で、UserIDをUUIDにしているだけです。
Unity側で生成したGUIDを作成し、playerPrefsに保存しておくことでUUIDとして扱います。
パスワードは必要ないのでとりあえず共通のものを設定しています。

通常の会員登録・ログイン処理はこちらが参考になります。

チュートリアル (Unity) : ログイン機能を作る | ニフティクラウド mobile backend

NCMBの導入

NCMBの導入については、公式のチュートリアルが参考になります。

イントロダクション (Unity) : クイックスタート | ニフティクラウド mobile backend

UUIDの管理

以前作成したSingletonMonoBehaviourを継承してシングルトンにしています。

MonoBehaviourを継承したシングルトンの実装 - Qiita


using UnityEngine;
using System;
using System.Collections;

public class UUIDManager : SingletonMonoBehaviour<UUIDManager> {
    Guid guid;
    [SerializeField]
    string _uuid = "";
    public string uuid {
        get {
            if ( _uuid == "" && HasUUID() ) {
                Load();
            }
            return _uuid;
        }
        private set {
            _uuid = value;
        }
    }

    void Start () {
        if ( !HasUUID() ) {
            Create();
            Save();
        } else if (uuid == "")  {
            Load();
        }
    }

    public void Create() {
        guid = Guid.NewGuid();
        uuid = guid.ToString();
    }

    public void Save() {
        PlayerPrefs.SetString("uuid",uuid);
        PlayerPrefs.Save();
        Debug.Log("uuid Save");
    }

    public void Load() {
        uuid = PlayerPrefs.GetString("uuid");
        Debug.Log("uuid Load");
    }

    public void Delete() {
        PlayerPrefs.DeleteKey("uuid");
    }

    public bool HasUUID() {
        if (PlayerPrefs.GetString("uuid").Length > 0) 
            return true;
        return false;
    }

}

mobile backendのユーザー認証

こちらも以前作成したSingletonMonoBehaviourを継承してシングルトンにしています。

MonoBehaviourを継承したシングルトンの実装 - Qiita


using UnityEngine;
using System;
using System.Collections;
using NCMB;

public class UserAuth : SingletonMonoBehaviour<UserAuth> {
    // パスワードは適当に設定
    static string PASSWORD = "zwDyWpnKZx74xdayyhs34s"; 

    public void AnonymousLogin() {
        string id = UUIDManager.Instance.uuid;
        string pw = PASSWORD;
        NCMBUser.LogInAsync (id, pw, (NCMBException e) => {
            if( e == null ){
                Debug.Log("anonymous login");
            } else if (e.ErrorCode == NCMBException.INCORRECT_PASSWORD) {
                // ユーザーがDBに登録されていない場合は登録する
                AnonymousSignup();
            }
        });
    }

    public void AnonymousSignup() {
        NCMBUser user = new NCMBUser();
        user.UserName = UUIDManager.Instance.uuid;
        user.Password = PASSWORD;
        NCMBACL acl = new NCMBACL();
        acl.SetWriteAccess("*",true);
        acl.SetReadAccess("*",true);
        user.ACL = acl;
        user.SignUpAsync((NCMBException e) => { 
            if( e == null ){
                Debug.Log("anonymous signup");
            }
        });
    }

    public void Logout() {
        NCMBUser.LogOutAsync ( (NCMBException e) => {
            if( e == null ){
                Debug.Log("logout");
            }
        });
    }
}

使用例

認証処理を行いたいシーンに空のGameObjectを作成し、以下のスクリプトをアタッチします。


using UnityEngine;
using System.Collections;

public class LoginManager : MonoBehaviour {
    public void Start () {
        // 起動時に匿名ログイン
        UserAuth.Instance.AnonymousLogin();
    }
    void OnApplicationQuit() {
        // アプリ終了時にログアウト
        UserAuth.Instance.Logout();
    }
}
7
9
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
7
9