Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SocialPlatforms;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net;
- using System;
- using com.shephertz.app42.paas.sdk.csharp;
- using com.shephertz.app42.paas.sdk.csharp.user;
- using AssemblyCSharp;
- using UnityEngine.UI;
- using System.IO;
- public class UserRegister : MonoBehaviour {
- Constant cons = new Constant();
- //public Constant cons;
- ServiceAPI sp = null;
- UserService userService = null; // Initializing User Service.
- User createUserObject = null;
- public string password;
- public int max = 2;
- public int offSet = 1;
- public string success, box;
- public bool isLoggedIn;
- // UI
- public RectTransform registerPanel;
- public RectTransform loginPanel;
- public InputField registerName;
- public InputField registerEmail;
- public InputField registerPassword;
- public InputField loginName;
- public InputField loginPassword;
- // Player Info
- private string playerDataProjectFile = "playerData.json";
- void Awake ()
- {
- App42API.Initialize (cons.apiKey, cons.secretKey);
- LoadPlayerInfo ();
- if (isLoggedIn)
- {
- registerPanel.gameObject.SetActive (false);
- loginPanel.gameObject.SetActive (false);
- }
- }
- void Start ()
- {
- userService = App42API.BuildUserService ();
- }
- void Update ()
- {
- if (isLoggedIn == false)
- {
- cons.userName = registerName.text;
- cons.emailId = registerEmail.text;
- cons.password = registerPassword.text;
- loginName.text = cons.userName;
- loginPassword.text = cons.password;
- }
- else
- {
- AutoLogin ();
- }
- }
- public void Register ()
- {
- userService.CreateUser (cons.userName, cons.password, cons.emailId, new UserResponse ());
- }
- public void Login ()
- {
- userService.Authenticate (cons.userName, cons.password, new UserResponse ());
- }
- public void AutoLogin ()
- {
- userService.Authenticate (cons.userName, cons.password, new UserResponse ());
- }
- public void SavePlayerInfo ()
- {
- string dataAsJson = JsonUtility.ToJson (cons);
- string filePath = Application.dataPath + playerDataProjectFile;
- File.WriteAllText (filePath, dataAsJson);
- }
- public void LoadPlayerInfo ()
- {
- string filePath = Path.Combine (Application.streamingAssetsPath, playerDataProjectFile);
- if (File.Exists (filePath))
- {
- string dataAsJson = File.ReadAllText (filePath);
- cons = JsonUtility.FromJson<Constant> (dataAsJson);
- Debug.Log ("1");
- Debug.Log (cons.userName);
- }
- else
- {
- cons = new Constant ();
- Debug.Log ("2");
- Debug.Log (cons.userName);
- }
- }
- }
- public class UserResponse : App42CallBack
- {
- private string result = "";
- UserRegister ur = GameObject.Find("Authentication").GetComponent<UserRegister>();
- DataController dc = GameObject.Find("DataController").GetComponent<DataController>();
- public void OnSuccess (object user)
- {
- try
- {
- if(user is User)
- {
- User userObj = (User)user;
- result = userObj.ToString();
- Debug.Log ("UserName : " + userObj.GetUserName());
- Debug.Log ("EmailID : " + userObj.GetEmail());
- ur.isLoggedIn = true;
- ur.SavePlayerInfo();
- dc.enabled = true;
- }
- else
- {
- IList<User> userList = (IList<User>)user;
- result = userList[0].ToString();
- Debug.Log ("UserName : " + userList[0].GetUserName());
- Debug.Log ("EmailId : " + userList[0].GetEmail());
- }
- }
- catch (App42Exception e)
- {
- result = e.ToString();
- Debug.Log ("App42Exception : "+ e);
- }
- }
- public void OnException (Exception e)
- {
- result = e.ToString();
- Debug.Log ("Exception : " + e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement