Mohamed Shaban
Mohamed Shaban

Reputation: 43

How can i make remember me check box with unity?

I made an app that has a login form which gets the data from a SQL database on a server , i want to add a remember me check box so the user can automatically login , how can i do this or how can i save a data to text file then retrieve it back ? thanks

Upvotes: 0

Views: 1899

Answers (2)

zhekazheka
zhekazheka

Reputation: 111

unity, to addition to writing to a file, also provides local storing in PlayerPrefs, which work for all platforms. Please read more about it here

https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

You can store login information there and check it afterward to see if player has login credentials.

Upvotes: 1

Universus
Universus

Reputation: 406

This script will create txt file in your project folder and write to it single row with login string. In order to use this you have to put this scrip anywhere you like then drag in editor your INPUT FIELD and toggle. Finaly assign OnClick event on your login button to execute loginClick() method.

using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class InputFieldTest : MonoBehaviour {

//reference for InputField, needs to be assigned in editor
public InputField login;
string loginText;
//reference for Toggle, needs to be assigned in editor
public Toggle rememberToggle;
bool remeberIsOn;

// Use this for initialization
void Start ()
{
    loginText = "";
    FindLogin();
}
public void FindLogin()
{
    if (File.Exists("userSetting.txt"))
    {
        using (TextReader reader = File.OpenText("userSetting.txt"))
        {
            string str;
            if ((str = reader.ReadLine()) != null) login.text = str;
        }
        //Debug.Log(loginText);
    }
}
public void remember()
{
    using (TextWriter writer = File.CreateText("userSetting.txt"))
    {
        writer.WriteLine(login.text);
    }
}
public void loginClick()
{
    remeberIsOn = rememberToggle.isOn;
    loginText = login.text;
    if (remeberIsOn)
    {
        remember();
    }
    else
    {
        using (TextWriter writer = File.CreateText("userSetting.txt"))
        {
            writer.WriteLine("");
        }
    }
}

}

Upvotes: 0

Related Questions