Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using System;
- using System.IO;
- using UnityEngine.EventSystems;
- using System.Runtime.Serialization.Formatters.Binary;
- public class loginScript : MonoBehaviour {
- public InputField inputName;// login tab for username
- public InputField pw; // login tab for password
- public InputField createName; // create account tab username
- public InputField createPw; // create account tab password
- public GameObject wrongUserOrPass;
- public GameObject noUsername;
- public GameObject noPassword;
- public GameObject pressSubmit;
- //hide submit part
- public GameObject textCreateAcc;
- public GameObject textUsername;
- public GameObject textPassword;
- public GameObject inputFieldUsername;
- public GameObject inputFieldPassword;
- public GameObject submitButton;
- public GameObject submitSuccessful;
- public GameObject hasNoAccount;
- public bool submitTypedUsername = false;
- public bool submitTypedPassowrd = false;
- public bool loginTypedUsername = false;
- public bool loginTypedPassword = false;
- public bool hasAccount = false;
- bool usernameIsActive = false;
- bool passwordIsActive = false;
- bool swithed = false;
- public void Start()
- {
- // Screen.SetResolution(1280, 720, false);
- wrongUserOrPass.SetActive(false);
- noUsername.SetActive(false);
- noPassword.SetActive(false);
- pressSubmit.SetActive(false);
- submitSuccessful.SetActive(false);
- EventSystem.current.SetSelectedGameObject(inputName.gameObject, null);
- inputName.OnPointerClick(new PointerEventData(EventSystem.current));
- }
- public void SubmitButton()
- {
- string createUsername = createName.text.ToString();
- string createPassword = createPw.text.ToString();
- if(System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save") == false)
- {
- System.IO.Directory.CreateDirectory(Application.dataPath + "/unity saveFiles");
- //put info in file
- if (string.IsNullOrEmpty(createPassword)== false)
- {
- submitTypedPassowrd = true;
- }
- else
- {
- submitTypedPassowrd = false;
- }
- if (string.IsNullOrEmpty(createUsername))
- {
- submitTypedUsername = false;
- }
- else
- {
- submitTypedUsername = true;
- }
- if (submitTypedUsername == true && submitTypedPassowrd == true)
- {
- System.IO.File.WriteAllText(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save", createUsername);
- System.IO.File.WriteAllText(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save", createPassword);
- submitSuccessful.SetActive(true);
- }
- }
- }
- public void LoginButton()
- {
- string userName = inputName.text.ToString();
- string password = pw.text.ToString();
- //read info from file
- string usernameReadFromFile = File.ReadAllText(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save");
- string PasswordReadFromFile = File.ReadAllText(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save");
- if(hasAccount == false)
- {
- hasNoAccount.SetActive(true);
- }
- else
- {
- hasNoAccount.SetActive(false);
- }
- if (string.IsNullOrEmpty(userName))
- {
- Debug.Log("ERROR | No content in username tab");
- wrongUserOrPass.SetActive(false);
- noUsername.SetActive(true);
- noPassword.SetActive(false);
- pressSubmit.SetActive(false);
- submitSuccessful.SetActive(false);
- loginTypedUsername = false;
- inputName.text = "";
- pw.text = "";
- }
- else
- {
- loginTypedUsername = true;
- }
- if (string.IsNullOrEmpty(password))
- {
- Debug.Log("ERROR | No content in password tab");
- wrongUserOrPass.SetActive(false);
- noUsername.SetActive(false);
- noPassword.SetActive(true);
- pressSubmit.SetActive(false);
- submitSuccessful.SetActive(false);
- inputName.text = "";
- pw.text = "";
- loginTypedPassword = false;
- }
- else
- {
- loginTypedPassword = true;
- }
- if (string.Equals(userName, usernameReadFromFile) && string.Equals(password, PasswordReadFromFile))
- {
- SceneManager.LoadScene("mainMenu");
- wrongUserOrPass.SetActive(false);
- noUsername.SetActive(false);
- noPassword.SetActive(false);
- pressSubmit.SetActive(false);
- hasNoAccount.SetActive(false);
- Debug.Log("logged in");
- }
- else
- {
- Debug.Log("ERROR | Wrong username or password");
- wrongUserOrPass.SetActive(true);
- noUsername.SetActive(false);
- noPassword.SetActive(false);
- submitSuccessful.SetActive(false);
- hasNoAccount.SetActive(false);
- inputName.text = "";
- pw.text = "";
- }
- if (System.IO.File.Exists("/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists("/unity saveFiles/gameloginDATAPass.save"))
- {
- }
- else
- {
- pressSubmit.SetActive(true);
- wrongUserOrPass.SetActive(false);
- noUsername.SetActive(false);
- noPassword.SetActive(false);
- submitSuccessful.SetActive(false);
- Debug.Log("ERROR | Username file not found!");
- }
- }
- public void QuitButton()
- {
- Application.Quit();
- }
- public void RemoveFile()
- {
- File.Delete("/unity saveFiles/gameloginDATAUser.save");
- File.Delete("/unity saveFiles/gameloginDATAPass.save");
- }
- public void Update()
- {
- string localUsername = inputName.ToString();
- if (string.IsNullOrEmpty(localUsername))
- {
- usernameIsActive = false;
- swithed = true;
- passwordIsActive = true;
- }
- else
- {
- usernameIsActive = true;
- }
- if (Input.GetKeyDown(KeyCode.Tab) && usernameIsActive == true)
- {
- EventSystem.current.SetSelectedGameObject(pw.gameObject, null);
- pw.OnPointerClick(new PointerEventData(EventSystem.current));
- }
- else if (swithed = true && passwordIsActive == true)
- {
- EventSystem.current.SetSelectedGameObject(inputName.gameObject, null);
- inputName.OnPointerClick(new PointerEventData(EventSystem.current));
- }
- if (Input.GetButtonDown("enter"))
- {
- if (System.IO.File.Exists("/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists("/unity saveFiles/gameloginDATAPass.save"))
- {
- LoginButton();
- }
- else
- {
- SubmitButton();
- }
- }
- if (System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAUser.save") && System.IO.File.Exists(Application.dataPath + "/unity saveFiles/gameloginDATAPass.save"))
- {
- textCreateAcc.SetActive(false);
- textUsername.SetActive(false);
- textPassword.SetActive(false);
- inputFieldUsername.SetActive(false);
- inputFieldPassword.SetActive(false);
- submitButton.SetActive(false);
- }
- else
- {
- textCreateAcc.SetActive(true);
- textUsername.SetActive(true);
- textPassword.SetActive(true);
- inputFieldUsername.SetActive(true);
- inputFieldPassword.SetActive(true);
- submitButton.SetActive(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement