批量工具-设置图片属性

使用UGUI开发,导入图片时,总是要设置图片的属性(tag,spriteMode,alpha….)很是机械繁琐操作,OnPreprocessTexture()可自动;SetImgProperty()可手动

public class OutPullImgAutoSetting : AssetPostprocessor
{
    /// 
    /// 导入时自动设置属性--1.copy到文件夹下都会自动设置的  
    ///2.拖入到u3d中也会自动设置---,此方法有点无赖,最好使用时才打开,因为有些图片要其他格式也会设置成这格式
    /// 
    void OnPreprocessTexture()
    {
        /*
    //自动设置类型;  
        TextureImporter imgPNG = (TextureImporter)assetImporter;
        imgPNG.textureType = TextureImporterType.Sprite;
        imgPNG.spriteImportMode = SpriteImportMode.Single;

        string dirName = System.IO.Path.GetDirectoryName(assetPath);
        imgPNG.spritePackingTag = System.IO.Path.GetFileName(dirName);        //自动设置打包tag;  
        imgPNG.sRGBTexture = true;
        imgPNG.alphaIsTransparency = true;
        imgPNG.mipmapEnabled = false;
        imgPNG.wrapMode = TextureWrapMode.Clamp;
        imgPNG.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.RGBA32);
        imgPNG.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8);
        //   Debug.Log("Set Packing Tag 导入图片自动设置图片属性---  " + folderStr + "  " + dirName + "  " + dirName);
    */
    }

    /// 
    /// 手动批量设置图片属性 project里的目录--可见下面的扩展
    /// 
    [MenuItem("SpritesPacker/_UI批量改属性-目录自己手动改下路径--别乱点")]
    public static void SetImgProperty()
    {
        return;//用时再打开
        DirectoryInfo uiStickers = new DirectoryInfo(Application.dataPath + "/Art/UI/Stickers");
        foreach (DirectoryInfo item in uiStickers.GetDirectories())//记住 uiStickers目录下 两级目录
        {
            foreach (FileInfo pngItem in item.GetFiles("*.png", SearchOption.AllDirectories))
            {
                string allPath = pngItem.FullName;
                string temp_assetPath = allPath.Substring(allPath.IndexOf("Assets"));
                TextureImporter imgPNG = AssetImporter.GetAtPath(temp_assetPath) as TextureImporter;
                imgPNG.textureType = TextureImporterType.Sprite;
                imgPNG.spriteImportMode = SpriteImportMode.Single;
                //自动设置打包tag; 
                string dirName = Path.GetDirectoryName(allPath);
                imgPNG.spritePackingTag = Path.GetFileName(dirName);

                imgPNG.sRGBTexture = true;
                imgPNG.alphaIsTransparency = true;
                imgPNG.mipmapEnabled = false;
                imgPNG.wrapMode = TextureWrapMode.Clamp;
                imgPNG.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.RGBA32);
                imgPNG.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8);
            }
        }
    }
}

扩展:有可n个文件夹,遍历下,可操作性强点
批量工具-设置图片属性_第1张图片

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
public class ImgSettingShow : EditorWindow
{
    [MenuItem("SpritePacker/打开图片设置框")]
    public static void OpenSetImgDialog()
    {
        GetConfig();
        EditorWindow.GetWindow(typeof(ImgSettingShow), false, "设置图片属性");
    }
    private static string _imgPath = "Art/Temp";
    private static string _imgTagName="zengsixin";
    public static List _floderLists;
    void OnGUI()
    {
        _imgPath = EditorGUILayout.TextField("图片路径", _imgPath);
        GUILayout.Space(10);
        _imgTagName = EditorGUILayout.TextField("图片Tag(若空则文件夹名)", _imgTagName);
        GUILayout.Space(20);
        if (GUILayout.Button("确定设置", GUILayout.Width(150)))
        {
            Debug.Log(Application.dataPath + "/" + _imgPath);
            SearchFiles(_imgPath);
            SetConfig(_imgPath);
        }
    }

    public static void SearchFiles(string imgPath)
    {
        DirectoryInfo inputPathAll = new DirectoryInfo(Application.dataPath + "/" + imgPath);
        _floderLists = new List();
        GetFloderList(inputPathAll);
        for (int a = 0; a < _floderLists.Count; a++)
        {
            foreach (var pngInfo in _floderLists[a].GetFiles("*.png", SearchOption.AllDirectories))
            {
                SetImgProperty(pngInfo);
            }
        }
    }
    ///  所有文件夹名 
    public static void GetFloderList(DirectoryInfo info)
    {
        if (info == null) return;  //不是目录 
        var floders = info.GetDirectories();
        for (int i = 0; i < floders.Length; i++)
        {
            var floder = floders[i];
            if (floder.GetDirectories().Length == 0)//是文件夹
            {
                if (_floderLists.Contains(floder) == false)
                    _floderLists.Add(floder);
            }
            else
            {//对于还有子目录,进行递归调用 
                if (_floderLists.Contains(floder) == false)
                    _floderLists.Add(floder);
                GetFloderList(floders[i]);
            }
        }
    }
    ///  设置图片属性 
    private static void SetImgProperty(FileInfo pngInfo)
    {
        string allPath = pngInfo.FullName;
        string temp_assetPath = allPath.Substring(allPath.IndexOf("Assets"));
        TextureImporter imgPNG = AssetImporter.GetAtPath(temp_assetPath) as TextureImporter;
        imgPNG.textureType = TextureImporterType.Sprite;
        imgPNG.spriteImportMode = SpriteImportMode.Single;
        //自动设置打包tag; 
        imgPNG.spritePackingTag = string.IsNullOrEmpty(_imgTagName) ? Path.GetFileName(Path.GetDirectoryName(allPath)) : _imgTagName;//tag
        imgPNG.sRGBTexture = true;
        imgPNG.alphaIsTransparency = true;
        imgPNG.mipmapEnabled = false;
        imgPNG.wrapMode = TextureWrapMode.Clamp;
        imgPNG.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.RGBA32);
        imgPNG.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8);
    }

    public static void GetConfig()
    {
        if (PlayerPrefs.HasKey("_imgPath"))
        {
            _imgPath = PlayerPrefs.GetString("_imgPath");
        }
    }
    public static void SetConfig(string imgPath)
    {
        PlayerPrefs.SetString("_imgPath", imgPath);
    }
}

你可能感兴趣的:(批量工具协助)