Hello!
I wrote a php (with MySQL) script is a user’s connection to the game, and then be displayed in GUI.Label user level.
It works like this: The user enters the username and password are sent to the php script, if the login and password are correct, the output echo “Correct”;, which sees the script in Unity, then logs on.
But the level is out of line with the entered username, so everything should be in one script, and I can`t use the echo, since Unity reads all lines.
How can I pass a variable from php to Unity without echo?
well, you need to parse your data. The most popular ways are by using XML or json. Or you could write your own parser with your own format
That is, to give up PHP? And for PHP?
No, for example you output an XML (string), using echo, in your PHP script and than you can parse the xml in c#
EDIT:
example from my code (I cant give it all to you but this should give you the basic idea of it)
private void ReadData(string input)
{
if(_showDebugMessages) Debug.Log("read data ...");
XmlReader reader = XmlReader.Create(new System.IO.StringReader(input));
string tempKey = string.Empty;
WebSaveContainer tempCon = new WebSaveContainer();
while(reader.Read())
{
switch(reader.NodeType)
{
case XmlNodeType.Element:
if(reader.Name == "WebSave") break;
tempKey = reader.Name;
while(reader.MoveToNextAttribute())
{
if(reader.Name == "name")
{
tempKey = reader.Value;
}else if (reader.Name == "type")
{
if(tempKey != string.Empty) tempCon.type = GetTypeFromString(reader.Value);
}
}
break;
case XmlNodeType.Text:
if(tempKey != string.Empty)
{
if(tempCon.type == null) tempCon.type = typeof(string);
tempCon.value = reader.Value;
_cache.Add(tempKey, tempCon);
tempKey = string.Empty;
tempCon = new WebSaveContainer();
}
break;
}
}
reader.Close();
}
and the XML looks like this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<WebSave>
<username>element</username>
<sid>00a062c9c14578cc4dc98b4cad94f68ad3e388af</sid>
<data name="some string" type="string">unity</data>
<data name="some string array" type="string[]">unity, test, germany</data>
<data name="some int" type="int">12</data>
<data name="some int array" type="int[]">465,31,687,24,647,32</data>
<data name="some float" type="float">185.24445</data>
<data name="some float array" type="float[]">1.5464,7544.25,54.546857</data>
<data name="some double" type="double">185.24445</data>
<data name="some double array" type="double[]">1.5464,7544.25,3.54676783788</data>
<data name="some Vector2" type="Vector2">(1.5,5.7)</data>
<data name="some Vector2 array" type="Vector2[]">(1.5,5.7),(1.5,5.7),(1.5,5.7)</data>
<data name="some Vector3" type="Vector3">(1.5,5.7,64)</data>
<data name="some Vector3 array" type="Vector3[]">(1.5,5.7,4.56),(1.5,5.7,5674.14),(1.5,5.7,655.4)</data>
<data name="some Vector4" type="Vector4">(1.5,5.7,54.5,65.1)</data>
<data name="some Vector4 array" type="Vector4[]">(1.5,5.7,54.5,65.1),(1.5,5.7,54.5,65.1)</data>
<data name="some byte array" type="byte[]">132</data>
</WebSave>
I apologize for the arrogance, but you can please explain how to use php and xml similar to the script
C#
IEnumerator LevelOk(string username,int level)
{
WWWForm form = new WWWForm();
form.AddField("myusername", PhotonNetwork.playerName);
form.AddField("level", level);
WWW levela = new WWW(url, form);
yield return levela;
string lev = levela.text;
levl = ""+lev;
}
PHP
$query = "SELECT level,HP,MP FROM $tbl_name WHERE username='$myusername'";
$res = mysql_query($query) or die ('Что-то не так с Вашим запросом! SQL:
' . $query . '
Ошибка:
' . mysql_error());
$row = mysql_fetch_array($res);
$level = $row['level'];
echo $level;
$query = "SELECT level,HP,MP FROM $tbl_name WHERE username='$myusername'";
$res = mysql_query($query) or die ('Что-то не так с Вашим запросом! SQL:
' . $query . '
Ошибка:
' . mysql_error());
$row = mysql_fetch_array($res);
echo '
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XML>
<data name="level" > '. $row['level'] .' </data>
<data name="HP" > '. $row['HP'] .' </data>
<data name="MP" > '. $row['MP'] .' </data>
<XML>
';
thats is a simple way of building a xml file
Can not display each line as a variable, it turns out only to show the code of XML, or nothing at all displayed, tried in C # code to post the same, just adding a few lines to the output, tried as an XML file.
Ofc you cant. You need an XML parser on unity side to translate from XML file (what you output from PHP) to your variables. You also need to read about XML to understand what it is and how it facilitates the data exchange between programs.
But if written in the XML data file, and then have to get the data from the XML can be replaced by a new user.
P.S. I’m sorry if you do not understand something or are not clearly written.