Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Tatanan on July 17, 2015, 10:01:52 AM

Title: Render from camera to texture
Post by: Tatanan on July 17, 2015, 10:01:52 AM
I am trying to take a (portion of a) screenshot with a camera and then put it as main texture in a UI Texture.

This is my code:

  1.                 private IEnumerator RenderTextureOnUpdate(UITexture texture){
  2.                         yield return new WaitForEndOfFrame();
  3.                        
  4.                         Texture2D tex = new Texture2D(434, 608);
  5.                         tex.ReadPixels(new Rect(0, 0, 434, 608), 0, 0);        
  6.                         tex.Apply();
  7.  
  8.                         texture.mainTexture = tex;
  9.                 }

Is it correct?
The UITexture shows always a black image, ie, no texture.

This code is attached to a camera.
Title: Re: Render from camera to texture
Post by: ArenMook on July 21, 2015, 10:55:09 AM
Doesn't seem like an NGUI question here. More of a question on how ReadPixels works, I'd say. I have never used it myself, so I can't suggest much. I would ask why you are making a new texture every time though instead of reusing an existing one. Once you set UITexture's mainTexture reference, it will stay. You can reuse it.