资源工作流之自动设置图片格式

此工具的作用:
1.可以帮助策划or美术导入图片自动设置为Sprite格式
2.并且设置默认的压缩方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SpriteImportEditor : AssetPostprocessor
{
    public override uint GetVersion()
    {
        return 1;
    }

    const string SYMBOL_ALTAS = "Altas";
    const string SYMBOL_Normal = "Normal";

    const string SYMBOL_ANDROID = "Android";
    const string SYMBOL_IPHONE = "iPhone";

    const string FORDER_RGB32 = "rgb32";
    const string FORDER_SHOP = "shop";
    const string FORDER_LOADING = "loading";
    const string FORDER_ZDW = "zdw";

    const string NAME_DM = "_dm_0";
    const string NAME_CASE_MAP = "case_map_";

    const string DIR_UI = "Assets/GameRes/UI/Sprites/";
    const string DIR_MAP = "Assets/GameRes/Maps/Sprites/";
    const string DIR_EFFECT = "Assets/X_Demo";

    static List<string> specia_1024_List = new List<string>()
    {
        "cs_dm_bg01"
    };

    const string DIR_MODEL = "Assets/GameRes/Models/";
    static List<string> playerFolder_List = new List<string>()
    {
        "girl",
        "mc",
        "mc2",
        "mc3",
        "mc4"
    };

    public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        if (importedAsset.Length == 0)
            return;

        foreach (string assetPath in importedAsset)
        {
            if (assetPath.EndsWith(".dds"))
            {
                EditorUtility.DisplayDialog("异常格式", "有文件:[" + assetPath + "]是dds格式的,不支持!", "知道了哥");
            }
        }
    }

    private void OnPreprocessTexture()
    {
        TextureImporter importer = (TextureImporter)assetImporter;
        if (importer.userData == this.GetVersion().ToString())
            return;

        string assetPath = importer.assetPath;

        //模型的贴图
        bool isInModels = assetPath.StartsWith(DIR_MODEL);
        if (isInModels)
        {
            foreach (var item in playerFolder_List)
            {
                if (assetPath.Contains(item))
                    return;
            }

            AndroidSet(importer, TextureImporterFormat.ETC2_RGB4, 256);
            iPhoneSet(importer, TextureImporterFormat.ETC2_RGB4, 256);

            importer.userData = this.GetVersion().ToString();
            importer.SaveAndReimport();
            EditorUtility.SetDirty(importer);
            return;
        }

        //普通的图
        bool isInNormal = assetPath.Contains(SYMBOL_Normal);
        if (isInNormal)
        {
            NormalSet(importer);
            Debug.LogFormat("自动帮您导入图片 纠正格式 : {0}", assetPath);

            importer.userData = this.GetVersion().ToString();
            importer.SaveAndReimport();
            EditorUtility.SetDirty(importer);
            return;
        }

        //图集的图
        bool isInAltas = assetPath.Contains(SYMBOL_ALTAS);
        if (isInAltas) //不是图集的,先不管,可能需要定制参数
        {
            bool bInUIDir = assetPath.StartsWith(DIR_UI);
            bool bInMap = assetPath.StartsWith(DIR_MAP);
            if (bInUIDir || bInMap)
            {
                OnPreprocess_Internal(importer);
            }
            Debug.LogFormat("自动帮您导入图片 纠正格式 : {0}", assetPath);

            importer.userData = this.GetVersion().ToString();
            importer.SaveAndReimport();
            EditorUtility.SetDirty(importer);
            return;
        }

        //特效的图
        bool isInEffect = assetPath.Contains(DIR_EFFECT);
        if (isInEffect)
        {
            GetAlphaSetAdapter(importer, out int maxSize, out TextureImporterFormat format);
            AndroidSet(importer, format, maxSize);
            iPhoneSet(importer, format, maxSize);
            Debug.LogFormat("自动帮您导入图片 纠正格式 : {0}", assetPath);

            importer.userData = this.GetVersion().ToString();
            importer.SaveAndReimport();
            EditorUtility.SetDirty(importer);
            return;
        }
    }

    private void OnPreprocess_Internal(TextureImporter importer)
    {
        NormalSet(importer);
        GetAlphaSet(importer, out int maxSize, out TextureImporterFormat format);
        AndroidSet(importer, format, maxSize);
        iPhoneSet(importer, format, maxSize);
    }

    // ================================================================================================ //

    void NormalSet(TextureImporter importer)
    {
        importer.isReadable = false;
        importer.textureType = TextureImporterType.Sprite;
    }
    bool NeedSetSize128()
    {
        bool bInZdw = assetPath.Contains(FORDER_ZDW);
        return bInZdw;
    }
    bool NeedSetSize1024()
    {
        bool bRGB32 = assetPath.Contains(FORDER_RGB32);
        bool bInShop = assetPath.Contains(FORDER_SHOP);//商城的NB一点
        bool bInLoading = assetPath.Contains(FORDER_LOADING);
        bool bDoor = assetPath.Contains(NAME_DM);//大门需要单独设置
        bool bCaseMap = assetPath.Contains(NAME_CASE_MAP);//大门需要单独设置
        bool bSpecial = assetPath.Contains(specia_1024_List[0]);//特殊列表,先这样吧 TODO
        return bRGB32 || bInShop || bInLoading || bDoor || bCaseMap || bSpecial;
    }

    void GetAlphaSet(TextureImporter importer, out int maxSize, out TextureImporterFormat format)
    {
        if (importer.DoesSourceTextureHaveAlpha())
        {
            format = TextureImporterFormat.RGBA32;
            maxSize = 512;
            if (NeedSetSize1024())
            {
                maxSize = 1024;
            }
            else
            {
                if (NeedSetSize128())
                {
                    maxSize = 128;
                }
            }
        }
        else
        {
            format = TextureImporterFormat.RGB24;
            maxSize = 512;
            if (NeedSetSize1024())
            {
                maxSize = 1024;
            }
        }
    }
    void GetAlphaSetAdapter(TextureImporter importer, out int maxSize, out TextureImporterFormat format)
    {
        if (importer.DoesSourceTextureHaveAlpha())
        {
            format = TextureImporterFormat.ETC2_RGBA8;
            maxSize = 256;
        }
        else
        {
            format = TextureImporterFormat.ETC_RGB4;
            maxSize = 256;
        }
    }

    void AndroidSet(TextureImporter importer, TextureImporterFormat format, int maxSize = 512)
    {
        importer.GetPlatformTextureSettings(SYMBOL_ANDROID, out _, out _);
        TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings
        {
            name = SYMBOL_ANDROID,
            format = format,
            maxTextureSize = maxSize,
            crunchedCompression = true,
            overridden = true
        };
        importer.SetPlatformTextureSettings(settings);
    }

    void iPhoneSet(TextureImporter importer, TextureImporterFormat format, int maxSize = 512)
    {
        importer.GetPlatformTextureSettings(SYMBOL_ANDROID, out _, out _);
        TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings
        {
            name = SYMBOL_IPHONE,
            format = format,
            maxTextureSize = maxSize,
            crunchedCompression = true,
            overridden = true
        };
        importer.SetPlatformTextureSettings(settings);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值