unity生成透明背景的截图,截图背景图透明,UGUI scrollview滑动的时候不能遮罩3d模型

转载 【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确实不了解。所以动态生成背包中模型的截图,显示在列表上,点击列表项,右侧显示单独的模型

记录一下!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值