Unity3d资源处理器AssetPostprocessor简单用法
发布日期:2021-06-30 19:38:37 浏览次数:2 分类:技术文章

本文共 2310 字,大约阅读时间需要 7 分钟。

我们有时候需要在导入资源的时候做一些自动化处理,比如导入图片自动设置压缩格式等,此时我们就需要使用AssetPostprocessor这个类了。

AssetPostprocessor 资源处理器

Unity盛典说明:

这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用

它是资源导入的一个管理器,在资源导入之前和之后可以根据导入的资源做一些设置和一些数据的修改,比如网格,纹理的压缩,模型添加组件等。当资源导入之前和之后都会发送通知,可以根据不同的资源类型,在导入之前和之后做不同的处理。

在子类中重载这个函数以便在纹理导入器运行之前获取通知

在子类中加入这个函数,以便在纹理载入存入磁盘之前获得一个通知。

在子类中加入这个函数,以便在模型载入之前获得一个通知。

在子类中加入这个函数,以便在模型载入之后获得一个通知。

在导入文件中,为每个至少附加了一个用户属性的游戏物体调用。

获取一个源材质

在子类中加入这个函数,以便在一个声音剪辑载入后获得一个通知。

在子类中加入这个函数,以便在一个声音剪辑载入之前获得一个通知。

在一些资源被导入后调用(当资源进度条到达末端)


例子

using UnityEngine;using System.Collections;using UnityEditor;public class MyEditor : AssetPostprocessor {    //模型导入之前调用    public void OnPreprocessModel()    {        Debug.Log ("OnPreprocessModel="+this.assetPath);    }    //模型导入之前调用    public void OnPostprocessModel(GameObject go)    {        Debug.Log ("OnPostprocessModel="+go.name);    }    //纹理导入之前调用,针对入到的纹理进行设置    public void OnPreprocessTexture()    {        Debug.Log ("OnPreProcessTexture="+this.assetPath);        TextureImporter impor = this.assetImporter as TextureImporter;        impor.textureFormat = TextureImporterFormat.ARGB32;        impor.maxTextureSize = 512;        impor.textureType = TextureImporterType.Advanced;        impor.mipmapEnabled = false;    }    public void OnPostprocessTexture(Texture2D tex)    {        Debug.Log ("OnPostProcessTexture="+this.assetPath);    }    public void OnPostprocessAudio(AudioClip clip)    {        }    public void OnPreprocessAudio()    {        AudioImporter audio = this.assetImporter as AudioImporter;        audio.format = AudioImporterFormat.Compressed;    }    //所有的资源的导入,删除,移动,都会调用此方法,注意,这个方法是static的    public static void OnPostprocessAllAssets(string[]importedAsset,string[] deletedAssets,string[] movedAssets,string[]movedFromAssetPaths)    {        Debug.Log ("OnPostprocessAllAssets");        foreach (string str in importedAsset) {            Debug.Log("importedAsset = "+str);        }        foreach (string str in deletedAssets) {            Debug.Log("deletedAssets = "+str);        }        foreach (string str in movedAssets) {            Debug.Log("movedAssets = "+str);        }        foreach (string str in movedFromAssetPaths) {            Debug.Log("movedFromAssetPaths = "+str);        }    }}

 

转载地址:https://linxinfa.blog.csdn.net/article/details/51801319 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:System.Reflection简介
下一篇:适配器模式

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月08日 14时12分04秒