Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Web.Script.Serialization;
  5. using Newtonsoft.Json;
  6.  
  7. namespace JavaScriptSerialization
  8. {
  9.     public class LoginDB : MonoBehaviour {
  10.  
  11.     public static string user = "";
  12.     private string password = "", id = "";
  13.     public InputField loginInput;
  14.     public InputField passInput;
  15.     public Text text;
  16.     public int _id;
  17.  
  18.     public void Update()
  19.     {
  20.         Users us = new Users();
  21.         _id = us.id;
  22.  
  23.     }
  24.     public void toReg()
  25.     {
  26.         Application.LoadLevel("Regist");
  27.     }
  28.  
  29.     [System.Serializable]
  30.  
  31.     public class Users
  32.     {
  33.         public int id { get; set; }
  34.         public string name { get; set; }
  35.         public string klass { get; set; }
  36.         public int gold { get; set; }
  37.         public int exp { get; set; }
  38.         public int level { get; set; }
  39.  
  40.             public override string ToString()
  41.             {
  42.                 return string.Format("id: {0} \name: {1}", id, name);
  43.             }
  44.         }
  45.  
  46.     public void Login()
  47.     {
  48.         //message = "";
  49.         user = loginInput.text.ToString ();
  50.         password = passInput.text.ToString ();
  51.  
  52.         if (user == "" || password == "")
  53.             text.text = "Please enter all the fields \n";
  54.         else
  55.         {
  56.             WWWForm form = new WWWForm();
  57.             form.AddField("email", user);
  58.             form.AddField("password", password);
  59.             WWW www = new WWW("http://uo.cx.ua/api/auth/login", form);
  60.             StartCoroutine(login(www));
  61.         }
  62.         }
  63.     IEnumerator login(WWW www)
  64.     {
  65.         yield return www;
  66.         if (www.error != null)
  67.         {
  68.             Debug.Log ("error " + www.error);
  69.             }
  70.         else
  71.         {
  72.             string json = www.text; // <---{"users":{"id":1,"name":"","klass":"","gold":0,"exp":0,"level":1}}
  73.             JavaScriptSerializer ser = new JavaScriptSerializer();
  74.             Users us = ser.Deserialize<Users>(json);
  75.             Users us2 = JsonConvert.DeserializeObject<Users>(json);
  76.  
  77.                 Debug.Log (us.id);
  78.                 Debug.Log(us2.id);
  79.                 Debug.Log (json);
  80.         }
  81.     }
  82.  
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement