Unity generates a screenshot with a transparent background, the background image of the screenshot is transparent, and the 3D model cannot be masked when the UGUI scrollview slides

Reprint  [Unity3D] Unity3D camera with transparent screenshots - Jingru ♂ fish - Blog Garden

using System;
using UnityEngine;
using System.IO;

public class CropPicture : MonoBehaviour
{
    public Camera cropCamera; //待截图的目标摄像机
    RenderTexture renderTexture;
    Texture2D texture2D;

    void Start()
    {
        renderTexture = new RenderTexture(800, 600, 32);
        texture2D = new Texture2D(800, 600, TextureFormat.ARGB32, false);
        cropCamera.targetTexture = renderTexture;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            RenderTexture.active = renderTexture;
            texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            texture2D.Apply();
            RenderTexture.active = null;
            
            byte[] bytes = texture2D.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "//pic//" + (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds + ".png", bytes);
        }
    }
}

Then drag this script to the main camera

Create a new camera that needs to take screenshots, why do you need to create a new one? Because it can't have a skybox.

Then drag this camera object onto the CropCamera variable on the main camera CropPicture script

Then set the property solid color of the camera that needs to take a screenshot, and set the background to be transparent

This can be used to generate a screenshot of the 3D model and display it on the ui, such as in the list of the character backpack. It is necessary to display the picture, but it is not necessary to display the model. Displaying the model will cause the scrollview of UGUI to not be covered by the mask, and the model will not be displayed when sliding. It has been displayed on the ui, and can only be solved by writing a shader. The shader really doesn't understand. So dynamically generate a screenshot of the model in the backpack, display it on the list, click on the list item, and a separate model is displayed on the right

record it!

Guess you like

Origin blog.csdn.net/github_38633141/article/details/123802781