Unityでログイン機能を実装したいです
そこで、こちらを参考に、
シーンファイルのダウンロード
ニフクラ mobile backendの初期設定
mobile backendと通信して、ログインや新規登録を行う
ログイン画面と新規登録画面を切り替える
と順にやっていったのですが
シーンに遷移ができません
因みに別のゲームで実装したかったので、新しくプロジェクトを作成し、行いました
遷移したいシーンもとりあえずStageという名で作り、Build&Settingから登録も行いました
念の為、シーンの遷移のスクリプトが古かったので、
using UnityEngine.SceneManagement;を追加して
SceneManager.LoadScene("Stage");に変更したりもしました
以下スクリプトですが
csharp:Player.cs
1 2using UnityEngine; 3using System.Collections; 4using UnityEngine.SceneManagement; 5 6public class LogInManager : MonoBehaviour 7{ 8 9 private GameObject guiTextLogIn; // ログインテキスト 10 private GameObject guiTextSignUp; // 新規登録テキスト 11 12 // ログイン画面のときtrue, 新規登録画面のときfalse 13 private bool isLogIn; 14 15 // ボタンが押されると対応する変数がtrueになる 16 private bool logInButton; 17 private bool signUpMenuButton; 18 private bool signUpButton; 19 private bool backButton; 20 21 // テキストボックスで入力される文字列を格納 22 public string id; 23 public string pw; 24 public string mail; 25 26 void Start() 27 { 28 29 FindObjectOfType<UserAuth>().logOut(); 30 31 // ゲームオブジェクトを検索し取得する 32 guiTextLogIn = GameObject.Find("GUITextLogIn"); 33 guiTextSignUp = GameObject.Find("GUITextSignUp"); 34 35 isLogIn = true; 36 guiTextSignUp.SetActive(false); 37 guiTextLogIn.SetActive(true); 38 39 } 40 41 void OnGUI() 42 { 43 44 // ログイン画面 45 if (isLogIn) 46 { 47 48 drawLogInMenu(); 49 50 // ログインボタンが押されたら 51 if (logInButton) 52 FindObjectOfType<UserAuth>().logIn(id, pw); 53 54 // 新規登録画面に移動するボタンが押されたら 55 if (signUpMenuButton) 56 isLogIn = false; 57 } 58 59 // 新規登録画面 60 else 61 { 62 63 drawSignUpMenu(); 64 65 // 新規登録ボタンが押されたら 66 if (signUpButton) 67 FindObjectOfType<UserAuth>().signUp(id, mail, pw); 68 69 // 戻るボタンが押されたら 70 if (backButton) 71 isLogIn = true; 72 } 73 74 // currentPlayerを毎フレーム監視し、ログインが完了したら 75 if (FindObjectOfType<UserAuth>().currentPlayer() != null) 76 SceneManager.LoadScene("Stage"); 77 78 } 79 80 private void drawLogInMenu() 81 { 82 // テキスト切り替え 83 guiTextSignUp.SetActive(false); 84 guiTextLogIn.SetActive(true); 85 86 // テキストボックスの設置と入力値の取得 87 GUI.skin.textField.fontSize = 20; 88 int txtW = 150, txtH = 40; 89 id = GUI.TextField(new Rect(Screen.width * 1 / 2, Screen.height * 1 / 3 - txtH * 1 / 2, txtW, txtH), id); 90 pw = GUI.PasswordField(new Rect(Screen.width * 1 / 2, Screen.height * 1 / 2 - txtH * 1 / 2, txtW, txtH), pw, '*'); 91 92 // ボタンの設置 93 int btnW = 180, btnH = 50; 94 GUI.skin.button.fontSize = 20; 95 logInButton = GUI.Button(new Rect(Screen.width * 1 / 4 - btnW * 1 / 2, Screen.height * 3 / 4 - btnH * 1 / 2, btnW, btnH), "Log In"); 96 signUpMenuButton = GUI.Button(new Rect(Screen.width * 3 / 4 - btnW * 1 / 2, Screen.height * 3 / 4 - btnH * 1 / 2, btnW, btnH), "Sign Up"); 97 98 } 99 100 private void drawSignUpMenu() 101 { 102 // テキスト切り替え 103 guiTextLogIn.SetActive(false); 104 guiTextSignUp.SetActive(true); 105 106 // テキストボックスの設置と入力値の取得 107 int txtW = 150, txtH = 35; 108 GUI.skin.textField.fontSize = 20; 109 id = GUI.TextField(new Rect(Screen.width * 1 / 2, Screen.height * 1 / 4 - txtH * 1 / 2, txtW, txtH), id); 110 pw = GUI.PasswordField(new Rect(Screen.width * 1 / 2, Screen.height * 2 / 5 - txtH * 1 / 2, txtW, txtH), pw, '*'); 111 mail = GUI.TextField(new Rect(Screen.width * 1 / 2, Screen.height * 11 / 20 - txtH * 1 / 2, txtW, txtH), mail); 112 113 // ボタンの設置 114 int btnW = 180, btnH = 50; 115 GUI.skin.button.fontSize = 20; 116 signUpButton = GUI.Button(new Rect(Screen.width * 1 / 4 - btnW * 1 / 2, Screen.height * 3 / 4 - btnH * 1 / 2, btnW, btnH), "Sign Up"); 117 backButton = GUI.Button(new Rect(Screen.width * 3 / 4 - btnW * 1 / 2, Screen.height * 3 / 4 - btnH * 1 / 2, btnW, btnH), "Back"); 118 } 119 120} 121``` 122 123``` 124UserAuth.cs 125 126using UnityEngine; 127using System.Collections; 128using NCMB; 129using System.Collections.Generic; 130 131public class UserAuth : MonoBehaviour 132{ 133 134 private string currentPlayerName; 135 136 // mobile backendに接続してログイン ------------------------ 137 138 public void logIn(string id, string pw) 139 { 140 141 NCMBUser.LogInAsync(id, pw, (NCMBException e) => { 142 // 接続成功したら 143 if (e == null) 144 { 145 currentPlayerName = id; 146 } 147 }); 148 } 149 150 // mobile backendに接続して新規会員登録 ------------------------ 151 152 public void signUp(string id, string mail, string pw) 153 { 154 155 NCMBUser user = new NCMBUser(); 156 user.UserName = id; 157 user.Email = mail; 158 user.Password = pw; 159 user.SignUpAsync((NCMBException e) => { 160 161 if (e == null) 162 { 163 currentPlayerName = id; 164 } 165 }); 166 } 167 168 // mobile backendに接続してログアウト ------------------------ 169 170 public void logOut() 171 { 172 173 NCMBUser.LogOutAsync((NCMBException e) => { 174 if (e == null) 175 { 176 currentPlayerName = null; 177 } 178 }); 179 } 180 181 // 現在のプレイヤー名を返す -------------------- 182 public string currentPlayer() 183 { 184 return currentPlayerName; 185 } 186 187} 188```
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。