Sprite的导入参数是无法在运行时修改的,如果要批量导入或者批量修改成特定格式,一张一张图片修改肯定会很慢。
Unity3D允许用C#扩展编辑器功能,可以很方便的执行批量导入和修改。
编辑器扩展代码放在Asserts/Editor目录下,一个cs脚本可以写多个编辑器扩展,每个扩展函数必须是static,通过[MenuItem(“Tools/auto SpriteSet &c”)]注释菜单路径,Unity3D编辑器会在对应位置增加扩展菜单,点击菜单即可执行对应的编辑器扩展函数 如
[MenuItem("Tools/auto SpriteSet &c")]
static void BatchSpriteSet()
{
}
以下代码批量设置Sprite为可读写、非压缩格式,用于运行时读取Sprite文件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class MySpriteSet : Editor
{
[MenuItem("Assets/MyEditor/SpriteSet &c")]
static void SpriteSet()
{
if (Selection.objects.Length > 0)
{
foreach (Texture texture in Selection.objects)
{
string selectionPath = AssetDatabase.GetAssetPath(texture);
TextureImporter textureIm = AssetImporter.GetAtPath(selectionPath) as TextureImporter;
textureIm.textureType = TextureImporterType.Sprite;
textureIm.spriteImportMode = SpriteImportMode.Single;
textureIm.isReadable = true;
var setting = textureIm.GetDefaultPlatformTextureSettings();
setting.format = TextureImporterFormat.RGBA32;
textureIm.SetPlatformTextureSettings(setting);
AssetDatabase.ImportAsset(selectionPath);
}
}
}
[MenuItem("Assets/MyEditor/batch SpriteSet &c")]
static void FolderSpriteSet()
{
List<string> paths = new List<string>();
foreach (Object obj in Selection.objects)
{
string selectionPath = AssetDatabase.GetAssetPath(obj);
paths.Add(selectionPath);
}
string[] allPath = AssetDatabase.FindAssets("t:Sprite", paths.ToArray());
EditorApplication.update = delegate ()
{
for (int i = 0; i < allPath.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(allPath[i]);
bool isCancel = EditorUtility.DisplayCancelableProgressBar("执行中...", path, (float)i / allPath.Length);
if (isCancel)
{
break;
}
TextureImporter textureIm = AssetImporter.GetAtPath(path) as TextureImporter;
if (null != textureIm)
{
textureIm.textureType = TextureImporterType.Sprite;
textureIm.spriteImportMode = SpriteImportMode.Single;
textureIm.isReadable = true;
var setting = textureIm.GetDefaultPlatformTextureSettings();
setting.format = TextureImporterFormat.RGBA32;
textureIm.SetPlatformTextureSettings(setting);
AssetDatabase.ImportAsset(path);
}
}
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
};
}
}
批量执行结果 批量设置后的导入参数
|