Atlas Download Example

To Download atlas into the game project from external URL we need to download  image and atlas data before we can use atlas.

Let's Create simply Cube mesh and apply to it image from external atlas.

 

void Start() {

	// Creating imag download url
	string imageUrl = path + "example.png";
	WWW www1  = new WWW (imageUrl);


	// Starting image download
	StartCoroutine(StartImageCoroutine(www1));



	// Creating data download url
	string dataUrl = path + "example_data.txt";
	WWW www2  = new WWW (dataUrl);


	// Starting data download
	StartCoroutine(StartDataCoroutine(www2));

}


IEnumerator StartImageCoroutine(WWW www) {
	yield return www;

	// check for errors
	if (www.error == null) {
		atlasImage = www.texture;
		OnLoadProgress ();
	} else {
		Debug.LogError("WWW Error: "+ www.error);
	}    
}


IEnumerator StartDataCoroutine(WWW www) {
	yield return www;

	// check for errors
	if (www.error == null) {
		atlasData = www.text;
		OnLoadProgress ();
	} else {
		Debug.LogError("WWW Error: "+ www.error);
	}    
}

 

After Image and data successfully downloaded we can parse atlas data and apply it to our Cube. 

 

private void OnLoadProgress () {
	loadProgress++;

	if(loadProgress == 2) {
		//image and atlas data downloaded
		//registration new atlas
		TPackManager.load (atlasData, atlasImage, "NewDownloadedAtlas");


		//applying downloaded image to material
		atlasObject.renderer.material.mainTexture = atlasImage;


		//creating mesh texture component
		TPMeshTextureNoHelperEx meshTexture = atlasObject.AddComponent<TPMeshTextureNoHelperEx> ();

		//applying properties and updating view
		meshTexture.atlas = "NewDownloadedAtlas";
		meshTexture.texture = "play";
		meshTexture.UpdateView ();

	}
}