Saving and Loading from Web

Easy Save allows you to save to a MySQL server on the web using the PHP file and MySQL database provided with Easy Save.

It saves to the database using Easy Save’s own format. However, you can use ES2Web.UploadRaw(string data) and ES2Web.LoadRaw() to upload and download a raw string to and from a database.

1. Find the ES2.php and ES2SQL.sql files supplied with Easy Save 2 in the Assets/Easy Save 2/Web folder.
ES2WebFiles

2. Add the ES2 tables to a new or existing MySQL database using the ES2SQL.sql file. Take note of the name of the database you have added the tables to as you will need this at a later stage. Note: Image shows how you can import tables using PHPMyAdmin Control Panel, which is often, but not always, supported by web space providers.
ImportES2SQL

3. Open the ES2.php file and replace the details in quotation marks with the hostname, username, password and database name of the MySQL database containing the ES2 tables. If you are unsure of these details, contact your webspace provider.
ES2MySQLDetails

4. Also in ES2.php, enter the username and password which you will use in Unity when calling ES2Web functions.
ES2PHPUnityDetails

5. Place the ES2.php file on your web server, and then enter the URL to the file into a web browser. If you have followed the previous steps correctly, you should see the message ‘ES2.php and MySQL database are working correctly‘.

 

You are now ready to use ES2Web in Unity.

Make sure that when using the ES2Web functions, you supply the same username and password as specified in your ES2PHP file.

We use coroutines to upload and download from web as this allows us to download data over multiple frames. We advise you familiarise yourself with coroutines before attempting to save or load from web.

In this example we create a coroutine to upload a Mesh to web. You can then start the coroutine using the StartCoroutine() method.

  1. First, we create an ES2Web object, using the URL to our ES2.php file as the path. We can also provide path parameters after the URL. An important parameter is webfilename, which specifies which logical file we would like to save to on our MySQL server.
  2. Other important parameters include webusername and webpassword, which are the username and password specified in our ES2.php file. We can even use the tag and encrypt parameter.
  3. Now we yield ES2Web.Upload(data) to upload our data.
  4. Finally, we check for any errors returned by ES2Web using the ES2Web.errorCode and ES2Web.error variables. A list of errors can be found in the Error Codes section of the ES2Web page.

C#

JS

In this example we download a piece of data from web and load it. You may also download an entire file from web by not specifying a tag as a parameter.

The URL and parameters work in the exact same way as in the Saving to Web section above.

  1. First, create our ES2Web object with our URL.
  2. Now instead of yielding ES2.Upload(), we yield ES2.Download() to download the data from the server.
  3. Once downloaded, and we’ve checked for errors, we can do one of two things.
    1. Save the data to a local file using ES2Web.SaveToFile(path).
    2. Load directly from the downloaded data using ES2Web.Load(tag), or one of the other load functions.

C#

JS

Deleting data using ES2Web.Delete(path) is much like uploading or downloading data.

C#

JS

It is strongly advised that you integrate ES2.php into a login system of your choosing.

The ES2.php file contains an  Authenticate($username, $password)  method which can be modified to integrate into your login system.

It has two parameters:

username is the webusername specified in Unity.

password is the webPassword specified in Unity. By default Easy Save 2 sends the password as an MD5 hash, so you may need to convert your password to an MD5 hash using PHP’s MD5($str) method.

Alternatively you can get ES2 to send your password in plain text. To do this, set the hashType variable of your ES2Web objects to ES2Web.HashType.None. However, it is not advised that you do this unless you are using HTTPS.

The Authenticate method should return false if either the username or password do not match, or true if they both match.

For a list of error codes, see the Error Codes section of the ES2Web documentation.