0

We are facing an issue with Unity. We are developing an IPAD app. One feature is to take a screenshot and use it for a button. Because the tree directory in the IPAD where it doesn't exist a Resources folder, we can't use "Resources.Load", and even so, because how this feature must work, the texture won't be available at the beginning, so we can't put it initially in the Resources folder. We have tried several solutions, like creating in the IPAD a Resources folder (inside the Documents folder) and trying to load the texture from it, or even this 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 as Texture);
}

Any help would be greatly appreciated. We've been stuck with this issue for several days

1 Answer 1

1

You can try the 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 the button.

If you want to save it to a file for use later on, you can do:

// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy( tex );

You'll have a PNG-file you can use like you want.

When you saved the file and want to load it somewhere from your Documents folder, you can try the following:

WWW imageToLoadPath = new WWW(/var/mobile/Applications/XXXXYYYY-XXXX-XXXX-XXXX-XXXXYYYYZZZZ/Documents/sample.png) //rendering texture
yield return imageToLoadPath; // <- the actual load of file
imageToLoadPath.LoadImageIntoTexture(buttonObject.renderer.material.mainTexture as Texture2D);

Those X's and Y's are the ID code of your application. When you've run the application once, you can find that folder in Application.dataPath. You could peek in there and check what you need to fill in.

5
  • Hi Joetjah. Thanks for your suggestion, but this is not what we need. It works, a screenshot of the desired size is taken and displayed properly in the button. But we need this same screenshot to be loaded next time we launch the app. The problem is how to load this image from the Documents folder of the IPAD, initially it is not available to put it in the resources folder Apr 25, 2013 at 12:07
  • @user2319223 Check out the code I've added to my answer. Would that help you?
    – Joetjah
    Apr 25, 2013 at 12:15
  • Also note that you can use the same WWW object to write the files to a specified folder you've chosen yourself, for example to a Screenshot-folder.
    – Joetjah
    Apr 25, 2013 at 12:17
  • Hi. Yes, finally we got the texture to be loaded using the class WWWW. Thanks for everything Apr 29, 2013 at 8:24
  • @user2319223 Glad I could help! If you'd like, please mark the answer as accepted so people searching for similar questions can refer to this.
    – Joetjah
    Apr 29, 2013 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.