- Home /
Displaying Username After Login (is this possible)?
Hello again! I would like to ask again, but this time, it is about displaying the username who logged into the game. What I did is that I wrote a PHP code that will fetch the username and then used UnityWebRequest.Get() to let the C# script read the PHP file, but then - no success. What only happens is that there is no name displayed on the Welcome Screen.
Here are my codes:
1) LoginInputs
//Variables
public InputField UsernameInput;
public InputField PasswordInput;
public Button LoginButton;
public Text message;
public void FieldCheckLogin() {
/*LoginButton.onClick.AddListener(() => {
StartCoroutine(Main.Instance.Web.Login(UsernameInput.text, PasswordInput.text));
});*/
message.text = "";
StartCoroutine(Login(UsernameInput.text, PasswordInput.text));
}
public IEnumerator Login(string username, string password)
{
WWWForm form = new WWWForm();
form.AddField("loginUser", username);
form.AddField("loginPass", password);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/sqlconnect-ChemAR/login.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
message.text = www.error;
}
else
{
Debug.Log(www.downloadHandler.text);
StartCoroutine(GetDate());
message.text = www.downloadHandler.text;
}
}
}
IEnumerator GetDate()
{
using (UnityWebRequest www = UnityWebRequest.Get("http://localhost/sqlconnect-ChemAR/GetDate.php"))
{
// Request and wait for the desired page.
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
message.text = www.error;
}
else
{
//Show results as text
Debug.Log(www.downloadHandler.text);
//Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
StartCoroutine(PleaseWait());
SceneManager.LoadScene(3);
}
}
}
IEnumerator PleaseWait() {
yield return new WaitForSeconds(5);
}
public void VerifyInputsLogin() {
LoginButton.interactable = (UsernameInput.text.Length >= 8 && PasswordInput.text.Length >= 8);
}
/*Conditions if the username and password are correct or not
public void FieldCheckLogin() {
if (UsernameInput.text == "" || PasswordInput.text == "")
{
message.text = "Please don't leave any of the fields blank.";
}
}*/
//Other Login Inputs will soon to be included (I need to setup a database first.)
2) UsernameDisplay
//Variables
public Text UserDisplay;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GetUsername());
}
IEnumerator GetUsername() {
using (UnityWebRequest www = UnityWebRequest.Get("http://localhost/sqlconnect-ChemAR/displayuserlogged.php")) {
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
//Show results as text
Debug.Log(www.downloadHandler.text);
//Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
}
Answer by Navy_Seals · Sep 25, 2019 at 06:13 AM
@LechongManok Its better to write a logic like this,
once after you submit the data to your server and you get a success message.. Just add the login name to a PlayerPrefs and retrieve it and show it in the display.
byte[] results = www.downloadHandler.data;
if(www.downloadHandler.data.ToString().Contains("Success")
{
PlayerPrefs.SetString("UID", name);
}
Retrieve the data and show it anywhere like this,
loginName.text = "Hi " + PlayerPrefs.GetString("UID") + "!";
Let me know if this helps you.
Your answer
Follow this Question
Related Questions
Animation not showing when close up 1 Answer
Bug when hiding resolution Dialog? (Settings get reset on startup) 3 Answers
How to fix display 5 Answers
Loading a Model 1 Answer
Something is missing in my code can any body help me? 0 Answers