unity3d - IOS (IPad) How to load a screenshot created at runtime into a Texture or Texture2D -
we facing issue unity. developing ipad app. 1 feature take screenshot , use button. because tree directory in ipad doesn't exist resources folder, can't use "resources.load", , so, because how feature must work, texture won't available @ beginning, can't put in resources folder. have tried several solutions, creating in ipad resources folder (inside documents folder) , trying load texture it, or piece of code:
public texture loadtexturefromfile(string filename) { texture2d texture = new texture2d(1024, 768); filestream fs = new filestream(filename, filemode.open, fileaccess.read); byte[] imagedata = new byte[fs.length]; fs.read(imagedata, 0, (int)fs.length); texture.loadimage(imagedata); return (texture texture); }
any appreciated. we've been stuck issue several days
you can try following:
texture2d tex = new texture2d(screen.width, screen.height); tex.readpixels(new rect(0, 0, screen.width, screen.height), 0, 0); tex.apply();
and put tex
on button.
if want save file use later on, can do:
// encode texture png var bytes = tex.encodetopng(); destroy( tex );
you'll have png-file can use want.
when saved file , want load somewhere documents folder, can try following:
www imagetoloadpath = new www(/var/mobile/applications/xxxxyyyy-xxxx-xxxx-xxxx-xxxxyyyyzzzz/documents/sample.png) //rendering texture yield return imagetoloadpath; // <- actual load of file imagetoloadpath.loadimageintotexture(buttonobject.renderer.material.maintexture texture2d);
those x's , y's id code of application. when you've run application once, can find folder in application.datapath
. peek in there , check need fill in.