Script rendering Mesh to Image with transparent background

Hi,

I have a dynamically generated but static (after generation) mesh. The mesh has a material with an unlit shader using the alpha channel.
Is it possible to render this mesh to an image with transparent background?
The process should happen offscreen.

I found the answer:

Here:

  1. Create a render texture

  2. Attach render texture to a camera,
    which should only capture the object
    / mesh

  3. Set Camera to only render a specific
    layer

  4. On capture (eg k press) move camera
    infront of specific game object and move game object to cameras specific render
    layer.

  5. Capture image given code below /
    answer links.

  6. (If unsused) dispose of Destroy(screenShot )

    private void Start()
    {
        captureCamera.enabled = false;
    }
    
    void LateUpdate()
    {
        capture |= Input.GetKeyDown("k");
        if (capture)
        {
            captureCamera.Render();
            RenderTexture.active = captureCamera.targetTexture;
            Texture2D screenShot = new Texture2D(screenShotWidth, screenShotHeight, TextureFormat.RGBA32, false);
            screenShot.ReadPixels(new Rect(0, 0, screenShotWidth, screenShotHeight), 0, 0);
            RenderTexture.active = null;
            screenShot.Apply();
            byte[] bytes = screenShot.EncodeToPNG();
            System.IO.File.WriteAllBytes(ScreenShotName(screenShotWidth, screenShotHeight), bytes);
            Debug.Log(string.Format(ScreenShotName(screenShotWidth, screenShotHeight)));
            capture = false;
        }
            
    }