UGUI之导入图片之前自动设置图片打包的 tag

时间:2023-03-10 02:55:31
UGUI之导入图片之前自动设置图片打包的 tag

之前一直在用的是NGUI,最近不知怎么突然兴趣来潮,想学习一下UGUI,毕竟,现在纵观Unity的市场,完全是UGUI的天下,NGUI已经渐渐退隐江湖,哈哈哈。。。

先来记录下,在图片资源导入到Unity后如何自动的对导入的图片进行一些特定的 格式设置、将packer tag 设置为图片所在文件夹的名字。

Unity是提供给了我们好多接口,在资源被导入之前或者之后,可以对导入的资源进行一定格式设置,如下:

https://docs.unity3d.com/ScriptReference/AssetPostprocessor.html

UGUI之导入图片之前自动设置图片打包的 tag

这里用的是最后一个方法即可实现,代码如下:

using UnityEditor;
using System.IO; /// <summary>
/// Get a notification just before the texture importer is run.
/// </summary>
public class MyTexturePreprocess : AssetPostprocessor
{
//Be called just before the texture importer is run
void OnPreprocessTexture()
{
//Automatically set the texture formate
TextureImporter ti = (TextureImporter)assetImporter;
ti.textureType = TextureImporterType.Sprite; //Automatically set the packing tag
string dir = Path.GetDirectoryName(assetPath);
string folderStr = Path.GetFileName(dir);
ti.spritePackingTag = folderStr;
}
}

将上述代码放到Editor目录下即可。