unity-editor-tools/editor/ImageConverter.cs
2025-03-17 01:02:24 +01:00

77 lines
2.5 KiB
C#

using UnityEngine;
using UnityEditor;
namespace editor
{
public class ImageConverter : EditorWindow
{
[MenuItem("Tools/Image Converter Window")]
public static void ShowWindow()
{
GetWindow<ImageConverter>("Image Converter");
}
private void OnGUI()
{
GUILayout.Label("Image Converter", EditorStyles.boldLabel);
GUILayout.Space(10);
GUILayout.Label($"Selected: {Selection.objects.Length}");
GUILayout.Space(20);
if (GUILayout.Button("To Sprite"))
{
ConvertSelectedTexturesToSprites();
}
if (GUILayout.Button("To Default"))
{
ConvertSelectedTexturesToSDefault();
}
}
private static void ConvertSelectedTexturesToSprites()
{
int count = 0;
Debug.Log("Convert selected textures...");
foreach (Object obj in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path))
continue;
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter == null)
continue;
textureImporter.textureType = TextureImporterType.Sprite;
textureImporter.spriteImportMode = SpriteImportMode.Single;
textureImporter.SaveAndReimport();
count++;
}
Debug.Log($"Converted {count} textures.");
}
private static void ConvertSelectedTexturesToSDefault()
{
int count = 0;
Debug.Log("Convert selected textures...");
foreach (Object obj in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path))
continue;
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter == null)
continue;
textureImporter.textureType = TextureImporterType.Default;
textureImporter.spriteImportMode = SpriteImportMode.Multiple;
textureImporter.SaveAndReimport();
count++;
}
Debug.Log($"Converted {count} textures.");
}
}
}