Search Unity

Screengrab to Texture2D

Discussion in 'Scripting' started by renman3000, Aug 4, 2015.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi, I am struggling with how, once I take a screengrab, how to apply it to a Texture2D.

    I have looked at the www. and loadImage() but one requires a url and one an image. All I have is a path..."documents/pictures/screengrab". How do I take this path and apply it return to a Texture2D?

    Thank you
     
  2. Daso1970

    Daso1970

    Joined:
    Aug 3, 2015
    Posts:
    9
    This would load an image into a Texture2D:

    Code (csharp):
    1. WWW www = new WWW("file://"+ Application.dataPath +"/filename.png");
    2. Texture2D texture = new Texture2D(99, 99, TextureFormat.ARGB32, false);
    3. www.LoadImageIntoTexture(texture);
    "Application.dataPath" in the example is the absolute path to the game's Assets folder. Can be replaced to a different folder on your computer (just keep in mind that it should work on other people's computers as well so hardcoded paths outside the Assets folder is probably not a good idea).
     
    renman3000 likes this.
  3. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130