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:
-
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.)
-
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; } }
}