AssetPostprocessor

本文深入探讨了UnityEditor的资产导入与处理机制,包括如何通过编辑器类进行资源路径管理、导入器引用及自定义导入过程。详细介绍了如何在导入流程中重载函数以实现对纹理、模型、音频等资源的自定义处理,以及如何接收通知以优化资源导入体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

是一个编辑器类,编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,需要在脚本前面加上 "using UnityEditor"

引用。

这样你可以在导入的设置中重载默认值或修改导入的数据,如纹理或网格。

 

assetPath

 

 

The path name of the asset being imported. 

被导入的资源的路径名

 

 

assetImporter 

 

Reference to the asset importer 

引用资源导入器

 

 

Functions

函数

 

LogWarning

 

 

Logs an import warning to the console. 

记录一个导入警告到控制台

 

 

LogError

 

 

Logs an import error message to the console. 

记录一个导入错误消息到控制台

 

 

GetPostprocessOrder 

 

Override the order in which importers are processed. 


重载导入器执行的次序

 

 

Messages Sent

发送消息

 

OnPreprocessTexture

 

 

Add 

this 

function 

in 

subclass 

to 

get 

notification 

just 

before 

the 

texture 

importer 

is run. 

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

 

 

OnPostprocessTexture 

 

Add this function in a subclass to get a notification when a texture has completed 

importing just before the texture is saved to disk. 

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

 

 

OnPreprocessModel

 

 

Add 

this 

function 

in 

subclass 

to 

get 

notification 

just 

before 

model 

(.fbx, 

.mb 

file etc.) is being imported. 

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

 

 

OnPostprocessModel

 

 

Add this function in a subclass to get a notification when a model has completed 

importing 

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

 

 

OnPostprocessGameObjectW...

 

 

Gets called for each GameObject that had at least one userpropery attached to it 

in the imported file. 

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

[


狗刨学习网

]

 

 

OnAssignMaterialModel

 

 

Feeds a source material 

获取一个源材质

 

 

OnPostprocessAudio 

 

Add 

this 

function 

in 

subclass 

to 

get 

notification 

when 

an 

audio 

clip 

has 

completed 

importing. 

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

 

 



 

OnPreprocessAudio 

 

Add this function in a subclass to get a notification just before an audio clip is 

being imported. 

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

 

 

OnPostprocessAllAssets 

 

This 

is 

called 

after 

importing 

of 

any 

number 

of 

assets 

is 

complete 

(when 

the 

Assets 

progress bar has reached the end). 

在一些资源被导入后调用

(

当资源进度条到达末端


### 如何在 Unity 中配置运行时脚本重新编译 为了实现Unity项目的运行时脚本重新编译,通常情况下并不建议直接修改Unity默认行为,因为这涉及到引擎内部机制以及可能带来的稳定性风险。然而,在某些特定需求下,比如开发工具或者编辑器扩展插件时,则可以通过自定义方法达成目的。 对于希望实现在不重启编辑器的情况下让更改后的C#代码生效的需求,可以利用`AssetPostprocessor`类来监听资源变化事件并触发相应的处理逻辑[^1]。具体做法如下: #### 使用 AssetPostprocessor 实现自动检测与重编译 通过继承`AssetPostprocessor`类,并覆写其成员函数如`OnPreprocessScript()`或`OnCompileComplete()`等,可以在每次保存脚本文件之后执行额外操作,例如刷新场景视图、更新依赖关系等。 ```csharp using UnityEditor; using UnityEngine; public class CustomCompiler : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (var asset in importedAssets) { if (asset.EndsWith(".cs")) { // 如果是C#源码文件发生改变 Debug.Log("Detected script change:" + asset); // 执行必要的清理工作,确保新版本能够正确加载 EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); // 刷新数据库 // 可选:强制立即编译所有待处理项 Compiler.compileScriptsInPlayMode = true; // 设置允许播放模式下的即时编译标志位 } } } [InitializeOnLoadMethod] public static void Initialize() { Debug.Log("Custom compiler initialized."); // 注册回调以便于后续监控资产变动情况 MonoImporter.scriptCompilationFinished += HandleScriptCompilationFinished; } private static void HandleScriptCompilationFailed(object sender, System.EventArgs e) { Debug.LogError("Compilation failed!"); } private static void HandleScriptCompilationFinished(bool success) { if (!success) return; Debug.Log("Compilation succeeded! Reloading assemblies..."); AssemblyReloadEvents.beforeAssemblyReload -= BeforeAssemblyReloadHandler; AssemblyReloadEvents.afterAssemblyReload -= AfterAssemblyReloadHandler; AssemblyReloadEvents.beforeAssemblyReload += BeforeAssemblyReloadHandler; AssemblyReloadEvents.afterAssemblyReload += AfterAssemblyReloadHandler; } private static void BeforeAssemblyReloadHandler() { Debug.LogWarning("Assemblies are about to be reloaded..."); } private static void AfterAssemblyReloadHandler() { Debug.Log("Assemblies have been successfully reloaded."); } } ``` 需要注意的是上述代码片段仅作为概念验证用途展示了一种可行的技术方案;实际应用过程中还需要考虑更多细节问题,例如性能影响评估、异常状况捕获等方面的内容。 另外一种思路是从外部调用命令行接口来进行动态构建过程控制,但这超出了本文讨论范围[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值