当前位置:网站首页>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
边栏推荐
- Source code analysis of ArrayList
- 2018 joint examination of nine provinces & Merging of line segment trees
- MapReduce项目案例1
- Day37 JS note motion function 2021.10.11
- Data analysis learning notes
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
- SHA256加密工具类
- 建立自己的网站(18)
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- Convert black mask picture to color annotation file
猜你喜欢

Using soapUI to obtain freemaker's FTL file template

Why do many people want to change careers as programmers, while some programmers want to change careers as others?

Build your own website (18)

ByteV搭建动态数字孪生网络安全平台----助力网络安全发展

Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly

Source code analysis of ArrayList

Self use demo of basic component integration of fluent

Simple understanding of ThreadLocal

What is data compliance? How to achieve data compliance?

Deployment and optimization of vsftpd service
随机推荐
【Unity编辑器扩展基础】、EditorGUILayout (三)
JS foundation 10
CDC synchronization if the primary key of a database table changes, will it be synchronized into two data or will it be synchronized to update the primary key?
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
Prefix and (one dimension)
自定义标题栏View
AcWing 605. Simple product (implemented in C language)
If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
Url追加参数方法,考虑#、?、$的情况
Leetcode 705. 设计哈希集合
What method is required for word, PDF and txt files to realize full-text content retrieval?
Which programming language will attract excellent talents?
What is DAPP system development and analytical understanding
Custom title bar view
Map sorting tool class
Open3d manual clipping point cloud
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
2. single digit statistics
Redis 原理 - List