Destroy vs Release render texture

Hi i’m creating an app in which I’m creating and destroying render textures based on different images sizes but I don’t know exactly what method use for destroying a render texture before creating another one. Should I use Destroy or Release the render texture to create another one with different size. What’s the difference?? Thanks.

The documentation explains it pretty well:
https://docs.unity3d.com/ScriptReference/RenderTexture.Release.html

In layman terms, Release() destroys the GPU (aka “hardware”) resources associated to the render texture, but the RenderTexture object used by Unity to wrap around this GPU data and expose it nicely to you is kept around and will re-generate the underlying GPU data if used again, as it would if you manually called Create().

Destroy() however, releases both GPU memory and the RenderTexture object.

So which one to use depends on the lifecycle of your render textures, if you’re reusing them in a pool-like fashion, etc.

The documentation for RenderTexture itself goes into further detail:

2 Likes

Thanks for the explanation.there is something I don’t quite understan, since you have to use new Rendertexture to change the size of the rendertexture your making reference. wouldn’t it pile up if I use only Release () instead of Destroy?. I’m using a Rendertexture that changes its size everytime y pick an image so in order to change its size I have to use new Rendertexture (). will it create multiple instances of the rendertexture if I use only release () instead of Destroy() ?? Thanks

That doesn’t change the size of the texture, it creates an entirely new texture of a different size. Anytime you use the “new” keyword in any programming language, it means you’re creating a new object (and allocating memory for it).

To resize a texture, you Release() its data and then change its size:

rt.Release();
rt.width = newWidth;
rt.height = newHeight;
rt.Create();

Yep of course, if you create a new RenderTexture and only Release() it but never call Destroy() then the GPU data will be released but the RenderTexture object will be kept around forever.

Rule of thumb:
to “undo” rt = new RenderTexture() call Destroy(rt).
to “undo” rt.Create() call rt.Release().

kind regards,

2 Likes

Thanks bro just the explanation I needed.