批量修改图片资源的属性。

Unity版本2022.3

如图,比如我们想要修改图片的属性的时候,大部分都是

csharp 复制代码
TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath("Assets/1.png"); // 获取文件
importer.xxxxxxx = xxxxxxx; // 修改属性

到这里没什么问题,但是,如果现在我需要修改 sprite 轴心等相关数据的时候。

一般都需要我点开SpriteEditor。然后去手动设置轴心,锚点位置等等。最后再去点击那个应用,然后还要确认一次才能保存对应设置,那现在我有几千张图需要这么处理该怎么办呢,不可以一张一张手动设置吧。当然可以美工直接出对应图直接匹配默认轴心锚点。但如果你只有一个人, 没有这个条件呢?

好了,现在开始操作

csharp 复制代码
首先获取你的所有图片资源。
然后你可以拿到importer
	TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath("Assets/1.png"); 


//修改图片资源的对应属性
    static void ModifySprite(TextureImporter importer)
    {
        if (importer != null)
        {

            importer.textureType =TextureImporterType.Sprite;
            TextureImporterSettings settings = new TextureImporterSettings();
            importer.ReadTextureSettings(settings);
            settings.spriteMode = (int)SpriteImportMode.Single;
            settings.spriteAlignment = (int)SpriteAlignment.Custom; // 轴心模式改成自定义
            settings.spritePivot = new Vector2(0.5f, 0.3333333f);
            settings.spritePixelsPerUnit = 100;
            settings.spriteBorder = new Vector4(0, 0, 0, 0);
            settings.spriteMeshType = SpriteMeshType.Tight;
            settings.spriteExtrude = 1;
            importer.SetTextureSettings(settings);
            importer.SaveAndReimport();
            Debug.Log("修改完成:" + importer.assetPath);
        }
    }

以上,就可以直接修改那些我们不能通过importer获取的属性了。

相关推荐
Flynt5 小时前
AI 写的 Three.js 大屏,为什么越跑越卡?我在 Node 里复现了它的内存曲线
前端·ai编程·three.js
一个游离的指针7 小时前
Vue2:vue-router
前端·javascript·vue.js
火山引擎和TA的超级拍档们9 小时前
产品情报局 11 | AI 数据湖 LAS:打造大模型时代多模态数据底座
前端·后端
JavaGuide9 小时前
Gemini 真的变强了,写代码做视频的能力接近 Claude Opus5.5 了!
前端·ai编程
niceyayale10 小时前
WebRTC 延迟怎么调试:从 ICE 到 jitter buffer 的六个观测点
前端
aleimu10 小时前
Windows 11 右键打开 Cmder 2.0:从找不到菜单到默认使用 Cmder
前端·人工智能·后端
tryCbest10 小时前
Vue系列——第 20 篇:Vue Router 4——路由管理完全指南(上)
前端·javascript·vue.js
feixing_fx10 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架
一条破秋裤10 小时前
【1115】【多线程】【笔记】
前端·chrome·笔记
黑十10 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能