Version: 2022.3

InitializeOnLoadAttribute

class in UnityEditor

切换到手册

描述

允许在 Unity 加载时和重新编译脚本时初始化 Editor 类。

重新编译项目中的脚本时,会调用带有此属性的静态构造函数(也称为域重新加载)。在 Unity 首次加载项目时、在 Unity 检测到脚本修改时(取决于自动刷新首选项),以及当进入运行模式时(取决于运行模式配置),会进行此调用。

Asset operations such as asset loading should be avoided in InitializeOnLoad methods. InitializeOnLoad methods are called before asset importing is completed and therefore the asset loading can fail resulting in a null object. To do initialization after a domain reload which requires asset operations use the AssetPostprocessor.OnPostProcessAllAssets callback. This callback supports all asset operations and has a parameter signaling if there was a domain reload.

另请参阅:启动时运行编辑器代码