Unity實(shí)現(xiàn)圖片生成灰白圖的方法
本文實(shí)例為大家分享了Unity生成圖片灰白圖的具體代碼,供大家參考,具體內(nèi)容如下
效果
原圖
生成出來的灰白圖
制作方法
把文章末尾的的TextureUtils.cs腳本放到工程的Assets / Editor目錄中
然后選中項(xiàng)目中的一張圖片,然后點(diǎn)擊菜單Tools / GenGrayTexture
就會在同級目錄中生成灰白圖片了
// TextureUtils.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; public class TextureUtils : MonoBehaviour { [MenuItem("Tools/GenGrayTexture")] public static void GenGrayTexture() { // 獲取選中的圖片 var textures = Selection.GetFiltered<Texture2D>(SelectionMode.DeepAssets); foreach (var t in textures) { var path = AssetDatabase.GetAssetPath(t); // 如果提示圖片不可讀,需要設(shè)置一下isReadable為true, 操作完記得再設(shè)置為false var imp = AssetImporter.GetAtPath(path) as TextureImporter; imp.isReadable = true; AssetDatabase.ImportAsset(path); var newTexture = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false); var colors = t.GetPixels(); var targetColors = newTexture.GetPixels(); for (int i = 0, len = colors.Length; i < len; ++i) { var c = colors[i]; // 顏色值計(jì)算,rgb去平均值 var v = (c.r + c.g + c.b) / 3f; targetColors[i] = new Color(v, v, v, c.a); } newTexture.SetPixels(targetColors); string fname = path.Split('.')[0] + "_gray.png"; File.WriteAllBytes(fname, newTexture.EncodeToPNG()); imp.isReadable = false; AssetDatabase.Refresh(); } } }
如果要批量修改,可以用Directory.GetFiles接口來獲取特定格式的文件
var files = Directory.GetFiles("D:\\path", "*.*", SearchOption.AllDirectories); foreach(var f in files) { if(!f.EndsWith(".png") && !f.EndsWith(".jpg")) continue; // TODO... }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#?DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹狀模型數(shù)據(jù)
NineRays.WebControls.FlyTreeView?是?9rays.net?推出的一款功能強(qiáng)大的樹狀模型數(shù)據(jù)顯示控件,本文主要介紹了如何使用其并結(jié)合?DataSet對象進(jìn)行數(shù)據(jù)顯示,感興趣的可以了解下2024-04-04c#數(shù)據(jù)綁定之刪除datatable數(shù)據(jù)示例
這篇文章主要介紹了c#刪除datatable數(shù)據(jù)示例,需要的朋友可以參考下2014-04-04