Last active
November 22, 2019 13:29
-
-
Save valbeat/74d59e470a41f0758eb8 to your computer and use it in GitHub Desktop.
ニフティクラウドmobile backendのUnity SDKで匿名ログイン処理 ref: https://qiita.com/kajitack/items/3ca88a20817962684a8c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class LoginManager : MonoBehaviour { | |
public void Start () { | |
// 起動時に匿名ログイン | |
UserAuth.Instance.AnonymousLogin(); | |
} | |
void OnApplicationQuit() { | |
// アプリ終了時にログアウト | |
UserAuth.Instance.Logout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment