using UnityEditor;
using UnityEngine;

public class TextureProcess : AssetPostprocessor
{
    private void OnPreprocessTexture()
    {
        TextureImporter textureImporter = assetImporter as TextureImporter;
        if (textureImporter == null)
            return;
        
        //缩略图处理
        if (textureImporter.assetPath.StartsWith("Assets/AssetRaw/UIRaw/Raw/Thum"))
        {
            textureImporter.maxTextureSize = 512;
            textureImporter.textureType = TextureImporterType.Sprite;
            textureImporter.textureShape = TextureImporterShape.Texture2D;
            textureImporter.alphaIsTransparency = false;
            textureImporter.isReadable = false;
            textureImporter.mipmapEnabled = false;
            textureImporter.wrapMode = TextureWrapMode.Clamp;
            textureImporter.filterMode = FilterMode.Bilinear;
            textureImporter.SetPlatformTextureSettings(GetAndroidTextureSettings(TextureImporterFormat.ASTC_6x6, 512));
            textureImporter.SetPlatformTextureSettings(GetiOSTextureSettings(TextureImporterFormat.ASTC_6x6, 512));
        }
        //关卡图处理
        else if (textureImporter.assetPath.StartsWith("Assets/AssetRaw/UIRaw/Raw/Level"))
        {
            if (textureImporter.name.Contains("_base"))
            {
                textureImporter.textureType = TextureImporterType.Sprite;
                textureImporter.textureShape = TextureImporterShape.Texture2D;
                textureImporter.alphaIsTransparency = false;
                textureImporter.isReadable = false;
                textureImporter.mipmapEnabled = false;
                textureImporter.wrapMode = TextureWrapMode.Clamp;
                textureImporter.filterMode = FilterMode.Bilinear;
                textureImporter.SetPlatformTextureSettings(GetAndroidTextureSettings(TextureImporterFormat.ASTC_6x6,
                    4096));
                textureImporter.SetPlatformTextureSettings(GetiOSTextureSettings(TextureImporterFormat.ASTC_6x6));
            }
            else
            {
                textureImporter.textureType = TextureImporterType.Sprite;
                textureImporter.textureShape = TextureImporterShape.Texture2D;
                textureImporter.alphaIsTransparency = true;
                textureImporter.isReadable = false;
                textureImporter.mipmapEnabled = false;
                textureImporter.wrapMode = TextureWrapMode.Clamp;
                textureImporter.filterMode = FilterMode.Bilinear;
                textureImporter.SetPlatformTextureSettings(GetAndroidTextureSettings(TextureImporterFormat.ASTC_6x6));
                textureImporter.SetPlatformTextureSettings(GetiOSTextureSettings(TextureImporterFormat.ASTC_6x6));
            }
        }
    }
    
    private TextureImporterPlatformSettings GetAndroidTextureSettings(TextureImporterFormat format, int size = 2048)
    {
        TextureImporterPlatformSettings textureImporter = new TextureImporterPlatformSettings();
        textureImporter.name = "Android";
        textureImporter.maxTextureSize = size;
        textureImporter.format = format;
        textureImporter.overridden = true;
        textureImporter.textureCompression = TextureImporterCompression.Compressed;
        return textureImporter;
    }
    
    private TextureImporterPlatformSettings GetiOSTextureSettings(TextureImporterFormat format, int size = 2048)
    {
        TextureImporterPlatformSettings textureImporter = new TextureImporterPlatformSettings();
        textureImporter.name = "iPhone";
        textureImporter.maxTextureSize = size;
        textureImporter.format = format;
        textureImporter.overridden = true;
        textureImporter.textureCompression = TextureImporterCompression.Compressed;
        return textureImporter;
    }
}
