I cannot seem any reference on how to go about spawning gameobjects from a sql/php backend using data retrieved using the WWW set. I cant seem to find any kind of reference in regards to this anywhere. Im trying to pull x,y,z from the sql using php and the spawning the prefab for the player at the position. I need to be able ot parse the data out of the returned data and then spawn the gameobject based on the data. Any thoughts/references on this would be greatly appreciated. Thank you!
You can use WWW to open any url you want, including your typical get parameters (e.g. http://myserver.com/?action=getData) www.data will contain the received data as a string. So you can write you own php code, that will return the reqired data in a format that you can easily parse. To parse the returned data you can use the standard .net string features, or you return xml and use .net's xml parsing functions.
This is how i ended up doing it, hopefully it can serve as an example for somone else =)
function Start()
{ // show the progress window showProgress=true;
var w = WWW(URL);
yield w;
planetData = w.data; //here we return the data our PHP told us
var singlecoords = planetData.Split("
"[0]); //line splitting php outut
curLine=0;
maxLines=singlecoords.Length;
for (line in singlecoords)
{
// advance progress indicator
++curLine;
var coords = line.Split(","[0]); //taking xyz & other data from singlecoords
if (line.Length >= 8) {
// makes sure we only instantiate valid planet records and
// not any blanks the PHP/web exchanger added to the end
planetID = parseInt(coords[0]);
planetName = coords[6];
planetOwner = coords[4];
planetSector = coords[5];
objectType = parseInt(coords[7]);
var pos = Vector3(parseInt(coords[1]),parseInt(coords[2]),parseInt(coords[3]));
if (ObjectFactories.Length>objectType && ObjectFactories[objectType] != null) {
var planetInstance = Instantiate(ObjectFactories[objectType], pos, Quaternion.identity);
planetInstance.name = planetName;
switch (objectType) {
case 0: /* Planet */
pInfo=planetInstance.GetComponent(PlanetClick);
pInfo.planetName=planetName;
pInfo.planetSector=planetSector;
pInfo.planetOwner=planetOwner;
pInfo.planetID=planetID;
pInfo.objectType=objectType;
break;
case 1: /* Asteroid Belt */
print(objectType);
pInfo=planetInstance.GetComponent(PlanetClick);
pInfo.planetName=planetName;
pInfo.planetSector=planetSector;
pInfo.planetOwner=planetOwner;
pInfo.planetID=planetID;
pInfo.objectType=objectType;
break;
default:
print("No initializer for object type "+objectType);
}
} else {
print("No prefab template in objectType slot "+objectType);
}
}
}
Question: Can i use this way to read fetched data from php/MYSQL Db.
code in my unity .js file.
var hs_get : WWW = new WWW (url); yield hs_get; var newdata = hs_data.data; . . .
does this mean , "newdata" has all the attributes as in the database table fetched from DB using PHP?
reply to me : sandeepsebol@gmail.com
Please note. This might fail strangely and silently. Check if there is difference between receiving hard coded and dynamic data using this php code:
this is hard coded text and will usually appear
<?php
print "This is dynamic text, should appear, but might disappear";
?>