Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class LoginRegisterUser : MonoBehaviour {
- public string urlController = "http://www.n32fps2016.dx.am/controller.php?";
- public InputField inputUserLogin, inputPassLogin;
- public Text Aviso;
- public Button Login_Button,Register_Button;
- public GameObject LoginPrefab, RegisterPrefab;
- [HideInInspector]
- public string username, email, newuser;
- bool logado,logando;
- float tempoErro;
- void Awake(){
- DontDestroyOnLoad(gameObject);
- }
- public void Login(){
- if (logando) return;
- logando = true;
- if (logado){
- logando = false;
- return;
- }
- if (inputUserLogin.text.Trim() == "" || inputPassLogin.text.Trim() == ""){
- // ta faltando digitar senha ou usuario
- StartCoroutine(FadeAviso(false, 2,"Digite todos os campos!"));
- logando = false;
- return;
- }
- if (inputUserLogin.text.Contains(" ")){
- // campo preenchido de forma incorreta
- StartCoroutine(FadeAviso(false, 2, "Campo preenchido incorretamente!"));
- logando = false;
- return;
- }
- inputPassLogin.text = inputPassLogin.text.Replace(" ", "%20");
- string url = urlController + "request=login&user=" + inputUserLogin.text + "&pass=" + inputPassLogin.text;
- WWW www = new WWW(url);
- tempoErro = 0;
- while (!www.isDone){
- tempoErro += Time.deltaTime * .01f;
- Wait(Time.deltaTime);
- if (tempoErro > 10000){
- StartCoroutine(FadeAviso(false, 2, "Sem conexão com a internet!"));
- logando = false;
- return;
- }
- }
- string result = www.text;
- if (result == "Erro"){
- //erro de conexao com banco de dados
- StartCoroutine(FadeAviso(false, 2, "Ops! Erro em nosso datacenter!"));
- logando = false;
- return;
- }
- if (result == "Inexistente"){
- // usuario e ou senha invalidos
- StartCoroutine(FadeAviso(false,2, "Usuario informado incorreto ou inexistente!"));
- logando = false;
- return;
- }
- // ususario logado com sucesso, pegando dados
- string[] dados = result.Split('/');
- username = dados[0]; // nome de usuario
- email = dados[1]; // email
- newuser = dados[2];
- logado = true;
- StartCoroutine(FadeAviso(true,2, "Logado com sucesso!"));
- UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(1);
- }
- void Register(){
- }
- void Wait(float t){
- while (t > 0){
- t -= Time.deltaTime;
- }
- }
- IEnumerator FadeAviso(bool type, float t, string text) {
- Image Painel = Aviso.GetComponentInChildren<Image>();
- if (type) Painel.color = Painel.color = new Color32(0, 255, 150, 90); else Painel.color = new Color32(255, 0, 0, 90);
- Aviso.gameObject.SetActive(true); // ativando
- Aviso.text = text; // setando texto
- Aviso.CrossFadeAlpha(0, 0, true);
- Painel.CrossFadeAlpha(0, 0, true);
- Aviso.CrossFadeAlpha(1, .25f, true); // alpha em 0 pra 1
- Painel.CrossFadeAlpha(1, .25f, true);
- yield return new WaitForSeconds(.25f);
- yield return new WaitForSeconds(t); // esperando tempo de vida
- Aviso.CrossFadeAlpha(0, .25f, true);
- Painel.CrossFadeAlpha(0, .25f, true); // alpha em 1 pra 0
- yield return new WaitForSeconds(.25f);
- Aviso.gameObject.SetActive(false); // desativando
- }
- }
Add Comment
Please, Sign In to add comment