起因:有個需求要批量按比例調(diào)整UI頁面大小。并不是簡單的縮放,是所有素材都需要按比例縮小。于是,圖片首當(dāng)其沖。這里記錄一下解決方案,因為參考了挺多別人的事例,雖然都描述的都差不多,但大部分都只描述了方法,這里記錄一下整個完整的方案。
環(huán)境:Unity2019.4.10f1
需求是是要把所有的圖片按照從 1080x2160 到 720x1440 的等比縮小
直接上代碼吧
public static void ChangeImageSize()
{
//獲取需要處理的文件夾
var oripath = Application.dataPath;
string path = oripath.Substring(0, oripath.LastIndexOf("/", oripath.LastIndexOf("/") - 1)) +
"/art/UIProject/assets";
//獲取文件夾下的所有文件
DirectoryInfo direction = new DirectoryInfo(path);
//DirectoryInfo.GetFiles返回當(dāng)前目錄的文件列表
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
//使用UNITY的進(jìn)度條來顯示處理進(jìn)度
var index = 0;
EditorApplication.update = delegate()
{
bool isCancel =
EditorUtility.DisplayCancelableProgressBar("處理中...", files[index].Name,
(float) index / files.Length);
//從所有文件中篩選出來圖片資源
if (!files[index].Name.EndsWith(".png") && !files[index].Name.EndsWith(".jpg"))
{
index++;
}
else
{
string xmlName = files[index].Name.Split('.')[0];
//圖片的處理方案
var myBitmap = new System.Drawing.Bitmap(files[index].FullName);
var x = Mathf.CeilToInt(myBitmap.Width * 720 / 1080f);
var y = Mathf.CeilToInt(myBitmap.Height * 1440 / 2160f);
var b = new System.Drawing.Bitmap(x, y);
var g = System.Drawing.Graphics.FromImage(b);
// 插值算法的質(zhì)量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(myBitmap, new System.Drawing.Rectangle(0, 0, x, y),
new System.Drawing.Rectangle(0, 0, myBitmap.Width, myBitmap.Height),
System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
myBitmap.Dispose();
//存到原圖的位置
b.Save(files[index].FullName);
b.Dispose();
index++;
}
if (isCancel || index >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
if (isCancel)
{
EditorUtility.DisplayDialog("取消提示", "取消處理后需要把已處理的文件還原才能再次處理。", "確認(rèn)");
}
if (index >= files.Length)
{
EditorUtility.DisplayDialog("圖片處理完成", "處理一次即可,如果誤操作,需要把對應(yīng)目錄下所有圖片文件還原", "確認(rèn)");
}
}
};
//原始處理方案,省了Unity進(jìn)度條等花里胡哨的東西。
// for (int i = 0; i < files.Length; i++)
// {
// if (!files[i].Name.EndsWith(".png") && !files[i].Name.EndsWith(".jpg")) continue;
// string xmlName = files[i].Name.Split('.')[0];
// Debug.Log("imageName:" + xmlName);
// var myBitmap = new System.Drawing.Bitmap(files[i].FullName);
// var x = Mathf.CeilToInt(myBitmap.Width * 720 / 1080f);
// Debug.Log("x:" + x);
// var y = Mathf.CeilToInt(myBitmap.Height * 1440 / 2160f);
// Debug.Log("y:" + y);
// var b = new System.Drawing.Bitmap(x, y);
// var g = System.Drawing.Graphics.FromImage(b);
// // 插值算法的質(zhì)量
// g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// g.DrawImage(myBitmap, new System.Drawing.Rectangle(0, 0, x, y),
// new System.Drawing.Rectangle(0, 0, myBitmap.Width, myBitmap.Height),
// System.Drawing.GraphicsUnit.Pixel);
// g.Dispose();
// myBitmap.Dispose();
// b.Save(files[i].FullName);
// b.Dispose();
// }
}
這里只是把圖片的大小做了變更,其他的屬性都可以類比一下更改。
當(dāng)然需要注意的有一點,就是代碼里使用的System.Drawing這個類Unity里面是不包含的。
我們可以通過在Plugins的目錄下添加System.Drawing.dll這個文件來達(dá)到使用該類的目的。
另外也可以直接通過C#生成解決方案來處理。即直接使用C#使用 原始處理方案 部分代碼生成windows桌面程序等來處理。因為很多工具在你創(chuàng)建C#方案時默認(rèn)是包含該工具類的。
另外有可能在你處理過程中可能會出現(xiàn)如下報錯。
這種錯誤的話可以檢查一下自己傳遞的參數(shù)有沒有問題,因為你如果是計算出圖片寬高的話,很有可能因為四舍五入導(dǎo)致某個數(shù)值為0,這時候可能就會報這個錯誤。
以上。
抽空把工具整理了一下,大家可以下載嘗試一下。工程鏈接
可以下載下面這些來文件來使用。
功能大概就是指定文件或文件夾按格式來處理圖片大小。文章來源:http://www.zghlxwxcb.cn/news/detail-405275.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-405275.html
到了這里,關(guān)于使用 C# / Unity 進(jìn)行圖像處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!