Uploading and Downloading Textures

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Uploading and Downloading Textures

Post by Joel »

Uploading and Downloading Textures

Before beginning this example, please make sure you have installed the ES2SQL.sql MySQL database and ES2.php to your server using the instructions HERE.

In this example, we will create a script which can either Upload or Download the texture on the object it is attached to. We will use this script to Upload the texture of one object to the database. Then when you reload the scene, it will Download this texture from the database and apply it to a blank plane.

Preparing the texture

So that we can read the texture data, you must set the Texture's 'Texture Type' to 'Advanced', and tick the box labelled 'Read/Write Enabled'.

You must also set the texture to the ARGB32 or RGB24 image format.
Texture Settings.png
The Script
using UnityEngine;
using System.Collections;

public class UploadDownloadTexture : MonoBehaviour 
{
	public enum Mode {Upload, Download};
	public Mode mode = Mode.Upload;
	public string url = "http://www.server.com/ES2.php";
	public string filename = "textureFile.txt";
	public string tag = "textureTag";
	public string webUsername = "ES2";
	public string webPassword = "65w84e4p994z3Oq";
	
	void Start () 
	{
		// Get texture, or throw error if texture doesn't exist.
		Texture2D texture = GetTexture ();
		if(texture == null)
			Debug.LogError ("There is no texture attached to this object.");
		
		if(mode == Mode.Upload)
			StartCoroutine(Upload(texture));
		else
			StartCoroutine(Download());
	}
	
	/* Uploads a texture to the server */
	private IEnumerator Upload(Texture2D texture)
	{
	    ES2Web web = new ES2Web(url, CreateSettings());
	      
	    // Start uploading our Texture and wait for it to finish.
	    yield return StartCoroutine(web.Upload(texture));
	      
	    if(web.isError)
	        Debug.LogError(web.errorCode + ":" + web.error);
		else
			Debug.Log ("Uploaded Successfully. Reload scene to load texture into blank object.");
	}
	
	/* Downloads a texture from the server */
	private IEnumerator Download()
	{
		 ES2Web web = new ES2Web(url, CreateSettings());
	      
	    // Start downloading our Texture and wait for it to finish.
	    yield return StartCoroutine(web.Download());
	      
	    if(web.isError)
		{
			// If there's no data to load, return.
			// Note: code "05" means that no data was found.
			if(web.errorCode == "05")
				return false;
				
	        Debug.LogError(web.errorCode + ":" + web.error);
		}
		
		// Load the Texture from the ES2Web object, using the correct tag.
		SetTexture( web.Load<Texture2D>(tag) );
		
		// Delete the data so our example works properly.
		yield return StartCoroutine (Delete());
		Debug.Log ("Texture successfully downloaded and applied to blank object.");
	}
	
	/* Deletes a texture from the server */
	private IEnumerator Delete()
	{
		 ES2Web web = new ES2Web(url, CreateSettings());
	      
	    // Delete our Texture and wait for confirmation.
	    yield return StartCoroutine(web.Delete());
	      
	    if(web.isError)
	        Debug.LogError(web.errorCode + ":" + web.error);
	}
	
	/* Creates an ES2Settings objects from the user
	 * defined settings */
	private ES2Settings CreateSettings()
	{
		ES2Settings settings = new ES2Settings();
		settings.webFilename = filename;
		settings.tag = tag;
		settings.webUsername = webUsername;
		settings.webPassword = webPassword;
		return settings;
	}
	
		/* Gets the Texture applied to this object, 
	 * or returns null if there's not one. */
	private Texture2D GetTexture()
	{
		if(renderer.material != null)
			if(renderer.material.mainTexture != null)
				return renderer.material.mainTexture as Texture2D;
		return null;	
	}
	
	/* Sets the texture to the one specified, or throws an error
	 * if there's no Material to apply the texture to. */
	private void SetTexture(Texture2D texture)
	{
		if(renderer.material != null)
				renderer.material.mainTexture = texture;
		else
			Debug.LogError ("There is no material attached to this object.");
	}
}
Notes
  • In this example, notice how both of the scripts use the same settings. The only difference is that one is set to Upload, and the other set to Download.
  • Included below is the Unity Package for the example. This package may also be included in the Examples folder of your copy of Easy Save 2.
Upload and Download a Texture Example.unitypackage
Locked