在游戏开发中,经常需要批量设置图片的格式为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);
}
}

Unity编辑器批量设置图片格式的更多相关文章

  1. java批量转换图片格式

    废话不多直接上代码,代码其实也不多.... package com.qiao.testImage; import java.awt.image.BufferedImage; import java.i ...

  2. 使用IMAGEMAGICK的CONVERT工具批量转换图片格式

    使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...

  3. Python批量修改图片格式和尺寸

    Python批量修改图片格式和尺寸 备注: 1.导入了PIL库,是处理图片用的,很强大; 2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除. 3.导入 ...

  4. Mac OS 如何批量转换图片格式

    ​ 在苹果电脑中,如何快速批量地转换图片的格式.苹果电脑的预览程序,可以实现PNG.JPEG.JPEG-2000.PDF.TIFF格式之间的互换.这里以将PNG格式转换为JPEG格式为例. 工具/原料 ...

  5. Python 批量修改图片格式和尺寸

    公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大小的软件网上很多,我的同事原来使用的美图看看的批量转换,但是稍微有点麻烦,每次还需要指定要转换的图 ...

  6. 【最简单】不用ps也可以批量转换图片格式

    不废话直接开始~ 1.新建文件夹,把需要转换的图片放进去,如图: 2.文件夹里建一txt文本,重点来了!txt文本的内容,如果是jpg转为png,则输入“ren *.jpg *.png”,同理png转 ...

  7. PyQt5设置图片格式及动画

    1.缩放图片'''使用QImage.Scale(width,height)方法可以来设置图片'''from PyQt5.QtCore import *from PyQt5.QtGui import * ...

  8. unity批量设置图片为etc2格式或者astc格式

    网上找了半天,没一个能用的,干脆自己写个,直接拷贝这个脚本就行 这个是ios版本的,安卓的话写在注释里面,去掉注释就能用了 现在ios支持一种新格式叫astc比原本的pvrtc压缩比更高,而且质量更高 ...

  9. [word]用Word2007查找和替换功能批量设置图片位置

    Word2007的"查找和替换"功能并不仅仅可以对文字进行批量的查找替换,还有很多神奇的功能,比如对插入的图片位置进行批量的查找和调整等等. 今天我们就来试试Word2007的&q ...

  10. python 批量更换图片格式脚本

    问题:将某文件下的所有jpg的图片更换为png的图片 简单的实现: # -*- coding:utf-8 -*- from os.path import splitext import glob fr ...

随机推荐

  1. 字符串— trim()、trimStart() 和 trimEnd()

    在今天的教程中,我们将一起来学习JavaScript 字符串trim().trimStart() 和 trimEnd(). 01.trim() 学习如何使用 JavaScript  trim()方法从 ...

  2. CSV文件存储

    CSV 文件存储 CSV,全称为 Comma-Separated Values,中文可以叫作逗号分隔值或字符分隔值,其文件以纯文本形式存储表格数据.该文件是一个字符序列,可以由任意数目的记录组成,记录 ...

  3. 【VyOS-开源篇-3】- container for vyos 搭建 Halo 博客-vyos-开源篇

    文章说明:介绍在vyos软路由上配置container容器,vyos最新滚动版1.5已经支持在vyos命令行中启动docker容器,在vyos 官网介绍是说1.3版本之后就都有这个功能了,如果你的版本 ...

  4. Windows下用VS2022编译安装Boost库

    Windows下用VS2022编译安装Boost库 下载地址: https://www.boost.org/users/download/ 解压得到如下文件: 编译安装: 打开vs2022命令行工具 ...

  5. Linux 内核:I2C子系统分析(1)基于子系统的驱动分析与实现

    Linux 内核:I2C子系统分析(1)基于子系统的驱动分析与实现 背景 在学习高通平台的有关知识,看到一篇博客中介绍了GPIO模拟I2C设备,觉得挺有意思的. 看了看有关的实现,发现自己之前学习从L ...

  6. 3.8折钜惠,瑞芯微RK3568J国产工业评估板“限时折扣”!

  7. Linux的访问权限详解

    题目 解读访问权限 rw-r--r--分别代表什么东西 r:代表可读 w:可写 e:可执行 方便起见进行拆分 rw- 代表文件所属用户的权限 r-- 代表同组用户的权限 r-- 代表其他用户的权限 同 ...

  8. SpringBoot定义异步任务类需要获取结果

    注意点: 要把异步任务封装到类里面,不能直接写到Controller 增加Future<String>返回结果AsyncResult<String>("task执行完 ...

  9. 【VMware vCenter】VMware vCenter Server(VCSA) 5.5 版本证书过期问题处理过程。

    之前帮客户处理了一个因证书过期导致 vCenter Server 无法登录的问题,在此记录一下,因为时间过去有点久了,可能会有些地方描述的不是很清楚,所以就当作参考就行.客户环境是一个非常老的 vCe ...

  10. 【译】You probably should stop using a custom TaskScheduler

    来自Sergey Tepliakov的 https://sergeyteplyakov.github.io/Blog/csharp/2024/06/14/Custom_Task_Scheduler.h ...