当前位置:网站首页>Unity导入资源后还手动修改资源的属性?这段代码可以给你节约很多时间:AssetPostprocessor
Unity导入资源后还手动修改资源的属性?这段代码可以给你节约很多时间:AssetPostprocessor
2022-06-28 12:00:00 【Unique_849997563】
一般来说,每个项目在立项的时候对资源的管理都会有相关的规定,比如资源的属性设置,而直接导入资源后可能属性是不满足需求的,这时候就可以在unity中创建一个继承AssetPostprocessor的类,实现相应的方法,就可以自动修改资源属性统一管理资源了,例如:Texture等资源。
比如纹理导入完成之前修改属性的话,可以使用OnPreprocessTexture方法,在方法中获取资源的TextureImporter ,然后修改相关属性,资源导入成功之后属性就会统一并且满足需求了!
/// <summary>
/// 该函数会在纹理导入完成之前调用
/// </summary>
private void OnPreprocessTexture()
{
Debug.Log("导入贴图:" + assetPath);
TextureImporter _importer = (TextureImporter)assetImporter;
_importer.textureType = TextureImporterType.Default;
_importer.mipmapEnabled = false;
_importer.isReadable = false;
var setting = _importer.GetPlatformTextureSettings("Android");
if (setting.format == TextureImporterFormat.ARGB32 || setting.format == TextureImporterFormat.RGB24)
{
Debug.Log("Android平台贴图没压缩:" + _importer.assetPath );
}
setting = _importer.GetPlatformTextureSettings("iPhone");
if (setting.format == TextureImporterFormat.ARGB32 || setting.format == TextureImporterFormat.RGB24)
{
Debug.Log("iOS平台贴图没压缩:" + _importer.assetPath);
}
}同理也可以纹理导入完成之后修改属性,可以实现OnPostprocessTexture方法。
/// <summary>
/// 该函数会在纹理导入完成之后调用
/// </summary>
/// <param name="texture"></param>
private void OnPostprocessTexture(Texture2D texture)
{
Debug.Log("导入贴图:" + texture.name);
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter != null)
{
string AtlasName = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(assetPath)).Name;
textureImporter.textureType = TextureImporterType.Sprite;
textureImporter.spriteImportMode = SpriteImportMode.Single;
textureImporter.spritePackingTag = AtlasName;
textureImporter.mipmapEnabled = false;
}
}OnPostprocessAllAssets类是static方法,所有资源的变化都会调用这个方法。
/// <summary>
/// 所有的资源的导入完成后都会调用
/// </summary>
/// <param name="importedAssets">导入资源路径</param>
/// <param name="deletedAssets">删除资源路径</param>
/// <param name="movedAssets">移动资源目标路径</param>
/// <param name="movedFromAssetPaths">移动资源源路径</param>
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string str in importedAssets)
{
Debug.Log("导入资源: " + str);
}
foreach (string str in deletedAssets)
{
Debug.Log("删除资源: " + str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
Debug.Log("从:" + movedFromAssetPaths[i]+",移动资源到:" + movedAssets[i]);
}
}其他类似方法如:模型、音频等资源也有对应的方法,可以根据需求实现它们。
using UnityEditor;
using UnityEngine;
public class AssetPostprocessorTools : AssetPostprocessor
{
/// <summary>
/// 音频资源导入完成之前调用
/// </summary>
private void OnPreprocessAudio()
{
AudioImporter _importer = (AudioImporter)assetImporter;
_importer.preloadAudioData = true;
}
/// <summary>
/// 从模型(.fbx,.mb文件等)导入动画之前调用
/// </summary>
private void OnPreprocessAnimation()
{
ModelImporter _importer = (ModelImporter)assetImporter;
}
/// <summary>
/// 模型导入之前调用
/// </summary>
private void OnPreprocessModel()
{
ModelImporter _importer = (ModelImporter)assetImporter;
}
/// <summary>
/// 音频资源导入完成之后调用
/// </summary>
/// <param name="clip"></param>
private void OnPostprocessAudio(AudioClip clip)
{
Debug.Log("导入音频:" + clip.name);
AudioImporter _importer = (AudioImporter)assetImporter;
}
/// <summary>
/// 模型导入完成之后调用
/// </summary>
/// <param name="g"></param>
private void OnPostprocessModel(GameObject g)
{
Debug.Log("导入模型:" + g.name);
}
/// <summary>
/// 精灵的纹理导入完成之后调用
/// </summary>
/// <param name="texture"></param>
/// <param name="sprites"></param>
private void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
{
Debug.Log("导入纹理:" + texture.name);
}
}
使用这些方法,就可以在unity导入资源的时候自动设置资源的属性了。
参考:
官方文档 AssetPostprocessor
边栏推荐
- 【C语言】如何很好的实现复数类型
- 【C语言】随机数文件对其进行三种排序方法
- Chapter 2 do you remember the point, line and surface (2)
- 【C语言】关于scanf()与scanf_s()的一些问题
- AcWing 605. Simple product (implemented in C language)
- 【C语言】文件读写函数使用
- 【vi/vim】基本使用及命令汇总
- Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
- Remoteviews layout and type restriction source code analysis
- ProCAST finite element casting process simulation software
猜你喜欢

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架

Share the easy-to-use fastadmin open source system - practical part

已知两个点和中间一个比例的点,求该点坐标

Day37 JS note motion function 2021.10.11

【C语言】关于scanf()与scanf_s()的一些问题

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

纯纯大怨种!那些年被劝退的考研专业

Database Series: is there any way to seamlessly upgrade the business tables of the database

Day34 JS notes regular expression 2021.09.29

Remoteviews layout and type restriction source code analysis
随机推荐
AcWing 608. Poor (implemented in C language)
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
开源项目维权成功案例: spug 开源运维平台成功维权
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
來吧元宇宙,果然這熱度一時半會兒過不去了
JS foundation 10
Day31 JS notes DOM 2021.09.26
Int~long long indicates the maximum and minimum number
2018 joint examination of nine provinces & Merging of line segment trees
RemoteViews布局和类型限制源码分析
Custom title bar view
【C语言】文件读写函数使用
JNI函数的2种书写方式
AsyncTask经验小结
Fruit FL studio/cubase/studio one music host software comparison
AcWing 605. Simple product (implemented in C language)
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
.NET混合开发解决方案24 WebView2对比CefSharp的超强优势
What is the main chain system?