使用 Script Execution Order 设置可以指定 Unity 在调用不同 MonoBehaviour 类的事件函数时采用的相对顺序。例如,可以指定 Unity 应该先运行 Rotation
MonoBehaviour 脚本的事件函数,然后才运行 MoveForward
MonoBehaviour 脚本的事件函数。
该顺序单独应用于事件函数的每个类别,所以 Unity 按指定顺序调用其在一帧内需要调用的任何 Awake 函数,随后按相同顺序来调用活动游戏对象的任何 Update 函数。
可以在 Project Settings Inspector 中调整 Script Execution Order 设置。访问菜单:__Edit__ > Project Settings__,然后选择 Script Execution Order__ 类别。
使用加号 (+) 按钮将脚本添加到设置的 Inspector 窗口中,然后选择类名。要删除脚本,请使用位于列表中每项右侧的减号 (-) 按钮。
要指定执行顺序,请将列表中的项拖到所需位置,或编辑列表中类的序号。分配的数字表示相对顺序。Unity 从上到下(从序号负值更大的脚本到序号正值更大的脚本)执行列表。Unity 在 Default Time 设置的时隙内执行列表中未列出的任何脚本:先执行负序号的所有脚本,然后执行列表中未列出的所有脚本,最后执行正序号的所有脚本。
序号是任意数,不代表任何物理数量。Editor 将这些值存储在脚本元数据文件中。您可以在序号之间留一些间隔,这有助于在列表中添加或移动其他脚本时避免进行无关的文件更改。
注意:此设置窗口中指定的执行顺序不会影响以 RuntimeInitializeOnLoadMethod 属性标记的函数的顺序。(无法指定运行时初始化的顺序。)
请参阅事件函数的执行顺序以了解 Unity 在一帧内调用每个不同类别的事件函数的时间。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.