Advertisement
NomadicWarrior

User Register Script

Apr 12th, 2018 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SocialPlatforms;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System;
  7. using com.shephertz.app42.paas.sdk.csharp;
  8. using com.shephertz.app42.paas.sdk.csharp.user;
  9. using AssemblyCSharp;
  10. using UnityEngine.UI;
  11. using System.IO;
  12.  
  13. public class UserRegister : MonoBehaviour {
  14.  
  15. Constant cons = new Constant();
  16. //public Constant cons;
  17. ServiceAPI sp = null;
  18. UserService userService = null; // Initializing User Service.
  19. User createUserObject = null;
  20. public string password;
  21. public int max = 2;
  22. public int offSet = 1;
  23. public string success, box;
  24. public bool isLoggedIn;
  25.  
  26. // UI
  27. public RectTransform registerPanel;
  28. public RectTransform loginPanel;
  29. public InputField registerName;
  30. public InputField registerEmail;
  31. public InputField registerPassword;
  32. public InputField loginName;
  33. public InputField loginPassword;
  34.  
  35. // Player Info
  36. private string playerDataProjectFile = "playerData.json";
  37.  
  38. void Awake ()
  39. {
  40. App42API.Initialize (cons.apiKey, cons.secretKey);
  41. LoadPlayerInfo ();
  42. if (isLoggedIn)
  43. {
  44. registerPanel.gameObject.SetActive (false);
  45. loginPanel.gameObject.SetActive (false);
  46. }
  47. }
  48.  
  49. void Start ()
  50. {
  51. userService = App42API.BuildUserService ();
  52. }
  53.  
  54.  
  55. void Update ()
  56. {
  57. if (isLoggedIn == false)
  58. {
  59. cons.userName = registerName.text;
  60. cons.emailId = registerEmail.text;
  61. cons.password = registerPassword.text;
  62. loginName.text = cons.userName;
  63. loginPassword.text = cons.password;
  64. }
  65. else
  66. {
  67. AutoLogin ();
  68. }
  69.  
  70.  
  71. }
  72.  
  73. public void Register ()
  74. {
  75. userService.CreateUser (cons.userName, cons.password, cons.emailId, new UserResponse ());
  76. }
  77.  
  78. public void Login ()
  79. {
  80. userService.Authenticate (cons.userName, cons.password, new UserResponse ());
  81. }
  82.  
  83. public void AutoLogin ()
  84. {
  85. userService.Authenticate (cons.userName, cons.password, new UserResponse ());
  86. }
  87.  
  88.  
  89. public void SavePlayerInfo ()
  90. {
  91. string dataAsJson = JsonUtility.ToJson (cons);
  92. string filePath = Application.dataPath + playerDataProjectFile;
  93. File.WriteAllText (filePath, dataAsJson);
  94. }
  95.  
  96. public void LoadPlayerInfo ()
  97. {
  98. string filePath = Path.Combine (Application.streamingAssetsPath, playerDataProjectFile);
  99. if (File.Exists (filePath))
  100. {
  101. string dataAsJson = File.ReadAllText (filePath);
  102. cons = JsonUtility.FromJson<Constant> (dataAsJson);
  103. Debug.Log ("1");
  104. Debug.Log (cons.userName);
  105. }
  106. else
  107. {
  108. cons = new Constant ();
  109. Debug.Log ("2");
  110. Debug.Log (cons.userName);
  111. }
  112. }
  113. }
  114.  
  115. public class UserResponse : App42CallBack
  116. {
  117. private string result = "";
  118. UserRegister ur = GameObject.Find("Authentication").GetComponent<UserRegister>();
  119. DataController dc = GameObject.Find("DataController").GetComponent<DataController>();
  120.  
  121. public void OnSuccess (object user)
  122. {
  123. try
  124. {
  125. if(user is User)
  126. {
  127. User userObj = (User)user;
  128. result = userObj.ToString();
  129. Debug.Log ("UserName : " + userObj.GetUserName());
  130. Debug.Log ("EmailID : " + userObj.GetEmail());
  131. ur.isLoggedIn = true;
  132. ur.SavePlayerInfo();
  133. dc.enabled = true;
  134. }
  135. else
  136. {
  137. IList<User> userList = (IList<User>)user;
  138. result = userList[0].ToString();
  139. Debug.Log ("UserName : " + userList[0].GetUserName());
  140. Debug.Log ("EmailId : " + userList[0].GetEmail());
  141.  
  142. }
  143. }
  144. catch (App42Exception e)
  145. {
  146. result = e.ToString();
  147. Debug.Log ("App42Exception : "+ e);
  148. }
  149. }
  150.  
  151. public void OnException (Exception e)
  152. {
  153. result = e.ToString();
  154. Debug.Log ("Exception : " + e);
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement