美文网首页
资源工作流之自动设置图片格式

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

作者: APP4x | 来源:发表于2020-09-09 09:50 被阅读0次

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

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    
    public class SpriteImportEditor : AssetPostprocessor
    {
        const string SYMBOL_ALTAS = "Altas";
    
        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_DOOR = "_dm_0";
    
        const string DIR_UI = "Assets/GameRes/UI/Sprites/";
        const string DIR_MAP = "Assets/GameRes/Maps/Sprites/";
    
    
        private void OnPreprocessTexture()
        {
            TextureImporter importer = (TextureImporter)assetImporter;
            string assetPath = importer.assetPath;
            bool isInAltas = assetPath.Contains(SYMBOL_ALTAS);
            if (!isInAltas) //不是图集的,先不管,可能需要定制参数
            {
                return;
            }
            Debug.LogFormat("自动帮您导入图片 纠正格式 : {0}", assetPath);
            bool bInUIDir = assetPath.StartsWith(DIR_UI);
            bool bInMap = assetPath.StartsWith(DIR_MAP);
            if (bInUIDir || bInMap)
            {
                OnPreprocess_Internal(importer);
            }
        }
    
        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 NeedSetRGBA32_Size1024()
        {
            bool bRGB32 = assetPath.Contains(FORDER_RGB32);
            bool bInShop = assetPath.Contains(FORDER_SHOP);//商城的NB一点
            bool bDoor = assetPath.Contains(NAME_DOOR);//美术说:大门需要高清
            return bRGB32 || bInShop || bDoor;
        }
        bool NeedSetSize128()
        {
            bool bInZdw = assetPath.Contains(FORDER_ZDW);
            return bInZdw;
        }
        bool NeedSetSize1024()
        {
            bool bInLoading = assetPath.Contains(FORDER_LOADING);
            return bInLoading;
        }
    
        void GetAlphaSet(TextureImporter importer, out int maxSize, out TextureImporterFormat format)
        {
            if (importer.DoesSourceTextureHaveAlpha())
            {
                if (NeedSetRGBA32_Size1024())
                {
                    format = TextureImporterFormat.RGBA32;
                    maxSize = 1024;
                }
                else
                {
                    format = TextureImporterFormat.ETC2_RGBA8;
                    maxSize = 512;
                    if (NeedSetSize128())
                    {
                        maxSize = 128;
                    }
                    if (NeedSetSize1024())
                    {
                        maxSize = 1024;
                    }
                }
            }
            else
            {
                format = TextureImporterFormat.ETC2_RGB4;
                maxSize = 512;
            }
        }
    
        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);
        }
    }
    

    相关文章

      网友评论

          本文标题:资源工作流之自动设置图片格式

          本文链接:https://www.haomeiwen.com/subject/brpkektx.html