Unity的APIUpdater

参考:https://docs.unity3d.com/Manual/APIUpdater.html
参考:http://man.hubwiz.com/docset/Unity_3D.docset/Contents/Resources/Documents/docs.unity3d.com/Manual/APIUpdater.html


APIUpdater

Unity本身就是一个不断迭代和改进的软件,它的类、函数和API都可能会不断更改。当Unity进行大版本更新时,这些改变可能会带来breaking changes(毁灭性的改变)。为了减少这些breaking changes带来的影响,Unity引入了API updater,它会识别出Assembly里过时的代码和dll,从而自动更新对应的代码和dll。APIUpdater有两种:

  • ScriptUpdater:负责更新scripts
  • AssemblyUpdater:负责更新dll files

Note: The API Updater can only fix certain errors and warnings in the API. These are indicated as UnityUpgradable in the console message. You must manually resolve other errors or warnings that the API Updater cannot handle.


Using the API updater

只要脚本触发了编译,就会自动使用API updater,它会自动重写obsolete code,把它变成新的API的形式。举个例子,Unity会自动把light.color = Color.red;更新为GetComponent<Light>().color = Color.red;

具体步骤是这样的:

  1. Unity触发脚本编译

  2. API Updater会检查编译错误和警告,看看它能否handle:如果没有编译错误或警告,则继续;否则,会展示一个对话框,如下图所示,提供自动的update(it displays a dialog offering an automatic update),应该是提示你,让你关掉Editor,备份项目,然后再打开原项目时,Unity就会自动修改老的代码了。
    在这里插入图片描述

  3. If you accept the update,API Updater会更新这个编译单元里所有的脚本

  4. API Updater会不断针对每个编译单元,执行上述操作


还有几点可以注意的:

  • 如果一个脚本被多个编译pass包含,那么它可能会被API updater多次更新,for example, editor scripts.

  • 如果你不允许API updater来更新脚本,那么console里会出现任何脚本编译错误和警告的Log,这些错误和井盖其实是可以在UnityUpgradable里通过API updater解决的,如下图所示
    在这里插入图片描述

  • 如果脚本有其他的编译错误,那么会妨碍API updater执行,Console会出现message来提示你,所以要保证事先没有其他的编译错误,如下图所示:
    在这里插入图片描述


Command line arguments related to API Updater

命令行使用batch mode启动Unity时,输入-accept-apiupdate可以开启API Updater


Logging

API Updater会把它带来的任何改变都存在Editor Log里,可以通过设置UNITY_APIUPDATER_LOG_THRESHOLD 环境变量,来控制打印信息的详细程度。一共有Error (default value)WarningInfoDebug四个等级,详细程度越来越高。

写法如下:

// windows平台
c:> set UNITY_APIUPDATER_LOG_THRESHOLD=Debug
c:> \path\to\unity\Unity.exe

// Linux平台
$ export UNITY_APIUPDATER_LOG_THRESHOLD=Debug
$ /path/to/unity/Unity

// Mac平台
$ export UNITY_APIUPDATER_LOG_THRESHOLD=Debug
$ /path/to/unity/Unity

Log信息如下所示:

[AssemblyUpdater] Property access to 'UnityEngine.Rigidbody
UnityEngine.GameObject::get_rigidbody()' in 'System.Void
Test.ClassReferencingObsoleteUnityAPIThroughEditorAssembly::Run()' replaced with 'T
UnityEngine.GameObject::GetComponent<UnityEngine.Rigidbody>()'.

Troubleshooting

API Updating failed. Check previous console messages.

可能会出现API Updater无法更新所有的过时代码的情况,此时可能要查询一下,是否有只读的代码脚本。

posted @ 2022-12-03 13:07  弹吉他的小刘鸭  阅读(157)  评论(0编辑  收藏  举报