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;
    }

Unity3D自动截取物体缩略图_第1张图片
Unity3D自动截取物体缩略图_第2张图片

你可能感兴趣的:(Unity,3D与编程语言,unity)