ShanxiKnowledgeBase/SXElectricalInspection/Assets/Editor/ScanAndConvertTextures.cs

63 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEditor;
using UnityEngine;
public class ScanAndConvertTextures : EditorWindow
{
[MenuItem("工具/UI转换工具")]
private static void ShowWindow()
{
GetWindow(typeof(ScanAndConvertTextures));
}
private void OnGUI()
{
GUILayout.Label("将图片批量转换的工具自动将02_UI下的png和jpg格式的图片转换为Sprite并且分辨率改为1024", EditorStyles.boldLabel);
if (GUILayout.Button("开始转换"))
{
ConvertTextures();
}
}
private void ConvertTextures()
{
string folderPath = "Assets"; // 修改为你想要扫描的文件夹路径
string[] files = AssetDatabase.FindAssets("t:Texture2D", new[] { folderPath });
foreach (var file in files)
{
string path = AssetDatabase.GUIDToAssetPath(file);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null && (System.IO.Path.GetExtension(path) == ".png" || System.IO.Path.GetExtension(path) == ".jpg"))
{
if (importer.textureType != TextureImporterType.Sprite)
{
importer.textureType = TextureImporterType.Sprite;
importer.spriteImportMode = SpriteImportMode.Single;
TextureImporterSettings textureSettings = new TextureImporterSettings();
importer.ReadTextureSettings(textureSettings);
textureSettings.maxTextureSize = 1024;
textureSettings.compressionQuality = (int)TextureCompressionQuality.Normal;
importer.crunchedCompression = true;
importer.SetTextureSettings(textureSettings);
importer.SaveAndReimport();
CrunchTexture(path);
}
}
}
Debug.Log("转换成功");
}
private void CrunchTexture(string path)
{
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
if (texture != null)
{
EditorUtility.CompressTexture(texture, TextureFormat.RGBA32, TextureCompressionQuality.Normal);
}
}
}