Search Unity

Screenshot method is not working

Discussion in 'Scripting' started by namba02250, May 21, 2015.

  1. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    hello guys, im trying to take a screenshot of my full screen and then use it as a texture, but when i load the picture it always loads the last one, and not the one i just take.

    These are the lines that im using.

    Application.CaptureScreenshot (texturePath);

    Texture2D inputTexture = (Texture2D)Resources.LoadAssetAtPath(texturePath, typeof(Texture2D));
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    LoadAssetAtPath works within your project folder, and works only in the editor. The file paths that it uses are relative to the project file, while CaptureScreenshot.... Actually I'm not sure where CaptureScreenshot places things, the documentation is entirely vague on that. In any case, I'm certain it's a different path from what LoadAssetAtPath uses.

    My recommendation is to use a render texture. Is there a particular reason that won't work?
     
  3. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    actually, both are saving the image in the same path, the problem is that when i call the load function it loads the last image and not the one i just took in the line before
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    1: Hmm, maybe CaptureScreenshot send it to the working directory, which could feasibly be the project root when you're in the editor mode.

    2: Do you only ever need it to function in the Editor? LoadAssetAtPath is still editor-only (and deprecated at that).

    3: The reason it loads the last one is that CaptureScreenshot probably doesn't actually capture it until the frame is rendered. Wait a frame before you read it in from the file.
     
  5. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    yees i think the "3" thing is what is happening, but i dont know how to fix it, and.. no i need it to work on android devices
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    So back to my first question, is there a reason that a render texture won't do what you need?
     
  7. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    i tried to use Render Texture, but it took different pictures depending on my "Game" window size =O
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'm not sure I follow. How did you try to use RT's? How did it "take different pictures"? I think that's the problem we need to solve here.
     
  9. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    first of all, thanks for your patient =p (my english is not the best), i tried to use RenderTexture, and Texture2D.readPixels, but it gave me different results when i edit the "Game" window size in the editor..... What i need is to take a picture of the screen and use it as a texture for a gameobject
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I still don't have enough information to tell what you were doing wrong with RenderTexture. (Texture2D.ReadPixels is separate, and unrelated) I need to see perhaps some sample code from when you tried RT's, some screenshots of what you mean by 'different results'. Help me help you!
     
  11. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19

    Texture2D tex = new Texture2D(mCamera.width, mCamera.height,TextureFormat.ARGB32,false);

    RenderTexture currentActiveRT = RenderTexture.active;

    tex.ReadPixels(new Rect(0, 0, mCamera.width, mCamera.height),0,0);

    tex.Apply ();

    RenderTexture.active = currentActiveRT;
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, the RenderTexture stuff in that code is doing absolutely nothing. You weren't testing RenderTexture at all; you were only testing ReadPixels.... which, come to think of it, is probably a better solution than using a RenderTexture anyway.

    So, let's forget RenderTextures for now and focus on ReadPixels.

    Now my first question is... what the hell is mCamera? It's clearly not a Camera, because that class doesn't have a "width" or "height" property. In any case, if you're trying to capture the whole screen, you will want to use Screen.width and Screen.height there.

    So, try that and see where we get.

    Also, use [code ] code tags[/code] - makes your code easier to read on the forums.
     
  13. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    the mCamera is a webCamTexture, and i wanted to take a screenshot of the camera, but then i scale the camera to fullscreen so... i want to take the picture to it.

    You'll ask, Why are you screenshoting a camera, when you can take a photo and.. this is because i need to use the result of a shader applied to the camera, but i cant get that information.. soo im trying the worst way to do it haha screenshot
     
  14. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    ?
     
  15. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I think, in essence, StarManta would like to see the entirety of your script posted here for reference. As he suggests, please make proper use of the "code" tags to make your script more readable. If you're unsure of how to do this, please read the "Using Code Tags Properly" sticky post at the top of the Scripting forums.
     
  16. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If the webcam image you have is on the screen, you don't want to use WebCamTexture. You're taking a screenshot now; forget the webcam exists. Use Screen.width and Screen.height.
     
    krougeau likes this.