Files
AIGame/AIClient/Assets/Editor/TexturePlatformPostprocessor.cs

71 lines
3.1 KiB
C#

#if UNITY_EDITOR
using System;
using HotUpdate.UI;
using UnityEditor;
namespace AIGame.EditorTools
{
/// <summary>UI 图集原图保持未压缩,其他图片按平台设置压缩格式。</summary>
public sealed class TexturePlatformPostprocessor : AssetPostprocessor
{
private void OnPreprocessTexture()
{
// 只处理项目图片,不覆盖 Packages 中的包资源。
if (!assetPath.StartsWith("Assets/", StringComparison.OrdinalIgnoreCase))
return;
var importer = (TextureImporter)assetImporter;
if (UiAtlasPaths.IsTexturePath(assetPath))
{
ConfigureUiAtlasSource(importer);
return;
}
// Unity 的 Windows 纹理设置使用 Standalone 平台组。
SetPlatformFormat(importer, "Standalone", TextureImporterFormat.DXT5);
SetPlatformFormat(importer, "iPhone", TextureImporterFormat.ASTC_6x6);
SetPlatformFormat(importer, "Android", TextureImporterFormat.ASTC_6x6);
SetPlatformFormat(importer, "WebGL", TextureImporterFormat.ASTC_8x8);
}
public static bool ConfigureUiAtlasSource(TextureImporter importer)
{
if (!UiAtlasPaths.IsTexturePath(importer.assetPath)) return false;
// 源图使用无损 RGBA32,平台覆盖关闭;最终压缩由 SpriteAtlas 自己处理。
bool changed = SetUncompressedSource(importer, "DefaultTexturePlatform");
changed |= SetUncompressedSource(importer, "Standalone");
changed |= SetUncompressedSource(importer, "iPhone");
changed |= SetUncompressedSource(importer, "Android");
changed |= SetUncompressedSource(importer, "WebGL");
return changed;
}
private static bool SetUncompressedSource(TextureImporter importer, string platform)
{
var settings = importer.GetPlatformTextureSettings(platform);
if (!settings.overridden && settings.format == TextureImporterFormat.RGBA32
&& settings.textureCompression == TextureImporterCompression.Uncompressed
&& !settings.crunchedCompression) return false;
settings.name = platform;
settings.overridden = false;
settings.format = TextureImporterFormat.RGBA32;
settings.textureCompression = TextureImporterCompression.Uncompressed;
settings.crunchedCompression = false;
importer.SetPlatformTextureSettings(settings);
return true;
}
private static void SetPlatformFormat(
TextureImporter importer, string platform, TextureImporterFormat format)
{
// 保留尺寸、缩放算法等原有设置,仅覆盖压缩相关选项。
var settings = importer.GetPlatformTextureSettings(platform);
settings.name = platform;
settings.overridden = true;
settings.format = format;
settings.crunchedCompression = false;
importer.SetPlatformTextureSettings(settings);
}
}
}
#endif