Search Unity

Rendertexture screenshot to NGUI UITexture

Discussion in 'Scripting' started by Tripwire, Sep 17, 2014.

  1. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    Hi,

    I'm trying to make a screenshot of a particular item on my screen. Here are some screenshots of my 2nd camera setup:


    Camera Preview:


    Item i'm taking a screenshot from:


    The following code will take the screenshot and put it on a UITexture (NGUI) with the same size as the item i'm taking a screenshot from:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TakeScreenShot : MonoBehaviour
    5. {
    6.     public Camera RenderCam;
    7.  
    8.     public UITexture tex;
    9.  
    10.     private Texture2D image;
    11.  
    12.     private int width = 863,
    13.                 height = 402;
    14.    
    15.  
    16.     public void CaptureImage()
    17.     {
    18.         RenderTexture rt = new RenderTexture(width, height, 24);
    19.         image = new Texture2D(width, height, TextureFormat.ARGB32, false);
    20.  
    21.         //Camera settings
    22.         RenderCam.aspect = 1.0f;
    23.         RenderCam.targetTexture = rt;
    24.         RenderCam.Render();
    25.  
    26.         RenderTexture.active = rt;
    27.         image.ReadPixels(new Rect(0.0f, 0.0f, width, height), 0, 0);
    28.         image.Apply();
    29.         tex.mainTexture = image;
    30.         RenderTexture.active = null;
    31.         RenderCam.targetTexture = null;
    32.  
    33.     }
    34. }
    The result:


    I just don't know anymore what i'm doing wrong here :( any help would be very appreciated!
     
  2. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    Anyone?
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you check whether the render texture is correct? You may expose it as variable. When you double click on it, it will be shown like a texture. This helps you to isolate the issue further.
    The other option is certainly that the scale of your UITexture is not correct. Did you check the border and uvRect of your UITexture?