Hi, i try to extract the image from camera and push to the RenderTexture, and show the image in RawImage. It works in editor but failed when install the app to AVP, some pictures are following, thanks for replying!
A couple of things jump out at me as being questionable. First, you set RenderTexture.active without unsetting it. Typically, you set RenderTexture.active temporarily, do whatever you need to do with it active (ReadPixels, in this case), and then restore its original value (as the example in the linked documentation does). I suspect that’s why you’re seeing the crash.
Second, I’m not sure why you’re copying the RenderTexture to another texture. You should just set the RenderTexture as the texture on screenshotDisplay.
Here’s an example that I just tested in a visionOS build that doesn’t copy the RenderTexture:
using UnityEngine;
using UnityEngine.UI;
public class RenderTextureUpdater : MonoBehaviour
{
public Camera camera;
public RawImage rawImage;
RenderTexture m_RenderTexture;
void Start()
{
m_RenderTexture = new(1920, 1080, 24, RenderTextureFormat.ARGB32);
rawImage.texture = m_RenderTexture;
}
void Update()
{
var previousTargetTexture = camera.targetTexture;
camera.targetTexture = m_RenderTexture;
camera.Render();
camera.targetTexture = previousTargetTexture;
}
}
Here’s an example that copies the texture (like your screenshot), but again, this is inefficient comparing to just using the RenderTexture directly. When you use a RenderTexture, we transfer its contents via a GPU blit that is much faster than the CPU transfer used for Texture2Ds.
using UnityEngine;
using UnityEngine.UI;
public class RenderTextureUpdater : MonoBehaviour
{
public Camera camera;
public RawImage rawImage;
RenderTexture m_RenderTexture;
void Start()
{
m_RenderTexture = new(1920, 1080, 24, RenderTextureFormat.ARGB32);
}
void Update()
{
var previousTargetTexture = camera.targetTexture;
camera.targetTexture = m_RenderTexture;
camera.Render();
camera.targetTexture = previousTargetTexture;
var previousActiveTexture = RenderTexture.active;
RenderTexture.active = m_RenderTexture;
var copiedTexture = new Texture2D(m_RenderTexture.width, m_RenderTexture.height, TextureFormat.RGB24, false);
copiedTexture.ReadPixels(new(0, 0, m_RenderTexture.width, m_RenderTexture.height), 0, 0);
RenderTexture.active = previousActiveTexture;
if (rawImage.texture != null)
Destroy(rawImage.texture);
rawImage.texture = copiedTexture;
}
}