91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
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}");
|
||
}
|
||
}
|
||
}
|