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

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

相关推荐
哆啦A梦15882 分钟前
Vue3魔法手册 作者 张天禹 016_vue3中一些特定用法介绍
前端·vue.js·typescript
henry10101044 分钟前
DeepSeek生成的网页小游戏 - 迷你高尔夫
前端·javascript·游戏·html
薛一半1 小时前
React的组件
前端·javascript·react.js
广州华水科技2 小时前
水库的单北斗GNSS变形监测系统是什么?主要有哪些应用?
前端
薛一半2 小时前
React三大属性之props
前端·javascript·react.js
Wect2 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:题解与思路解析
前端·算法·typescript
烤麻辣烫3 小时前
正则表达式快速掌握
前端·javascript·学习·正则表达式·html
长城20243 小时前
HTML5中可以省略属性值的11个属性总结
前端·html·html5·属性值·省略属性值
木斯佳3 小时前
前端八股文面经大全:小红书前端一面(2026-2-3)·面经深度解析
前端·状态模式
心之语歌4 小时前
flutter 父子组件互相调用方法,值更新
前端·javascript·flutter