首页 > 其他分享 >Unity编辑器批量设置图片格式

Unity编辑器批量设置图片格式

时间:2024-08-14 10:49:43浏览次数:8  
标签:TextureImporterFormat textureImporter 图片格式 Unity 编辑器 setting 设置 android fileInfo

在游戏开发中,经常需要批量设置图片的格式为Sprite类型,手动设置太麻烦,下面的编辑器脚本实现选中文件夹右键/Texture/SetAllImagesToSpriteType实现批量设置图片格式,具体格式参数可自行定义

using System;
using System.IO;
using UnityEngine;
using UnityEditor;

/// <summary>
/// 资源重设置导入刷新
/// </summary>
public class AssetProcessor : AssetPostprocessor
{
    [MenuItem("Assets/Texture/SetAllImagesToSpriteType")]
    public static void SetAllTextureType()
    {
        //获取鼠标点击图片目录
        var arr = Selection.GetFiltered(typeof(DefaultAsset), SelectionMode.Assets);
        string folder = AssetDatabase.GetAssetPath(arr[0]);
        Debug.Log("SetAllImagesToSpriteType Path:" + folder);

        //遍历文件夹获取所有.png/.jpg
        DirectoryInfo direction = new DirectoryInfo(folder);
        FileInfo[] pngFiles = direction.GetFiles("*.png", SearchOption.AllDirectories);
        FileInfo[] jpgfiles = direction.GetFiles("*.jpg", SearchOption.AllDirectories);

        try
        {
            SetTexture(pngFiles);
            SetTexture(jpgfiles);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
        finally
        {
            EditorUtility.ClearProgressBar();
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

    static void SetTexture(FileInfo[] fileInfo)
    {
        for (int i = 0; i < fileInfo.Length; i++)
        {
            //这里第一次写时有一个接口可直接调用,但是第二次写时找不到了 所以用了切割字符
            string fullpath = fileInfo[i].FullName.Replace("\\", "/");
            string path = fullpath.Replace(Application.dataPath, "Assets");
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            EditorUtility.DisplayProgressBar("批量处理图片", $"{i}/{fileInfo.Length}  \n{fileInfo[i].Name} ", i / (float)fileInfo.Length);
            SetTextureFormat(textureImporter);
            //触发重新导入资源才生效,否则需要重启Unity触发自动导入
            AssetDatabase.ImportAsset(path);
        }
    }

    //设置图片格式
    static void SetTextureFormat(TextureImporter textureImporter)
    {
        //根据路径获得文件夹目录,设置图集的packagingTag
        string AtlasName = new DirectoryInfo(Path.GetDirectoryName(textureImporter.assetPath)).Name;
        textureImporter.mipmapEnabled = false;
        textureImporter.isReadable = false;
        textureImporter.textureType = TextureImporterType.Sprite;
        textureImporter.spritePackingTag = AtlasName;
        textureImporter.wrapMode = TextureWrapMode.Clamp;
        textureImporter.npotScale = TextureImporterNPOTScale.None;
        textureImporter.textureFormat = TextureImporterFormat.Automatic;
        textureImporter.textureCompression = TextureImporterCompression.Compressed;

        //Android端单独设置
        TextureImporterPlatformSettings setting_android = new TextureImporterPlatformSettings();
        setting_android.overridden = true;
        setting_android.name = "Android";
        //根据是否有透明度,选择RGBA还是RGB
        if (textureImporter.DoesSourceTextureHaveAlpha())
            setting_android.format = TextureImporterFormat.ETC2_RGBA8;
        else
            setting_android.format = TextureImporterFormat.ETC2_RGB4;

        textureImporter.SetPlatformTextureSettings(setting_android);

        //IOS端单独设置
        TextureImporterPlatformSettings setting_iphone = new TextureImporterPlatformSettings();
        setting_iphone.overridden = true;
        setting_iphone.name = "iOS";
        //根据是否有透明度,选择RGBA还是RGB
        if (textureImporter.DoesSourceTextureHaveAlpha())
            setting_android.format = TextureImporterFormat.ASTC_6x6;
        else
            setting_android.format = TextureImporterFormat.ASTC_6x6;
        textureImporter.SetPlatformTextureSettings(setting_iphone);
    }
}

 

标签:TextureImporterFormat,textureImporter,图片格式,Unity,编辑器,setting,设置,android,fileInfo
From: https://www.cnblogs.com/flamesky/p/18358436

相关文章

  • unity中, 二维平面上,求从点A出发,沿着方向B,与线段C的交点
    代码说明:点A:起始点。方向B:一个方向向量,表示从点A出发的方向。线段C:由两个点C1和C2定义。1usingUnityEngine;23publicclassLineIntersection:MonoBehaviour4{5//返回从点A出发,沿着方向B,与线段C的交点。如果没有交点,则返回null6publicstati......
  • Unity中利用遗传算法训练MLP
    Unity中利用遗传算法训练MLP梯度下降法训练神经网络通常需要我们给定训练的输入-输出数据,而用遗传算法会便捷很多,它不需要我们给定好数据,只需要随机化多个权重进行N次“繁衍进化”,就可以得出效果不错的网络。这种训练方式的好处就是不需要训练用的预期输出数据,适合那类可以简单......
  • 在Unity中开发MQTT客户端
    概述:        在Unity环境中使用MQTTnet库(一个流行的.NET库,用于实现MQTT客户端和服务器。它支持.NETCore和.NETFramework,并提供了灵活的API以及高性能的实现)搭建自己的MQTT客户端.我使用的版本:Version=4.3.6.1152        但是在开发客户端之前,你需要......
  • 【Unity精品插件】Odin Inspector and Serializer:提升开发效率的利器
    概述OdinInspectorandSerializer是UnityAssetStore上的一款备受好评的工具,专为Unity开发者设计,以提供更强大的属性检查器和序列化功能。自推出以来,它已经获得了超过11,000个五星级评价,并且被85,000多名客户所认可。功能特点1.轻松集成:Odin很容易操作,并且不会打......
  • unity2022.3.9+Pico更换渲染管线后打包,人物材质不可显示问题
    为了解决字体和场景闪烁问题吗,更换渲染管线旧项目管线是URP 新的项目管线是内置管线buildin()  内置管线需要设置两个地方,可以解决人物材质不显示问题1.PICO-StereoRenderingMode选择 MultiPass模式 2,Player-OtherSetting-AutoGraphicsAPI勾选(注:项目中有......
  • 破防了!加班狗常用的TOP4 PDF编辑器2024年集锦
    我们天天都得跟一堆文件打交道,尤其是PDF文件。PDF的好处是,不管在哪个设备上打开,内容和格式都保持原样,而且不容易被改。不过,有时候这也让人挺头疼的,因为想改点什么就麻烦了。幸好,技术一直在进步,现在市面上有很多好用的PDF编辑器,它们就像魔法棒,让编辑文档变得轻松又快速。今天,我......
  • pbootcms去除ueditor编辑器图片自动添加的title和alt属性
    pbootcms后台使用的是百度ueditor编辑器,ueditor上传图片会自动添加title、alt属性,属性值为源图的文件名,pbootcms模板中title为图片上传后的一串日期数字名称,从SEO和用户体验角度来说都不好。我们如果想去掉这个属性要怎么操作呢?接下来准备改造成默认图片上传后只带alt="",一个空al......
  • 了解VSCode:一款功能强大的开源代码编辑器
    VisualStudioCode(简称VSCode)是由微软开发的一款免费、开源的源代码编辑器。它以其强大的功能、丰富的插件生态系统、跨平台兼容性以及出色的用户体验,成为了广大开发者的首选工具。以下是对VSCode的详细介绍,涵盖其特点、功能、安装与配置、以及扩展生态等方面。一、VSCode的......
  • DzzOffice 编辑器篇
    百度编辑器工具栏模式文件:dzz\system\ueditor\ueditor.config.js代码UEDITOR_CONFIG.mode={};内的表示不同模式显示不同的工具,其中full段表示完整的工具。工具代码'anchor':'锚点','undo':'撤销','redo':'重做','bold':'加粗',&......
  • PDF编辑不求人!这三款免费版编辑器助你轻松搞定!
    作为一名办公室文员,每天和PDF文件打交道那是家常便饭。打印合同、整理报告、编辑资料,PDF文件简直就是我的工作小伙伴。不过,说起编辑PDF,那可真是个技术活。以前,我总是为这事儿头疼,直到遇见了几款pdf编辑器免费版,简直是让我大开眼界,工作效率也噌噌往上涨!1.福昕PDF编辑器网址:ht......