I guess if you’re posting over http to php, you’ll want to convert the values to string, which you add to a WWWForm, and then submit to your server. Here’s a short example which uploads the player’s x, y & z position to an example php script:
using UnityEngine;
using System.Collections;
public class UploadPosition : MonoBehaviour {
public int playerID = 1;
string url = "http://example.com/StorePlayerPosition.php";
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Upload ());
}
}
IEnumerator Upload() {
// Create a Web Form
var form = new WWWForm();
form.AddField("playerID", playerID );
form.AddField("x", transform.position.x.ToString("0.00"));
form.AddField("y", transform.position.y.ToString("0.00"));
form.AddField("z", transform.position.z.ToString("0.00"));
Debug.Log("submitting "+form+" to "+url);
WWW request = new WWW(url, form);
yield return request;
if (!string.IsNullOrEmpty(request.error))
{
Debug.Log(request+" error:
"+request.error);
} else {
Debug.Log ("Sent “+request+” successfully, result:
"+request.text);
}
}
}
I added a player ID value too just for example purposes because you’re quite likely to want something like this, but it’s not vital to the code.
For floating point values like your position, I’ve put a limit on the accuracy to avoid scientific notation being used (for example, if your player position x is 0.0000001, your post request might contain “x=1E-07”). To do this, I used the form of ToString which accepts a formatting parameter and specificed “0.00” as the format, which limits the value to two decimal places.
To get values back from your server, you’ll first - of course - need to write a php script which retrieves the values from your DB. You’d then print out these values to the result in some format of your choosing. Perhaps XML, YAML, or just plain comma separated values.
I’m going to assume the simplest option, which is that your php script prints something out like this:
1.23,3.14,10.92
So you’d request the php script (passing the ID of the player you want), and read the result text. Then split the result txt using the comma delimiter, and parse each of the three x,y,z strings into real float numbers. If you have more complex information to transmit (the player’s other stats, inventory, quest status, etc), you might want to look at using an industry standard information format like XML or YAML, and use a pre-written encoder and decoder to package your data in text form for transmission over http.
Here’s an example of simple comma-separated version:
using UnityEngine;
using System.Collections;
public class RetrievePosition: MonoBehaviour {
public int playerID = 1;
string url = "http://example.com/GetPlayerPosition.php";
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Retrieve());
}
}
IEnumerator Retrieve() {
// Create a Web Form, including player ID
var form = new WWWForm();
form.AddField("playerID", playerID );
Debug.Log("submitting "+form+" to "+url);
WWW request = new WWW(url, form);
yield return request;
if (!string.IsNullOrEmpty(request.error))
{
Debug.Log(request+" error:
"+request.error);
} else {
Debug.Log ("Sent “+request+” successfully, result:
"+request.text);
// split the result using comma as the delimiter
string pos = request.text.Split(‘,’);
// parse the three strings into real float numbers
float x = float.Parse(pos[0]);
float y = float.Parse(pos[1]);
float z = float.Parse(pos[2]);
// assign the position by putting the parsed numbers into a vector3
transform.position = new Vector3(x,y,z);
}
}
}