Allow user to download images from game assets

HI,

I am a working on an Android project, which is almost over, except for one feature that I want to add to the game. I want the player to be able to download some wallpaper images that is included in the the projects asset, just as bonus content. First I thought of displaying the images as GUITexture and then saving screenshots using Application.CaptureScreenShot(), but it doesn’t seem to work. There are some work around for taking screenshots that I came across online.

However, I think that a better solution would be to copy the asset file, i.e. the .jpg wallpapers to the SDCard of the user or maybe internal storage, as screenshot might have encoding issues also. Can this be done, without too much complications?

Thanks,
Vatsal Ambastha

Re: Application.CaptureScreenShot() - are you sure you have permission to write the file at the location you specify? You should be able to write the file at Application.persistentDataPath. This doesn’t necessarily solve your problem of providing the image for the user. I don’t know how the sandboxing on Android works, but you’ll likely have to provide a plugin to move the file to the gallery or some other place you have permission. Prime31’s Etcetera plugin has a saveImageToGallery() method that should allow you to move the file.

As for getting the bytes to begin with you have a couple of choices beyond CaptureScreenShot(). If you want to use a texture in your app, you can use Texture2d.EncodeToPNG() to create the PNG, and then use File.WriteAllBytes() to write the file wherever you have permission.

Another option is to store the file in your resources directory as a TextAsset. Change the file extension of the image file to .bytes. So ‘image.png’ becomes ‘image.bytes’, and then drag and drop it into the Resources folder. To write the image out you can do:

var image = Resources.Load(filename) as TextAsset;
File.WriteAllBytes(Application.persistentDataPath+"/"+filename+".png", image.bytes); 

Where filename would be the name of the file without the “.bytes” extension.