71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
|
|
public class MaterialOrientationRenamer : EditorWindow
|
|
{
|
|
[MenuItem("Tools/Material Orientation Renamer")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<MaterialOrientationRenamer>("Material Renamer");
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (GUILayout.Button("Rename Selected Materials"))
|
|
{
|
|
RenameMaterials();
|
|
}
|
|
}
|
|
|
|
private void RenameMaterials()
|
|
{
|
|
Object[] selectedObjects = Selection.objects;
|
|
int renameCount = 0;
|
|
|
|
foreach (Object obj in selectedObjects)
|
|
{
|
|
if (obj is Material mat)
|
|
{
|
|
Texture mainTex = mat.mainTexture;
|
|
|
|
if (mainTex != null)
|
|
{
|
|
string path = AssetDatabase.GetAssetPath(mainTex);
|
|
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
|
|
|
|
if (importer != null)
|
|
{
|
|
int width, height;
|
|
GetTextureDimensions(path, out width, out height);
|
|
|
|
string orientation = width >= height ? "Horizontal" : "Vertical";
|
|
string matPath = AssetDatabase.GetAssetPath(mat);
|
|
|
|
string newName = $"{mat.name}_{orientation}";
|
|
AssetDatabase.RenameAsset(matPath, newName);
|
|
renameCount++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
|
|
EditorUtility.DisplayDialog("Renaming Complete", $"{renameCount} materials renamed successfully.", "OK");
|
|
}
|
|
|
|
private void GetTextureDimensions(string assetPath, out int width, out int height)
|
|
{
|
|
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
|
|
string fullPath = Path.Combine(Application.dataPath.Replace("Assets", ""), assetPath);
|
|
|
|
byte[] fileData = File.ReadAllBytes(fullPath);
|
|
tex = new Texture2D(2, 2);
|
|
tex.LoadImage(fileData);
|
|
width = tex.width;
|
|
height = tex.height;
|
|
}
|
|
}
|