Unity3D自动截取物体缩略图

Unity3D自动截取物体缩略图

1.也可以截取六视图,在CaptureSetting中设置不同的rotation即可
2.待实现:
将所有模型的缩略图尺寸调整到相近大小

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AutoScreenCapture : MonoBehaviour
{
    [SerializeField] RenderTexture target;
    [SerializeField] Button save;
    [SerializeField] Camera renderCamera;
    [SerializeField] List<GameObject> models = new List<GameObject>();
    [SerializeField] Transform root;
    [SerializeField] List<CaptureSetting> settings = new List<CaptureSetting>();

    int index = 0;
    private void Start()
    {
        for (int i = 0; i < root.childCount; i++)
        {
            models.Add(root.GetChild(i).gameObject);
        }
        save.onClick.AddListener(() =>
        {
            StartCoroutine(Capturing());
            //Debug.Log(SizeToFit());
            //SaveScreebCapture();
        });

    }
    
   
    IEnumerator Capturing()
    {
        var index = 0;
        for (int i = 0; i < settings.Count; i++)
        {
            renderCamera.backgroundColor = settings[i].bg;
            SetRootDirection(root, settings[i].rotate);
            index = 0;
            while (index < models.Count)
            {
                models[index].gameObject.SetActive(true);
                yield return new WaitForEndOfFrame();
                //yield return new WaitForSeconds(0.1f);
                SaveImage(settings[i],index, CreateFrom(target));
                target.DiscardContents();
                //yield return new WaitForSeconds(0.1f);
                yield return new WaitForEndOfFrame();
                models[index].gameObject.SetActive(false);
                index++;

            }
        }
       
    }
   
    void SetRootDirection(Transform p_root,Vector3 p_direction)
    {
        p_root.transform.localEulerAngles = p_direction;
    }
    public void SaveImage(CaptureSetting p_setting,int p_index, Texture2D p_texture2D)
    {
        var path = Application.dataPath + string.Format(p_setting.path + models[p_index].name.Split('_')[0] + p_setting.suffix + ".png");
        //Debug.Log(path);
        if(p_setting.cutBg)
            p_texture2D = CutBg(p_texture2D,p_setting);
        if (p_setting.bg2Transparent)
            Bg2Transparent(p_texture2D,p_setting.bg);

        var bytes = p_texture2D.EncodeToPNG();

        System.IO.File.WriteAllBytes(path, bytes);
    }

    void Bg2Transparent(Texture2D p_texture2D,Color p_bg)
    {
        var width = p_texture2D.width;
        var height = p_texture2D.height;

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                var color = p_texture2D.GetPixel(i, j);
                if (color == p_bg)
                {
                    color.a = 0;
                    p_texture2D.SetPixel(i, j, color);
                }
            }
        }
        p_texture2D.Apply();
    }
    Texture2D CutBg(Texture2D p_texture2D,CaptureSetting p_setting)
    {
        var top = 0;
        var bottom = p_texture2D.height;
        var left = p_texture2D.width;
        var right = 0;

        for (int x = 0; x < p_texture2D.width; x++)
        {
            for (int y = 0; y < p_texture2D.height; y++)
            {

                var color = p_texture2D.GetPixel(x, y);

                if (color!= p_setting.bg)
                {
                		//只执行一次
                    if (bottom > y)
                        bottom = y;
                    if (top < y)
                        top = y;
                    //只执行一次
                    if (left > x)
                        left = x;
                    if (right < x)
                        right = x;
                }
               
            }
        }
        top += p_setting.border;
        bottom -= p_setting.border;
        left -= p_setting.border;
        right+= p_setting.border;

        top = Mathf.Clamp(top, 0, p_texture2D.height);
        bottom = Mathf.Clamp(bottom, 0, p_texture2D.height);
        left = Mathf.Clamp(left, 0, p_texture2D.width);
        right = Mathf.Clamp(right, 0, p_texture2D.width);

        var height = top - bottom;
        var width = right - left;
       
        Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);

        var colors = p_texture2D.GetPixels(left, bottom, width, height);
        texture2D.SetPixels(0, 0, width, height, colors);
        texture2D.Apply();
        return texture2D;
    }

    public Texture2D CreateFrom(RenderTexture renderTexture)
    {
        
        Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
        RenderTexture.active = renderTexture;

        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        texture2D.Apply();

        return texture2D;
    }
}
 [System.Serializable]
    public struct CaptureSetting
    {
        public Vector3 rotate;
        public string suffix;
        public string path;
        public bool similarImageSize;
        public bool cutBg;
        public bool bg2Transparent;
        public Color bg;
        public int border;
    }

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值