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

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获取的属性了。

相关推荐
申行7 小时前
Vue 3 + DYMO Connect Framework 实战:从标签模板到打印服务的完整实现
前端
两只羊ovo7 小时前
Vue 3 + DeepSeek 流式输出实战:从“干等”到“打字机”体验
前端·vue.js
Yeyu7 小时前
车载多 App 同屏渲染(二):SurfaceControlViewHost 跨进程
前端
樊小肆7 小时前
离谱,每轮请求 25% 的 token,竟在重发模型想完就扔的内心独白
前端·人工智能·agent
小妖现世7 小时前
前端面试复习笔记:React 高频题 + JS 手写题
前端
holidaypenguin7 小时前
Windows 本地 HTTPS 证书生成指南
前端
酷酷的逗逗乐7 小时前
前端转 Agent 开发 · 第六节
前端·程序员
回家吃饭去吧7 小时前
WebAssembly 深度解析:前端性能的最后一块拼图
前端
爱勇宝7 小时前
没有 Fn 键关触控板?我做了一个双击即用的 Windows 小工具
前端·后端·程序员
尤小小8 小时前
Vite-SSG 实践:Vue项目预渲染落地完整方案
前端·vue.js