当前位置:网站首页>After importing resources, unity also manually modifies the properties of resources? This code can save you a lot of time: assetpostprocessor
After importing resources, unity also manually modifies the properties of resources? This code can save you a lot of time: assetpostprocessor
2022-06-28 12:20:00 【Unique_ eight hundred and forty-nine million nine hundred and n】
Generally speaking , Each project will have relevant regulations on resource management when it is approved , For example, the attribute settings of resources , After importing resources directly, the attributes may not meet the requirements , At this time, you can unity Create an inheritance in AssetPostprocessor Class , Implement the corresponding method , You can automatically modify resource attributes and manage resources uniformly , for example :Texture And so on .
For example, if you modify the attributes before importing the texture , have access to OnPreprocessTexture Method , Get the resource in the method TextureImporter , Then modify the relevant properties , After the resource is successfully imported, the attributes will be unified and meet the requirements !
/// <summary>
/// This function will be called before the texture import completes.
/// </summary>
private void OnPreprocessTexture()
{
Debug.Log(" Import map :" + 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 The platform map is not compressed :" + _importer.assetPath );
}
setting = _importer.GetPlatformTextureSettings("iPhone");
if (setting.format == TextureImporterFormat.ARGB32 || setting.format == TextureImporterFormat.RGB24)
{
Debug.Log("iOS The platform map is not compressed :" + _importer.assetPath);
}
}Similarly, you can modify the attributes after the texture is imported , Can achieve OnPostprocessTexture Method .
/// <summary>
/// This function will be called after the texture import is completed.
/// </summary>
/// <param name="texture"></param>
private void OnPostprocessTexture(Texture2D texture)
{
Debug.Log(" Import map :" + 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 Class is static Method , This method is called for all resource changes .
/// <summary>
/// All resources will be called after importing
/// </summary>
/// <param name="importedAssets"> Import resource path </param>
/// <param name="deletedAssets"> Delete resource path </param>
/// <param name="movedAssets"> Mobile Resource destination path </param>
/// <param name="movedFromAssetPaths"> Move resource source path </param>
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string str in importedAssets)
{
Debug.Log(" Import resources : " + str);
}
foreach (string str in deletedAssets)
{
Debug.Log(" Delete resources : " + str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
Debug.Log(" from :" + movedFromAssetPaths[i]+", Move resources to :" + movedAssets[i]);
}
}Other similar methods such as : Model 、 Audio and other resources also have corresponding methods , They can be implemented on demand .
using UnityEditor;
using UnityEngine;
public class AssetPostprocessorTools : AssetPostprocessor
{
/// <summary>
/// The audio resource is called before it is imported.
/// </summary>
private void OnPreprocessAudio()
{
AudioImporter _importer = (AudioImporter)assetImporter;
_importer.preloadAudioData = true;
}
/// <summary>
/// From the model (.fbx,.mb Documents, etc. ) Call before importing animation
/// </summary>
private void OnPreprocessAnimation()
{
ModelImporter _importer = (ModelImporter)assetImporter;
}
/// <summary>
/// Call before model import
/// </summary>
private void OnPreprocessModel()
{
ModelImporter _importer = (ModelImporter)assetImporter;
}
/// <summary>
/// After the audio resource is imported, it is called.
/// </summary>
/// <param name="clip"></param>
private void OnPostprocessAudio(AudioClip clip)
{
Debug.Log(" Import audio :" + clip.name);
AudioImporter _importer = (AudioImporter)assetImporter;
}
/// <summary>
/// After the model is imported, it is called.
/// </summary>
/// <param name="g"></param>
private void OnPostprocessModel(GameObject g)
{
Debug.Log(" Import model :" + g.name);
}
/// <summary>
/// After the import of the wizard's texture, it is called.
/// </summary>
/// <param name="texture"></param>
/// <param name="sprites"></param>
private void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
{
Debug.Log(" Import texture :" + texture.name);
}
}
Use these methods , You can go to unity When importing resources, the properties of the resources are automatically set .
Reference resources :
Official documents AssetPostprocessor
边栏推荐
- 【Unity编辑器扩展实践】、查找所有引用该图片的预制体
- 设置Canvas的 overrideSorting不生效
- SEO优化的许多好处是与流量有直接关系
- 【Unity编辑器扩展基础】、EditorGUILayout (一)
- It really doesn't matter if a woman fails to pass the college entrance examination and buys thousands of villas in a counter attack
- [C language] about scanf() and scanf_ Some problems of s()
- How to get a generic type
- Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
- Come on, yuanuniverse. Sure enough, the heat won't pass for a while
- URL append parameter method, considering #$ Situation of
猜你喜欢

建立自己的网站(18)
![[C language] about scanf() and scanf_ Some problems of s()](/img/d1/e3d0b845e699c8c1fe3eb9f3b250e1.png)
[C language] about scanf() and scanf_ Some problems of s()

【C语言】结构体嵌套二级指针的使用
![[C language] use of nested secondary pointer of structure](/img/59/8b61805431e152995c250f6dd08e29.png)
[C language] use of nested secondary pointer of structure

Graphics view framework for QT learning (to realize startup animation)

UGUI使用小技巧(五) Scroll Rect组件的使用

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration

【vi/vim】基本使用及命令汇总

不到一小时,苹果摧毁了15家初创公司

开源项目维权成功案例: spug 开源运维平台成功维权
随机推荐
Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
Redis hash hash type string (5)
【编解码】从零开始写H264解码器(1) 总纲
[source code + code comments] error state Kalman filter, extended Kalman filter, gps+imu fusion, EKF eskf gps+imu
【C语言】判断三角形
Usage and principle of precomputedtextcompat
华泰证券开户安全吗? 开户有风险吗
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
设置Canvas的 overrideSorting不生效
已知两个点和中间一个比例的点,求该点坐标
【Unity编辑器扩展实践】、查找所有引用该图片的预制体
AcWing 609. Salary (implemented in C language)
Sha256 encryption tool class
AcWing 608. Poor (implemented in C language)
Source code analysis of ArrayList
6.A-B
Redis 原理 - List
30套JSP网站源代码合集「建议收藏」
RemoteViews布局和类型限制源码分析
MapReduce project case 1