Files
PrinceOfGlory/Assets/__Test/Editor/TextureImportSettingsEditor.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

91 lines
2.7 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;
using System.Collections.Generic;
public class TextureImportSettingsEditor : EditorWindow
{
[MenuItem("Tools/Set Texture Import Settings")]
public static void ShowWindow()
{
// 显示自定义编辑器窗口
GetWindow<TextureImportSettingsEditor>("Texture Import Settings");
}
private void OnGUI()
{
if (GUILayout.Button("Set Texture Import Settings for Current Scene"))
{
SetTextureImportSettings();
}
}
// 设置当前场景中所有贴图的导入参数
private static void SetTextureImportSettings()
{
// 获取当前场景中的所有材质
HashSet<Texture> textures = new HashSet<Texture>();
// 遍历场景中的所有 GameObject获取材质中的贴图
foreach (var renderer in FindObjectsOfType<Renderer>())
{
// 获取材质的所有贴图
foreach (var material in renderer.sharedMaterials)
{
if (material != null)
{
// 遍历材质的所有属性
foreach (var property in material.GetTexturePropertyNames())
{
Texture texture = material.GetTexture(property);
if (texture != null)
{
textures.Add(texture); // 添加到集合,避免重复
}
}
}
}
}
// 遍历所有找到的贴图并设置导入参数
foreach (var texture in textures)
{
SetTextureImportSettings(texture);
}
Debug.Log("Texture import settings have been updated.");
}
// 设置单个纹理的导入参数
private static void SetTextureImportSettings(Texture texture)
{
string assetPath = AssetDatabase.GetAssetPath(texture);
if (string.IsNullOrEmpty(assetPath))
{
Debug.LogWarning($"Texture not found in assets: {texture.name}");
return;
}
// 获取纹理的导入设置
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter != null)
{
// 设置最大尺寸为 2048
textureImporter.maxTextureSize = 2048;
// 设置压缩格式为 ASTC 8x8
textureImporter.textureCompression = TextureImporterCompression.Compressed;
textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ASTC_8x8);
// 应用这些更改
EditorUtility.SetDirty(textureImporter);
textureImporter.SaveAndReimport();
}
else
{
Debug.LogWarning($"TextureImporter not found for: {texture.name}");
}
}
}