[Unity工具]批量修改Texture

时间:2022-11-30 10:09:33

BatchModifyTexture.cs

 using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO; public class BatchModifyTexture { [MenuItem("Assets/BatchModifyTexture")]
static void Init()
{
//获取Project视图中的选中目录下的所有图片
Object[] objects = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered);
string dir = Application.dataPath.Substring(, Application.dataPath.LastIndexOf("/")) + "/" + AssetDatabase.GetAssetPath(objects[]);
//Debug.LogError(dir);
string[] paths = Directory.GetFiles(dir, "*.png", SearchOption.AllDirectories); //创建存放目录
string saveDir = Application.dataPath + "/BatchModifyTexture";
if (!Directory.Exists(saveDir))
{
Directory.CreateDirectory(saveDir);
} //图片处理
for (int i = ; i < paths.Length; i++)
{
string path = paths[i];
string assetPath = path.Substring(path.IndexOf("Assets/"));
string name = assetPath.Substring(assetPath.LastIndexOf("\\") + );
//Debug.LogWarning(assetPath);
//Debug.LogWarning(name); //设置成可读
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
textureImporter.textureType = TextureImporterType.Advanced;
textureImporter.isReadable = true;
AssetDatabase.ImportAsset(assetPath); //图片颜色变亮
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
Color[] colors = tex.GetPixels(, , tex.width, tex.height);
Texture2D texTemp = new Texture2D(tex.width, tex.height);
Color[] colorsTemp = new Color[colors.Length]; for (int j = ; j < colors.Length; j++)
{
colorsTemp[j] = colors[j] * 2f;
}
texTemp.SetPixels(, , tex.width, tex.height, colorsTemp);
texTemp.Apply();
byte[] bytes = texTemp.EncodeToPNG();
File.WriteAllBytes(saveDir + "/" + name, bytes);
} AssetDatabase.Refresh();
Debug.Log("BatchModifyTexture Finish");
}
}