- Home /
Using PHP to generate objects or list in Unity with C#
Hello,
I have searched up and down for some information on using PHP to generate content in Unity. Here is what I am trying to do:
The goal is to create an interface in the webplayer generated by entries in a php document.
Example:
Unity contacts php. The php file tells Unity that there are 2 entries. Each entry is for a separate webplayer game. Unity retrieves 2 URLs per entry (one for the webplayer and one for an image location). So now Unity has information that there are 2 games on my server. Unity uses this info to instantiate a prefab (a simple pane) that will act as a button. Once the button is created, the PNG image is applied as a render texture, and the webplayer url is attached in the button script. The newly created button can now be clicked and it will load the new webplayer game.
In this manner a menu list can be dynamically generated based on how many entries there are in the php file. (hopefully all this makes sense)
Since I am a beginner in PHP and C# I am looking for a basic example of the php and the C# script to get me started.
Thank you.
Answer by duck · Oct 11, 2010 at 10:09 PM
First of all read this answer to find out about how to communicate with web scripts in Unity in general:
How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?
You'll want to use the .data variable of the completed WWW object to read the file containing the URLS.
Once you have this data as a string, you'll want to split it up by whatever delimiters you have chosen. Perhaps they're separated by a certain character, with each pair separated by a newline. Or perhaps you've chosen a more advanced data format like JSON or XML.
Assuming the former, you can parse your individual items of data out using something like the method described in this answer (except instead of using a TextAsset, the text comes from the value returned by your WWW call):
putting a large amount of data into structures
Next you'll want to load the texture, which you can do using exactly the same method that you used to load the URL strings, except you need to read out the .texture variable instead of the .data variable on the WWW objecct once the call is complete.
(Referring to this as a RenderTexture is incorrect. A RenderTexture is a texture generated from the view of an in-game camera, and is often used to create realtime reflections).
Finally you might want to use this texture on a button, in a list of GUI buttons - a method do draw a list of textures like this is described in this answer which uses a for loop to draw an array of toggles.
In that example, it uses the number of children of the gameobject as the array of items, so you'd want to substitute that for your list of games.
Also you'll want to use buttons instead of toggle items for the actual GUI elements for each item.
Hope this is enough to get you on your way!
Thanks so much for the help! I'm still working on setting this up, but so far so good.
Answer by furic · Dec 10, 2014 at 06:49 AM
If you use GameAnalytics like me, just use JSON created in PHP and phase them in C#
PHP code:
$output['coins'] = $coinsGain;
$output['gems'] = $gemsGain;
$output['exp'] = $expGain;
$output['status'] = 'success';
$output['checksum'] = md5($coinsGain . '|' . $gemsGain . '|' . $realHash . '|' . $secretKey);
echo json_encode($output);
PHP output:
{"coins":18,"gems":10,"exp":10,"status":"success","checksum":"e147ad20659b7462499b29dee9dc2bb6"}
C# code:
WWW post = new WWW (postURL + someInput);
yield return post;
Hashtable returnParam = (Hashtable)GA_MiniJSON.JsonDecode (post.text);
Your answer
Follow this Question
Related Questions
Web Player and database 1 Answer
Javascript with php 1 Answer
Unity to PHP/MySQL: password and username security 1 Answer
Unity - MySQL data loss 0 Answers