Unity3D Texture2D 保存 加载 裁剪 缩略图 转Sprite

Texture2D 保存 加载 裁剪 缩略图 转Sprite

namespace ShangShangQian.Utility
{
    using System.IO;
    using UnityEngine;

    public static class Texture2DUtility
    {
        /// 
        /// 从文件中加载图片
        /// 
        /// 要加载的路径
        /// 
        public static Texture2D LoadTexture2dFromFile(string path)
        {
            Texture2D t2d = new Texture2D(2, 2);
            t2d.LoadImage(File.ReadAllBytes(path));
            t2d.Apply();
            return t2d;
        }
        /// 
        /// 保存图片到文件
        /// 
        /// 要保存的
        /// 
        public static void SaveTexture2d(this Texture2D origin, string fullPath)
        {
            string dir = Path.GetDirectoryName(fullPath);
            string fileName = Path.GetFileNameWithoutExtension(fullPath);
            string fileExtension = Path.GetExtension(fullPath);
            SaveTexture2d(dir, fileName, fileExtension, origin);
        }
        private static void SaveTexture2d(string dirPath, string fileName,string Extension, Texture2D origin)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
           
            byte[] data = origin.EncodeToJPG(100);
            string fullPath = Path.Combine(dirPath, fileName + Extension );
            File.WriteAllBytes(fullPath, data);
        }
 
        /// 
        /// 获取缩略图
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
        {
            Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
            Color[] rpixels = result.GetPixels(0);
            float incX = (1.0f / (float)targetWidth);
            float incY = (1.0f / (float)targetHeight);
            for (int px = 0; px < rpixels.Length; px++)
            {
                rpixels[px] = source.GetPixelBilinear(incX * ((float)px % targetWidth), incY * ((float)Mathf.Floor(px / targetWidth)));
            }
            result.SetPixels(rpixels, 0);
            result.Apply();
            return result;
        }

        /// 
        /// 将图片以中心进行裁剪,处理成正方形
        /// 
        /// 
        /// 
        public static Texture2D CutToSquare(Texture2D t2d)
        {
            //如果是竖图 则以宽为基准
            bool VerticalImage = t2d.height > t2d.width;
            Vector2 startPoint = Vector2.zero;//截取的起始坐标 图片左下角为(0,)
            Vector2 generateSize = Vector2.zero; //要裁剪的分辨率
            if (VerticalImage)
            {
                startPoint.x = 0;
                startPoint.y = (t2d.height - t2d.width) / 2;

                generateSize.x = t2d.width;
                generateSize.y = t2d.width;
            }
            else
            {
                startPoint.x = (t2d.width - t2d.height) / 2;
                startPoint.y = 0;

                generateSize.x = t2d.height;
                generateSize.y = t2d.height;
            }
            int pixel = t2d.height > t2d.width ? t2d.width : t2d.height;
            Texture2D result = new Texture2D((int)generateSize.x, (int)generateSize.y, TextureFormat.RGBA32, false);
            Color[] data = t2d.GetPixels((int)startPoint.x, (int)startPoint.y, (int)generateSize.x, (int)generateSize.y);
            result.SetPixels(data, 0);
            result.Apply();
            return result;
        }
   
        /// 
        /// Texture2D转Sprite
        /// 
        /// 
        /// 
        public static Sprite Texture2dToSprite(Texture2D t2d)
        {
            Sprite sp = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), Vector2.zero);
            return sp;
        }
    }
}

你可能感兴趣的:(unity,Texture2D,裁剪,缩略图)