前回の続きです。
今回はUnity側の実装です。
まず、流れの説明。
1:アプリ起動時にローカルからID・PASSを読み取る。
2:読み取ったID・PASSをPHP側に送って認証する。
3:ローカルから読み取れなければ初回起動なのでランダムに生成してデータベースに登録&ローカルに保存
※ローカルに保存してるので改ざんの可能性もありますが、今回は無視します。
※また、通信状況が悪くて通信できなかった場合の処理も無視します。
■まずは状態を管理するユーザーマネージャを用意します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public static class UserManager { //DBに送信する情報 static private string ID; static private string PASS; static private string LEVEL; static private string TEMPID; static private string TEMPPASS; static private string LOGINTIME; //DB処理の状態 static private int ResultState; public const int WAIT = 0; //非同期処理完了待ち public const int READY = 1; //タイトル画面遷移準備完了 public const int RELOAD = 2; //再読み込み public const int SIGN_UP = 3; //新規登録が終わったのでログイン認証 public const int SEARCH_CONTINUE = 4; //検索終了(存在していたので再発行) public const int SEARCH_FIN = 5; //検索終了(存在していないので新規登録) //Get群 public static string GetID() { return ID; } public static string GetPASS() { return PASS; } public static string GetLEVEL() { return LEVEL; } public static int GetResultState() { return ResultState; } public static string GetTEMPID() { return TEMPID; } public static string GetTEMPPASS() { return TEMPPASS; } public static string GetLOGINTIME() { return LOGINTIME; } //Set群 public static void SetID(string id) { ID = id; } public static void SetPASS(string pass) { PASS = pass; } public static void SetLEVEL(string level) { LEVEL = level; } public static void SetResultState(int resultstate) { ResultState = resultstate; } public static void SetTEMPID(string tempid) { TEMPID = tempid; } public static void SetTEMPPASS(string temppass) { TEMPPASS = temppass; } public static void SetLOGINTIME(string logintime) { LOGINTIME = logintime; } } |
■次に、通信用のファイルを用意します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; public class Catch : MonoBehaviour { private string[] ServerAddress = new string[3]; private int m_URLIndex; public const int LOGIN = 0; public const int REGISTER = 1; public const int SEARCH = 2; //初期化処理 private void Awake() { //URLの添え字 m_URLIndex = 0; //URL群 ServerAddress[0] = "login.php"; ServerAddress[1] = "register.php"; ServerAddress[2] = "search.php"; } //DB処理はこの関数から行う public void DBprocessingStart(int index) { m_URLIndex = index; StartCoroutine("Access"); //Accessコルーチンの開始 } private IEnumerator Access() { Dictionary<string, string> dic = new Dictionary<string, string>(); //PHPファイルへ送信する変数(送信先に応じて変化させる) switch (m_URLIndex) { case LOGIN: //ログイン認証 { dic.Add("ID", UserManager.GetTEMPID()); dic.Add("PASS", UserManager.GetTEMPPASS()); break; } case REGISTER: //新規登録 { dic.Add("ID", UserManager.GetTEMPID()); dic.Add("PASS", UserManager.GetTEMPPASS()); dic.Add("LOGINTIME", System.DateTime.Now.ToString("yyyyMMdd")); break; } case SEARCH: //検索 { dic.Add("ID", UserManager.GetTEMPID()); break; } } //指定したURLにデータを送信 StartCoroutine(Post(ServerAddress[m_URLIndex], dic)); // POST yield return 0; } private IEnumerator Post(string url, Dictionary<string, string> post) { WWWForm form = new WWWForm(); foreach (KeyValuePair<string, string> post_arg in post) { form.AddField(post_arg.Key, post_arg.Value); } UnityWebRequest webRequest = UnityWebRequest.Post(url, form); yield return webRequest.SendWebRequest(); if (webRequest.error != null) { //そもそも接続ができていないとき //通信状況の良い場所で再度立ち上げて下さい。 } else if (webRequest.isDone) { //非同期処理完了 //URLによって分岐 switch (m_URLIndex) { case LOGIN: //ログイン { //認証が成功しているかどうか if (webRequest.downloadHandler.text != "") { //認証成功 string[] userData = webRequest.downloadHandler.text.Split(','); //読み取ったデータをユーザー管理へ登録 UserManager.SetID(UserManager.GetTEMPID()); UserManager.SetPASS(UserManager.GetTEMPPASS()); UserManager.SetLEVEL(userData[0]); UserManager.SetLOGINTIME(userData[1]); //ログイン処理完了 UserManager.SetResultState(UserManager.READY); } else { //認証失敗 //復元やら初期化やらをする } break; } case REGISTER: //登録 { //登録が成功しているかどうか if (webRequest.downloadHandler.text == "") { //登録成功 //登録が完了したのでログイン認証へ UserManager.SetResultState(UserManager.SIGN_UP); } else { //何らかのエラーなので、再登録を試みる UserManager.SetResultState(UserManager.RELOAD); } break; } case SEARCH: //検索 { //ID検索結果 //既に登録済みかどうか if (webRequest.downloadHandler.text != "") { //既に登録済みなので、再発行 UserManager.SetResultState(UserManager.SEARCH_CONTINUE); } else { //登録されていないIDなので、新規登録へ UserManager.SetResultState(UserManager.SEARCH_FIN); } break; } } //------------------------- } } private IEnumerator CheckTimeOut(UnityWebRequest webRequest, float timeout) { float requestTime = Time.time; while (!webRequest.isDone) { if (Time.time - requestTime < timeout) yield return null; else { //タイムアウト処理 break; } } yield return null; } } |
■最後に、起動時にログインや新規登録をするファイル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class StartUp : MonoBehaviour { Catch m_Ca; int ID; int PASS; // Start is called before the first frame update void Start() { //ローカルからIDとPASSを取得 ID = PlayerPrefs.GetInt("ID", 0); PASS = PlayerPrefs.GetInt("PASS", 0); //取得できれば2回目の起動 if (ID != 0 || PASS != 0) { //仮登録 UserManager.SetTEMPID(ID.ToString()); UserManager.SetTEMPPASS(PASS.ToString()); //ログイン認証 m_Ca.DBprocessingStart(Catch.LOGIN); //非同期処理完了待ち UserManager.SetResultState(UserManager.WAIT); } else { //初回起動 //IDとPASSをランダムに生成 ID = Random.Range(10000000, 99999999); PASS = Random.Range(10000000, 99999999); //ランダム生成したIDがあるかを検索 UserManager.SetTEMPID(ID.ToString()); UserManager.SetTEMPPASS(PASS.ToString()); //検索 m_Ca.DBprocessingStart(Catch.SEARCH); //非同期処理完了待ち UserManager.SetResultState(UserManager.WAIT); } } // Update is called once per frame void Update() { switch (UserManager.GetResultState()) { case UserManager.WAIT: //非同期処理完了待ち { //Now Loading表示 break; } case UserManager.READY: //タイトル表示 { break; } case UserManager.RELOAD: //再読み込み { //初期化 //ローカルに0を保存してscene再読み込み PlayerPrefs.SetInt("ID", 0); PlayerPrefs.SetInt("PASS", 0); PlayerPrefs.Save(); //再読み込み SceneManager.LoadScene(SceneManager.GetActiveScene().name); break; } case UserManager.SIGN_UP: //新規登録にログイン処理 { //ログイン認証を行う m_Ca.DBprocessingStart(Catch.LOGIN); //非同期処理待機待ちへ UserManager.SetResultState(UserManager.WAIT); break; } case UserManager.SEARCH_FIN: //検索終了(存在していないので新規登録) { //新規登録 m_Ca.DBprocessingStart(Catch.REGISTER); //ID・PASSをローカルに保存 PlayerPrefs.SetInt("ID", int.Parse(UserManager.GetTEMPID())); PlayerPrefs.SetInt("PASS", int.Parse(UserManager.GetTEMPPASS())); PlayerPrefs.Save(); //非同期処理待機待ちへ UserManager.SetResultState(UserManager.WAIT); break; } case UserManager.SEARCH_CONTINUE: //検索終了(存在していたので再発行) { //IDをランダムに再生成 ID = Random.Range(10000000, 99999999); //再検索 UserManager.SetTEMPID(ID.ToString()); m_Ca.DBprocessingStart(Catch.SEARCH); //非同期処理待機待ちへ UserManager.SetResultState(UserManager.WAIT); break; } } } } |
一応これで起動時に認証または新規登録が行えるようになります。
間違いや不明点があればコメントで指摘して頂ければ、
出来る限りお応えしようと思います。