【Unity】像素图片导入批处理

本文介绍了如何在Unity中批量处理像素图片,自动将Texture Type设置为Sprite (2D and UI),并设置Packing Tag为文件夹名。通过使用Unity的AssetPostprocessor,实现了在导入图片时自动应用设置,包括Filter Mode为Point模式,适用于像素风格的图片。代码示例展示了具体的实现过程,并提到了其他相关的图片批处理和Unity快捷键设置资源。
摘要由CSDN通过智能技术生成

具体可以参照 Unity3d 官方文档。

http://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPreprocessTexture.html

代码出处:http://blog.csdn.net/huutu


在此基础上添加了设置Filter Mode为Point模式,可以适用于像素图片的处理

当然同样可以处理,如下图所示的所有设置


/**************************
 * 文件名:AutoSetTextureUISprite.cs;
 * 文件描述:导入图片资源到Unity时,自动修改为UI 2D Sprite,自动设置打包tag 为文件夹名字;
 * 创建日期:2015/05/04;
 * Author:陈鹏;
 ***************************/

using UnityEngine;
using System.Collections;
using UnityEditor;

public class AutoSetTextureUISprite :AssetPostprocessor
{

    void OnPreprocessTexture()
    {

Unity,你可以使用Editor Script来对图片资源进行批处理。以下是一些简单的示例。 1. 批量修改图片格式 你可以使用以下代码将所有选定的图片转换为PNG格式: ```csharp using UnityEditor; using UnityEngine; using System.IO; public class BatchImageFormat : EditorWindow { [MenuItem("Tools/Batch Image Format Converter/Convert to PNG")] static void ConvertToPNG() { foreach (Object obj in Selection.objects) { string assetPath = AssetDatabase.GetAssetPath(obj); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; textureImporter.textureCompression = TextureImporterCompression.Uncompressed; textureImporter.SaveAndReimport(); Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); byte[] bytes = texture.EncodeToPNG(); File.WriteAllBytes(assetPath, bytes); } } } ``` 2. 批量修改图片尺寸 你可以使用以下代码将所有选定的图片的尺寸修改为指定的宽度和高度: ```csharp using UnityEditor; using UnityEngine; using System.IO; public class BatchImageResize : EditorWindow { int newWidth = 512; int newHeight = 512; [MenuItem("Tools/Batch Image Resizer/Resize Images")] static void Init() { BatchImageResize window = EditorWindow.GetWindow<BatchImageResize>(); window.Show(); } void OnGUI() { EditorGUILayout.LabelField("New Size"); newWidth = EditorGUILayout.IntField("Width", newWidth); newHeight = EditorGUILayout.IntField("Height", newHeight); if (GUILayout.Button("Resize")) { foreach (Object obj in Selection.objects) { string assetPath = AssetDatabase.GetAssetPath(obj); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; textureImporter.isReadable = true; textureImporter.SaveAndReimport(); Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); TextureScale.Bilinear(texture, newWidth, newHeight); byte[] bytes = texture.EncodeToPNG(); File.WriteAllBytes(assetPath, bytes); } } } } ``` 这里使用了一个名为`TextureScale.Bilinear`的方法来修改纹理的大小。这个方法可以在Unity的官方论坛上找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值