• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LechongManok · Sep 25, 2019 at 01:49 AM · displayusername

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;
         }
     }

 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying the Y-value of the transform for the main camera 1 Answer

Display Problem- some assets not showing up in Scene mode 1 Answer

Display Object Positions in Gui Box 2 Answers

ngui only displays pink boxes (embedded graphics problem?) 0 Answers

How to display a bezier curve between transforms in-game rather than in editor? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges