Search Unity

Display data from php to unity

Discussion in 'Multiplayer' started by Alex1McGrady, Jul 31, 2017.

  1. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    Hello guys.I want to display some data from a php file to my unity.The problem is unity doesn't display it but if i put Debug.log i can see them.

    My php code


    As you can see if i login with my localhost(like if i was on a website) i can see all my table rows,the session username and the number 100(which i put it as a test).

    Now the unity part:
    This is my code when i want to login

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class internetCalls : MonoBehaviour
    6. {
    7.     public static internetCalls _Instance;
    8.  
    9.     public bool IsLoggedIn = false;
    10.     public string LoggedInUsername = "";
    11.  
    12.     public string message = "";
    13.     public displayDataFromPhp display;
    14.     public bool IsSamurai = false;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         _Instance = this;
    20.         DontDestroyOnLoad(this.gameObject);
    21.     }
    22.     public void OnGUI()
    23.     {
    24.         if (message != "")
    25.             GUILayout.Box(message);
    26.     }
    27.     public void DoLogin(string user, string password)
    28.     {
    29.         WWWForm www = new WWWForm();
    30.         www.AddField("user", user);
    31.         www.AddField("password", password);
    32.  
    33.  
    34.  
    35.         WWW w = new WWW("http://localhost/CodeIgniterUnity/index.php/Users/login", www);
    36.         StartCoroutine(Login(w, user));
    37.         LoggedInUsername = user;
    38.         Debug.Log("Logged in: " + user);
    39.  
    40.     }
    41.  
    42.     public void DoRegisterSamurai(string user, string name, string password)
    43.     {
    44.        
    45.             WWWForm www = new WWWForm();
    46.             www.AddField("user", user);
    47.             www.AddField("name", name);
    48.             www.AddField("password", password);
    49.             WWW w = new WWW("http://localhost/CodeIgniterUnity/index.php/Users/registerSam", www);
    50.             StartCoroutine(Register(w));
    51.  
    52.     }
    53.     public void DoRegisterKnight(string user, string name, string password)
    54.     {
    55.  
    56.         WWWForm www = new WWWForm();
    57.         www.AddField("user", user);
    58.         www.AddField("name", name);
    59.         www.AddField("password", password);
    60.         WWW w = new WWW("http://localhost/CodeIgniterUnity/index.php/Users/registerKnight", www);
    61.         StartCoroutine(Register(w));
    62.  
    63.     }
    64.    
    65.  
    66.     public IEnumerator Register(WWW w)
    67.     {
    68.         yield return w;
    69.         if (w.error == null)
    70.         {
    71.             message += "Register Good!!!";
    72.         }
    73.         else
    74.         {
    75.             message += "Couldn't Connect";
    76.         }
    77.     }
    78.  
    79.  
    80.    public IEnumerator Login(WWW w, string user)
    81.     {
    82.         yield return w;
    83.         if (w.error != null)
    84.         {
    85.            
    86.  
    87.                 message += "Login Good!!!";
    88.                 IsLoggedIn = true;
    89.                 LoggedInUsername = user;
    90.                 Debug.Log("Logged in: " + user);
    91.                 UnityEngine.SceneManagement.SceneManager.LoadScene("map");
    92.        
    93.         }
    94.         else
    95.         {
    96.             message += "Couldn't Connect";
    97.         }
    98.     }
    99.  
    100. }
    and this is my code (in the next scene after the user logs in) to display the data when he logs in.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class displayDataFromPhp : MonoBehaviour
    6. {
    7.     public Text dis;
    8.     string answer;
    9.     string url = "http://localhost/CodeIgniterUnity/index.php/Users/";
    10.  
    11.  
    12.     public void Start()
    13.     {
    14.        
    15.         WWW get_www = new WWW(url);
    16.         StartCoroutine(WaitForRequest(get_www));
    17.  
    18.     }
    19.     IEnumerator WaitForRequest(WWW www)
    20.     {
    21.         yield return www;
    22.  
    23.         if (www.error == null)
    24.         {
    25.  
    26.              answer = www.text.ToString();
    27.         }
    28.         else {
    29.             Debug.Log("WWW Error: " + www.error);
    30.          
    31.         }
    32.     }
    33.  
    34.  
    35.     void OnGUI()
    36.     {
    37.         dis.text = answer;  //emfanizei apo tin vasi mou ta stoixeia
    38.         Debug.Log(answer);
    39.  
    40.  
    41.     }
    42. }
    As you can see when i log in with unity it shows ONLY the number 100(which i put to test it in my php file at the beggining)


    In the debug log i can see the number 100 AND my database table but NOT the session username.


    What i want is to somehow show instead of the green 100,the username session(which is not even in the debug log).Any ideas?


    P.S sorry for my english!
     

    Attached Files:

  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    It is in the debug. And if you access the diz text component. You also dont want to use the OnGui for the new Unity UI. Your formatting is probably to blame here. Font size etc. You prolly dont want to just spit the answer to one text field. That's gonna break on different resolutions etc.