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

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

相关推荐
张老师带你学11 小时前
unity作业,街角小场景
科技·游戏·unity·游戏引擎·模型
~无忧花开~12 小时前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
cj814012 小时前
Prompt,Agent,Skill,Mcp分别于langchain有什么关系
前端
SuperEugene12 小时前
Axios + Vue 错误处理规范:中后台项目实战,统一捕获系统 / 业务 / 接口异常|API 与异步请求规范篇
前端·javascript·vue.js·前端框架·axios
行走的陀螺仪12 小时前
手写 Vue3 极简 i18n
前端·javascript·vue.js·国际化·i18n
羽沢3113 小时前
一篇简单的STOMP教程QAQ
前端·javascript·stomp
code_Bo13 小时前
使用AI完成Swagger接口类型在前端自动生成的工具
前端·后端·架构
加个鸡腿儿13 小时前
从"包裹器"到"确认按钮"——一个组件的三次重构
前端·vue.js·设计模式
子兮曰13 小时前
AI写代码坑了90%程序员!这5个致命bug,上线就炸(附避坑清单)
前端·javascript·后端