Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.20 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. using System;
  6. using System.IO;
  7. using UnityEngine.EventSystems;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. public class loginScript : MonoBehaviour {
  10.     public  InputField inputName;// login tab for username
  11.     public InputField pw; // login tab for password
  12.     public InputField createName; // create account tab username
  13.     public InputField createPw; // create account tab password
  14.     public GameObject wrongUserOrPass;
  15.     public GameObject noUsername;
  16.     public GameObject noPassword;
  17.     public GameObject pressSubmit;
  18.     //hide submit part
  19.     public GameObject textCreateAcc;
  20.     public GameObject textUsername;
  21.     public GameObject textPassword;
  22.     public GameObject inputFieldUsername;
  23.     public GameObject inputFieldPassword;
  24.     public GameObject submitButton;
  25.     public GameObject submitSuccessful;
  26.     public GameObject hasNoAccount;
  27.     public bool submitTypedUsername = false;
  28.     public bool submitTypedPassowrd = false;
  29.     public bool loginTypedUsername = false;
  30.     public bool loginTypedPassword = false;
  31.     public bool hasAccount = false;
  32.     bool usernameIsActive = false;
  33.     bool passwordIsActive = false;
  34.     bool swithed = false;
  35.     public void Start()
  36.     {
  37.  
  38. //      Screen.SetResolution(1280, 720, false);
  39.         wrongUserOrPass.SetActive(false);
  40.         noUsername.SetActive(false);
  41.         noPassword.SetActive(false);
  42.         pressSubmit.SetActive(false);
  43.         submitSuccessful.SetActive(false);
  44.         EventSystem.current.SetSelectedGameObject(inputName.gameObject, null);
  45.         inputName.OnPointerClick(new PointerEventData(EventSystem.current));
  46.  
  47.     }
  48.     public void SubmitButton()
  49.     {
  50.         string createUsername = createName.text.ToString();
  51.         string createPassword = createPw.text.ToString();
  52.  
  53.             if(System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save") == false)
  54.          {
  55.             System.IO.Directory.CreateDirectory(Application.dataPath + "/unity saveFiles");
  56.            
  57.             //put info in file
  58.             if (string.IsNullOrEmpty(createPassword)== false)
  59.             {
  60.                 submitTypedPassowrd = true;
  61.             }
  62.             else
  63.             {
  64.                 submitTypedPassowrd = false;
  65.             }
  66.             if (string.IsNullOrEmpty(createUsername))
  67.             {
  68.                 submitTypedUsername = false;
  69.             }
  70.             else
  71.             {
  72.                 submitTypedUsername = true;
  73.  
  74.             }
  75.             if (submitTypedUsername == true && submitTypedPassowrd == true)
  76.             {        
  77.                     System.IO.File.WriteAllText(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save", createUsername);
  78.                     System.IO.File.WriteAllText(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save", createPassword);
  79.                     submitSuccessful.SetActive(true);
  80.             }
  81.            
  82.  
  83.         }
  84.            
  85.     }
  86.  
  87.     public void LoginButton()
  88.     {
  89.         string userName = inputName.text.ToString();
  90.         string password = pw.text.ToString();
  91.         //read info from file
  92.         string usernameReadFromFile = File.ReadAllText(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save");
  93.         string PasswordReadFromFile = File.ReadAllText(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save");
  94.         if(hasAccount == false)
  95.         {
  96.             hasNoAccount.SetActive(true);
  97.         }
  98.         else
  99.         {
  100.             hasNoAccount.SetActive(false);
  101.         }
  102.         if (string.IsNullOrEmpty(userName))
  103.         {
  104.             Debug.Log("ERROR | No content in username tab");
  105.             wrongUserOrPass.SetActive(false);
  106.             noUsername.SetActive(true);
  107.             noPassword.SetActive(false);
  108.             pressSubmit.SetActive(false);
  109.             submitSuccessful.SetActive(false);
  110.             loginTypedUsername = false;
  111.             inputName.text = "";
  112.             pw.text = "";
  113.  
  114.  
  115.  
  116.         }
  117.         else
  118.         {
  119.             loginTypedUsername = true;
  120.         }
  121.         if (string.IsNullOrEmpty(password))
  122.         {
  123.             Debug.Log("ERROR | No content in password tab");
  124.             wrongUserOrPass.SetActive(false);
  125.             noUsername.SetActive(false);
  126.             noPassword.SetActive(true);
  127.             pressSubmit.SetActive(false);
  128.             submitSuccessful.SetActive(false);
  129.             inputName.text = "";
  130.             pw.text = "";
  131.             loginTypedPassword = false;
  132.         }
  133.         else
  134.         {
  135.             loginTypedPassword = true;
  136.         }
  137.         if (string.Equals(userName, usernameReadFromFile) && string.Equals(password, PasswordReadFromFile))
  138.         {
  139.             SceneManager.LoadScene("mainMenu");
  140.             wrongUserOrPass.SetActive(false);
  141.             noUsername.SetActive(false);
  142.             noPassword.SetActive(false);
  143.             pressSubmit.SetActive(false);
  144.             hasNoAccount.SetActive(false);
  145.             Debug.Log("logged in");
  146.         }
  147.         else
  148.         {
  149.  
  150.             Debug.Log("ERROR | Wrong username or password");
  151.             wrongUserOrPass.SetActive(true);
  152.             noUsername.SetActive(false);
  153.             noPassword.SetActive(false);
  154.             submitSuccessful.SetActive(false);
  155.             hasNoAccount.SetActive(false);
  156.             inputName.text = "";
  157.             pw.text = "";
  158.         }
  159.         if (System.IO.File.Exists("/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists("/unity saveFiles/gameloginDATAPass.save"))
  160.         {
  161. }
  162.         else
  163.         {
  164.             pressSubmit.SetActive(true);
  165.             wrongUserOrPass.SetActive(false);
  166.             noUsername.SetActive(false);
  167.             noPassword.SetActive(false);
  168.             submitSuccessful.SetActive(false);
  169.             Debug.Log("ERROR | Username file not found!");
  170.         }
  171.     }
  172.     public void QuitButton()  
  173.     {
  174.         Application.Quit();
  175.     }
  176.     public void RemoveFile()
  177.     {
  178.         File.Delete("/unity saveFiles/gameloginDATAUser.save");
  179.         File.Delete("/unity saveFiles/gameloginDATAPass.save");
  180.     }
  181.     public void Update()
  182.     {
  183.         string localUsername = inputName.ToString();
  184.  
  185.         if (string.IsNullOrEmpty(localUsername))
  186.         {
  187.             usernameIsActive = false;
  188.             swithed = true;
  189.             passwordIsActive = true;
  190.         }
  191.         else
  192.         {
  193.             usernameIsActive = true;
  194.         }
  195.  
  196.         if (Input.GetKeyDown(KeyCode.Tab) && usernameIsActive == true)
  197.         {
  198.  
  199.  
  200.             EventSystem.current.SetSelectedGameObject(pw.gameObject, null);
  201.             pw.OnPointerClick(new PointerEventData(EventSystem.current));
  202.         }
  203.         else if (swithed = true && passwordIsActive == true)
  204.         {
  205.                 EventSystem.current.SetSelectedGameObject(inputName.gameObject, null);
  206.                 inputName.OnPointerClick(new PointerEventData(EventSystem.current));
  207.         }
  208.        
  209.         if (Input.GetButtonDown("enter"))
  210.         {
  211.             if (System.IO.File.Exists("/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists("/unity saveFiles/gameloginDATAPass.save"))
  212.             {
  213.                 LoginButton();
  214.             }
  215.             else
  216.             {
  217.                 SubmitButton();
  218.                
  219.             }
  220.  
  221.         }
  222.         if (System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save"))
  223.         {
  224.             textCreateAcc.SetActive(false);
  225.             textUsername.SetActive(false);
  226.             textPassword.SetActive(false);
  227.             inputFieldUsername.SetActive(false);
  228.             inputFieldPassword.SetActive(false);
  229.             submitButton.SetActive(false);
  230.         }
  231.         else
  232.         {
  233.             textCreateAcc.SetActive(true);
  234.             textUsername.SetActive(true);
  235.             textPassword.SetActive(true);
  236.             inputFieldUsername.SetActive(true);
  237.             inputFieldPassword.SetActive(true);
  238.             submitButton.SetActive(true);
  239.         }
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement