[unity]批量处理图片资源

来源:互联网 发布:知乎 匿名用户 编辑:程序博客网 时间:2024/05/30 02:24

原理比较简单, 原理比较简单textureImporter,直接上代码 效果为


点确定就可以

using System.Collections.Generic;using System.Linq;using UnityEngine;using UnityEditor;using System.IO;using UnityEngine.EventSystems;public class TextureImportPlatFormInfo{    public string Name;        public TextureImporterFormat TextureFormat;    public TextureImporterFormat HasAlphaChannelFormat;    public int MaxSize;    public bool isSetToggled;}public class TextureImportSetter : EditorWindow{    private string modelRootDir = Application.dataPath;    public static TextureImporterFormat format = TextureImporterFormat.ETC2_RGB4;    private TextureImportPlatFormInfo androidFormatInfo = new TextureImportPlatFormInfo()    {        MaxSize = 512,        Name = "Android",        TextureFormat = TextureImporterFormat.ETC2_RGB4,        HasAlphaChannelFormat = TextureImporterFormat.ETC2_RGBA8    };    /// <summary>    /// ios导入格式    /// </summary>    private TextureImportPlatFormInfo IosFormatInfo =        new TextureImportPlatFormInfo()        {            MaxSize = 512,            Name = "iPhone",            TextureFormat = TextureImporterFormat.DXT5,            HasAlphaChannelFormat = TextureImporterFormat.DXT5        };    private string searchFilters = "*.tga";    private int[] formatSize = new int[]{32,64,128,256,512,1024,2048,4096};    private string[] formatDes = new[] { "32", "64",  "128", "256", "512", "1024", "2048", "4096" };    /// <summary>    /// 当前选中平台编号    /// </summary>    private int curSelectPlatformIndex = 0 ;    private bool isSelectAndroid    {        get { return curSelectPlatformIndex == 0; }    }    private bool isSelectIos    {        get { return curSelectPlatformIndex == 1; }    }    [MenuItem("Custom/SetTextureFormat")]    public static void Init()    {        EditorWindow.GetWindow<TextureImportSetter>();    }    public void OnGUI()    {        EditorGUILayout.BeginVertical();        if (GUILayout.Button("选择资源文件夹"))        {            modelRootDir = EditorUtility.OpenFolderPanel("模型根目录", modelRootDir, modelRootDir);        }        EditorGUILayout.LabelField("资源文件夹 : " + modelRootDir);        GUILayout.BeginHorizontal();        if (GUILayout.Toggle(isSelectAndroid, androidFormatInfo.Name ))        {            curSelectPlatformIndex = 0;        }        if (GUILayout.Toggle(isSelectIos, IosFormatInfo.Name))        {            curSelectPlatformIndex = 1;        }        GUILayout.EndHorizontal();        if (isSelectAndroid)        {            DrawItemUI(androidFormatInfo);        }        if (isSelectIos)        {            DrawItemUI(IosFormatInfo);        }        if(GUILayout.Button("确定"))        {            SetFormat();        }        EditorGUILayout.EndVertical();    }    private void DrawItemUI(TextureImportPlatFormInfo platFormInfo)    {        platFormInfo.MaxSize = EditorGUILayout.IntPopup("图片大小", platFormInfo.MaxSize, formatDes, formatSize);        platFormInfo.TextureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("选择格式(不含alpht通道的格式)", platFormInfo.TextureFormat);        platFormInfo.HasAlphaChannelFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("选择格式(含有alpht通道的格式)", platFormInfo.HasAlphaChannelFormat);        platFormInfo.isSetToggled = EditorGUILayout.Toggle("选中该平台", platFormInfo.isSetToggled);    }    private void SetFormat()    {        if (!androidFormatInfo.isSetToggled && !IosFormatInfo.isSetToggled)        {            Debug.LogError("无设置打包平台");            return;        }        var paths = Directory.GetFiles(modelRootDir,"*.*", SearchOption.AllDirectories).Where( s =>s.EndsWith(".png")|| s.EndsWith(".tga")|| s.EndsWith(".jpg") ) .ToArray();        float progress = 0;        for (int i = 0;i<paths.Length;i++)        {            string absultepath = paths[i].Replace(Application.dataPath, "Assets");            progress =((float)i) /paths.Length;            EditorUtility.DisplayProgressBar("修改图片中..", absultepath, progress);            TextureImporter import = AssetImporter.GetAtPath(absultepath) as TextureImporter;                    if(import == null)continue;            Texture2D textureFile = AssetDatabase.LoadAssetAtPath(absultepath, typeof(Texture2D)) as Texture2D;            Debug.LogError(textureFile.width);            import.textureType = TextureImporterType.Advanced;            bool isHaveAlphaChannel = import.DoesSourceTextureHaveAlpha();            if (androidFormatInfo.isSetToggled)            {                import.SetPlatformTextureSettings(androidFormatInfo.Name, androidFormatInfo.MaxSize,                    isHaveAlphaChannel ?                     androidFormatInfo.HasAlphaChannelFormat : androidFormatInfo.TextureFormat);            }            if (IosFormatInfo.isSetToggled)            {                import.SetPlatformTextureSettings(IosFormatInfo.Name, IosFormatInfo.MaxSize,                     isHaveAlphaChannel ?                     IosFormatInfo.HasAlphaChannelFormat : IosFormatInfo.TextureFormat);            }            import.anisoLevel = 1;            AssetDatabase.ImportAsset(absultepath);        }        EditorUtility.ClearProgressBar();        EditorUtility.DisplayDialog("设置完毕", "设置完毕", "OK");    }    public void OnEnable()    {        this.modelRootDir = Application.dataPath;        this.title = "模型贴图格式设置";    }}



0 0
原创粉丝点击