Unity to PHP
Posted: Wed Feb 02, 2011 7:12 pm
I promised yall these scripts sometime back . I owe them to you so here they are
Want to access a PHP file on a webserver while running Unity? Here is how
in the example below I am getting the player name for Quests of Crocania..This is named getname.js and is a script in unity. I placed it in a folder called webscripts and attach it to the player
I then have a file on my localhost in the directory of /crocania/getmethods/ called webgetname.php
as you can see unity runs the script which does a http request to run the file. The PHP file runs and echos out the player name. Unity is listening for anything returned and when the name is returned we turn it into a unity variable.
Now you are asking.."Well what about multiple variables from one call...Well ole Halls has got you covered!
this is how I grab all the players skills
this is a file called webgetskills.js and is also a unity .js file
i have a php file located ocalhost/crocania/webgetskills.php that it calls
As you can see it collects everything echoed and I just add a : between each variable. Once it gets to unity I split the variables by : into an array.
Simple as that!!!
result

Want to access a PHP file on a webserver while running Unity? Here is how
in the example below I am getting the player name for Quests of Crocania..This is named getname.js and is a script in unity. I placed it in a folder called webscripts and attach it to the player
Code: Select all
function Awake () {
getname();
}
function getname()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;
var URL = "http://localhost/crocania/getmethods/webgetname.php"; //change for your URL
var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
inctext = w.text;
formText = inctext.Split(":"[0]);
////////////create variables from DB
GameObject.Find ("Playername").guiText.text=""+formText[0];
w.Dispose(); //clear our form in game
}
}
I then have a file on my localhost in the directory of /crocania/getmethods/ called webgetname.php
Code: Select all
<?php
include_once '../connect.php';
session_start();
?>
<?php
$player=$_SESSION['player'];
echo $player;
?>
Now you are asking.."Well what about multiple variables from one call...Well ole Halls has got you covered!
this is how I grab all the players skills
this is a file called webgetskills.js and is also a unity .js file
Code: Select all
function Awake () {
getskills();
}
function getskills()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;
var URL = "http://localhost/crocania/webgetskills.php"; //change for your URL
var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
inctext = w.text;
formText = inctext.Split(":"[0]);
////////////create variables from DB
var miningskill = formText[2];
var forestryskill = formText[1];
var harvestskill = formText[3];
var searchskill = formText[4];
//////put variables on GUI
guiText.text=""+forestryskill;
GameObject.Find ("MiningText").guiText.text=""+miningskill;
GameObject.Find ("Playername").guiText.text=""+formText[0];
GameObject.Find ("Harvesttext").guiText.text=""+harvestskill;
GameObject.Find ("Searchtext").guiText.text=""+searchskill;
w.Dispose(); //clear our form in game
}
}
Code: Select all
<?php
include_once 'connect.php';
session_start();
?>
<?php if (isset($_SESSION['player']))
{
$player=$_SESSION['player'];
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("Could not get user stats");
$playerinfo3=mysql_fetch_array($playerinfo2);
}
else
{
print "Sorry, not logged in please <A href='login.php'>Login</a><br>";
exit;}
echo $playerinfo3['name'];
echo ":";
echo $playerinfo3['Forestry'];
echo ":";
echo $playerinfo3['Mining'];
echo ":";
echo $playerinfo3['Harvest'];
echo ":";
echo $playerinfo3['Search'];
?>
Simple as that!!!
result
