Upload & download files to a web server using ES3 Cloud
Using the MySQL tables and PHP files included with Easy Save 3, you’re able to upload, download, and sync files with a database on a server. This will also allow you to access these files across platforms.
Note that if you just need files to be backed up, Easy Save works with Steam Auto Cloud, Android Auto Backup and Apple iCloud Backup out of the box so ES3Cloud might not be required depending on on what platforms you’re targeting.
Installation
- Place the ES3Cloud.php file on your server
- The ES3Cloud.php file is found in the Assets/Plugins/Easy Save 3/Web folder in your project.
- Navigate to the file in a web browser
- i.e. https://www.yourserver.com/ES3Cloud.php
- Follow the instructions by entering your database details
- If your server does not support certain features, you may be asked to manually add the tables to the database, and manually create an ES3Variables.php file.
- Take note of the API key you are given when the installation completes
- This can also be found in the ES3Variables.php file.
- You’re ready to use ES3 Cloud from your Unity project
Syncing Files
You can synchronize a local file with the server using the ES3Cloud.Sync method:
- If the local file is newer than the file on the server, or no file exists on the server:
- The file on the server will be overwritten with the local file.
- If the file on the server is newer than the local file, or no file exists locally:
- The local file will be overwritten with the file on the server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create a new ES3Cloud object with the URL to our ES3.php file. var cloud = new ES3Cloud("https://www.myserver.com/ES3Cloud.php", "myAPIKey"); // Synchronise a local file with the cloud for a particular user. yield return StartCoroutine(cloud.Sync("myFile.es3", "myUser", "myUsersPassword")); if(cloud.isError) Debug.LogError(cloud.error); // Synchronise another local file, but make it global for all users. yield return StartCoroutine(cloud.Sync("myGlobalFile.es3")); if(cloud.isError) Debug.LogError(cloud.error); |
Other Methods
Methods are provided to allow more direct control of the uploading and downloading process.
See the Other Methods section of the ES3Cloud page for more information.
Handling Errors
After calling an ES3Cloud coroutine, you should always confirm the request was successful by checking the ES3Cloud.isError variable.
If an error occurred, an error code and error message will be stored in the ES3Cloud.errorCode and ES3Cloud.error variables.
For more information on error codes, see the ES3Cloud.errorCode page.
WebGL and CORS
If your WebGL player and ES3Cloud.php are on the same server, ES3Cloud will work in the exact same way as it does on any other platform.
If your WebGL player is on a different server to your ES3Cloud.php file, WebGL requires that your server has CORS enabled. More information on this can be found in Unity’s WebGL Networking guide.
ES3Cloud on iOS
If using the ES3Cloud.Sync method on iOS you will need to declare that you are using File timestamp APIs using code C617
Managing Users
ES3Cloud provides optional user and password parameters, allowing you to store data which is unique to a user.
ES3Cloud does not contain any login system functionality, so you may need to integrate your project with a login system to generate a username and password to the user. See the ‘Integrating with a login system’ section below for more information.
For security reasons, a hash of the username and password are sent to the server rather than the username and password itself. If you wish to send the raw username and password to the server so that you can use them in a custom PHP script, add them as POST variables using the ES3Cloud.AddPOSTField method.
If no username or password is specified, the file is assumed to be global.
Integrating with a login system
To integrate ES3Cloud with a login system you would need to do the following:
- After creating your ES3Cloud object, use ES3Cloud.AddPOSTField to add your user’s username and password to the request. E.g.
12cloud.AddPOSTField("username", username);cloud.AddPOSTField("password", password); - Modify the ES3Cloud.php file so that immediately after the opening <?php of the file, use $_POST to get the username and password. E.g.
12$username = $_POST["username"];$password = $_POST["password"]; - Authenticate this username and password with your login system (contact your login system provider if you require further information regarding this). If the username and password don’t authenticate, call the Error($headerMsg, $msg, $code) method with an appropriate error and a 403 code. E.g.
12if( !AuthenticateWithYourLoginSystem($username, $password) )Error("Username or password was incorrect", "Username or password was incorrect", 403);