转载 【Unity3D】Unity3D 摄像机带透明截图 - 静茹♂鱼 - 博客园
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);
}
}
}
然后把这个脚本拖到主摄像机上
新建一个需要截图的摄像机,为什么需要新建呢?因为它不能有天空盒。
然后把这个摄像机物体拖到主摄像机CropPicture脚本上的CropCamera变量上
然后设置这个需要截图的摄像机的属性 solid color ,background设置透明
这个可以用于生成3d模型的截图,显示在ui上,比如角色背包的列表中的,需要显示图片,没必要显示模型,显示模型会导致UGUI的scrollview不能被mask覆盖,导致滑动的时候,模型一直显示在ui上面,只能写shader来解决,shader确实不了解。所以动态生成背包中模型的截图,显示在列表上,点击列表项,右侧显示单独的模型
记录一下!