Author Topic: Render from camera to texture  (Read 1412 times)

Tatanan

  • Jr. Member
  • **
  • Thank You
  • -Given: 11
  • -Receive: 0
  • Posts: 81
    • View Profile
    • Casual Arena
Render from camera to texture
« 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.
Game developer on Casual Arena.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Render from camera to texture
« Reply #1 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.