Rendering complete camera view(16:9) onto a texture in Unity3d

前端 未结 2 1118
囚心锁ツ
囚心锁ツ 2021-01-19 13:27

I was playing around with Unity\'s render textures where you can render a camera\'s view onto a texture. However, I noticed that it doesn\'t render the entire camera\'s view

相关标签:
2条回答
  • 2021-01-19 14:04

    It's quite simple. No code required. You need to have same values between your RenderTexture size (x,y) and the RectTransform (width/height) you are using to render your texture to (the one with the RawImage component attached). Let's say you want a standard 16:9 HD aspect ratio. You need to set up your RenderTexture size to 1280x720 and your RectTransform Width and Height to 1280x720 as well. Then you can scale your RectTransform to any size you need to fit the UI layout you are designing.

    0 讨论(0)
  • 2021-01-19 14:06

    With 'RenderTexture' you can specify your texture size: http://docs.unity3d.com/ScriptReference/RenderTexture.Create.html

    It should go like this:

    Camera camera = GameObject.Find("Main Camera");
    int resWidth = Screen.width;
    int resHeight = Screen.height;
    
    RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    camera.targetTexture = rt; //Create new renderTexture and assign to camera
    Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture
    
    camera.Render();
    
    RenderTexture.active = rt;
    screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D
    
    camera.targetTexture = null;
    RenderTexture.active = null; //Clean
    Destroy(rt); //Free memory
    
    0 讨论(0)
提交回复
热议问题