0
\$\begingroup\$

A Gameobject sprite, a Texture2D which is written into using "Texture2D.ReadPixels" works/is visible in the Editor/Play Mode but is not/is invisible when built.

The Texture2D is written to with the RT at timed intervals, and when the game is first built, before the interval where RT is written to by the game, the RT seems to work, but as soon as it's written to by the script, it disappears.

What's odd is, I've tried replacing the Sprite with an Image and instead of using the Texture2D using a material with the RenderTexture itself, but that is invisible when built as well.

The fact that the Texture2D sprite appears before being written to would make me think it's not an inability to render, but an error with the Texture2D being written to, but that doesn't explain why the RenderTexture itself doesn't appear when used as an Image.

Overall, I'm just really confused and don't know what's going on or what to do.

Part of the code where the RT is written into the Texture2D:

public Texture2D CameraFeed;
    
    [...]
    

IEnumerator RefreshCamera(float RefreshRate)
{
        
    yield return new WaitForSeconds(RefreshRate);
    yield return new WaitForEndOfFrame();
    
    CameraFeed.Reinitialize(1, 1);
    CameraFeed.SetPixel(1,1, Color.black);
    CameraFeed.Reinitialize(510, 492);

    if(SelectedCamera == DiningRoom && CameraPowers.DiningRoomPower > 0)
    {
        RenderTexture.active = DiningRoomRT;
        CameraFeed.ReadPixels(new Rect(0, 0, DiningRoomRT.width, DiningRoomRT.height), 0, 0);
        CameraFeed.Apply();
    }
    [...]
    //Terminal
    
    TerminalLinesVisible = 0;
    TerminalTimer = 0;
    
    //
    
    StartCoroutine(RefreshCamera(RefreshRate));
    
    
}
\$\endgroup\$
3
  • \$\begingroup\$ What are you putting into the RenderTexture DiningRoomRT, and how? Is it possible that rendering to this target is failing, leading to a render texture that's all black/transparent? \$\endgroup\$
    – DMGregory
    Commented Aug 20 at 17:37
  • \$\begingroup\$ It's directly rendered to by a camera. I don't see why it would fail to render in the build but not in the editor. \$\endgroup\$
    – Konxovar
    Commented Aug 20 at 17:46
  • \$\begingroup\$ When you say you previewed it with an Image component, do you mean a RawImage? If not, test with that to check whether there's anything being drawn into the RenderTexture. If not, walk us through the camera setup so we can reproduce the problem to investigate where it's going wrong. \$\endgroup\$
    – DMGregory
    Commented Aug 20 at 18:09

1 Answer 1

1
\$\begingroup\$

I was able to get it to work (I don't know why, that's above my head) by rewriting the code to be more like this. https://docs.unity3d.com/ScriptReference/Camera.Render.html

Instead of directly reading from the Render Texure, I had to change the RT in the script to the camera and then take it through the camera. DiningRoomRT in the following is a Camera whereas it was a Render Texture in the post:

IEnumerator RefreshCamera(float RefreshRate)
{
        
    yield return new WaitForSeconds(RefreshRate);
    yield return new WaitForEndOfFrame();


    var currentRT = RenderTexture.active;
    
    if(SelectedCamera == DiningRoom && CameraPowers.DiningRoomPower > 0)
    {
        RenderTexture.active = DiningRoomRT.targetTexture;
    
        DiningRoomRT.Render();
        CameraFeed.ReadPixels(new Rect(0, 0, DiningRoomRT.targetTexture.width, DiningRoomRT.targetTexture.height), 0, 0);
        CameraFeed.Apply();
        yield return CameraFeed;
    }
        
    [...]
    //Terminal
    
    TerminalLinesVisible = 0;
    TerminalTimer = 0;
    
    //
    
    StartCoroutine(RefreshCamera(RefreshRate));
    
    
}

A few other things since I'm not sure what finally made it start working: I set it from Vulkan to DirectX. I turned on Texture Streaming Furthermore, the RT Cameras were EditorOnly tagged.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .