Page 1 of 1

Unity to PHP

Posted: Wed Feb 02, 2011 7:12 pm
by hallsofvallhalla
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

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;

?>
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

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
   }
}
i have a php file located ocalhost/crocania/webgetskills.php that it calls

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'];


?>
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

Image

Re: Unity to PHP

Posted: Wed Feb 02, 2011 7:20 pm
by Ravinos
that looks relatively painless!

Re: Unity to PHP

Posted: Wed Feb 02, 2011 8:32 pm
by Callan S.
That's great! Really excited about the potential of this! Thanks Halls! I'll keep coming back to this and studying it.

When you made that gathering demo, by what method did you do the first person camera? I'm still floundering in unity, mostly.

Re: Unity to PHP

Posted: Thu Feb 03, 2011 10:39 am
by SpiritWebb
This is awesome...thanks for this! I do got a question. How would one go about adding the functionality to allow players to see each other move around in real time? And could a chat option be implemented in the same manner? If this is possible. And with this, how much stress can a database handle if several hundreds of players are connected?

Looking at other options other then SmartFoxServer as I did manage to get a connection to a db, its confusing on where to go from there, cause it skips the PHP entirely and I cannot find anything to help in that direction. Unity -> SmartFoxServer -> PHP -> db and back.

Re: Unity to PHP

Posted: Thu Feb 03, 2011 7:04 pm
by hallsofvallhalla
I would not use this method to track dynamic positions. Especially with hundreds of players. You could use it to place objects on a map and such but since it is a Http request I would not use it for a refresh rate under a second or two.



I used the First person camera script.

Re: Unity to PHP

Posted: Sat Feb 05, 2011 2:32 am
by davidc088
That's looking great!

Re: Unity to PHP

Posted: Sat Feb 12, 2011 1:00 am
by SpiritWebb
Database has:

database name: unity
table users, 2 columns. id and player. Only one row, which is my name, with an id of 1.

webtest.js (unityscript)

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://odcmm/UnitySiteTest/webtest.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 ("player").guiText.text=""+formText[0];
     
     
     w.Dispose(); //clear our form in game
   }
}
webtest.php

Code: Select all

<?php 
   $db = mysql_connect("localhost","root","") or die("Could not connect to db");
if(!$db)
die("no db");
if(!mysql_select_db("unity",$db))
die("No Database Selected");
session_start();
?>

<?php

    $player=$_SESSION['player'];
echo $player;

?>
When I pull it up on the browser, I am getting this error:
Notice: Undefined index: player in C:\wamp\www\UnitySiteTest\webtest.php on line 11

Re: Unity to PHP

Posted: Sat Feb 12, 2011 5:43 am
by Callan S.
$_SESSION['player'] doesn't seem to have been set yet? If you just gave $_SESSION['player'] a dummy value it'd probably work. I really must try this with unity myself soon!!!

Re: Unity to PHP

Posted: Tue Feb 15, 2011 8:59 am
by Callan S.
I'm trying the first code, that just grabs a name. And getting this error

Code: Select all

NullReferenceException
getname+$getname$9+$.MoveNext() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/getname.js:24)
What does that mean?

Edit: I'm guessing it perhaps refers to some component I don't have.

I tried out something I found with google

Code: Select all

print("The variable is :"+formText);
And it shows the value down in the editor window.

Is there an easy way to show the info on screen? I've tried this

Code: Select all

function OnGUI()
{
  GUI.Label(Rect(0,0,100,100),formText)
}
And it just gives an error?

Re: Unity to PHP

Posted: Mon Feb 21, 2011 1:05 pm
by hallsofvallhalla
@spirit I built this with sessions in mind. Make sure you are logging in and creating a session in the browser first. Remember QOC had Unity built into the game. SO you may want to remove session and just have unity pass the players name or somekind of ID to the database.


@callan your issue is here

Code: Select all

 GameObject.Find ("Playername").guiText.text=""+formText[0];
do you have a GUI object named playername?