Unity脚本执行顺序和Start()
Unity script execution order and Start()
Unity 的文档有这一行:
By default, the Awake, OnEnable and Update functions of different scripts are called in the order the scripts are loaded (which is arbitrary).
所以我这里有两个问题:
- 在这种情况下,"Arbitrary" 是什么意思?是随机的吗?
- 这是否还包括 Start() 和 Awake(),或者 Start() 是否有自己的特殊行为,不遵循脚本执行顺序,即使该顺序已在项目设置中更改?
我想知道 Unity "decides" 它在运行时的表现如何,特别是因为它似乎有时会起作用,但在其余时间它会导致崩溃或其他问题几乎没有解释,但文档并没有真正提到它,我似乎无法在其他地方找到太多信息。
这个说法有点令人困惑。
Awake、OnEnable 和 Update 将总是按顺序调用。
1.What does "Arbitrary" mean in this context? Is it random?
是的,是随机的。虽然,不是谈论Awake、OnEnable和Update功能。它在谈论脚本。脚本是随机选择执行的。
2.Does this also include Start() alongside Awake(), or does Start() have its own special behaviour that doesn't follow the script
execution order, even if that order has been altered in the project
settings?
答案 #1 也应该回答问题 #2。
这不影响 Start() Awake() 或 OnEnable() 等回调函数。
I've wondered for a while how Unity "decides" how it behaves when it
runs, especially since it seems like something will work some of the
time but the rest of the time it causes a crash or something with
little to no explanation
是的,这是真的。这在过去也发生在我身上。当您有包含许多脚本的大型项目时,更容易发生这种情况。脚本是随机调用的。有时,您会收到空异常错误,因为 GetComponent
无法工作。这就是制作 Script Execution Order Settings 的原因,以便您始终可以设置脚本执行的顺序。
我解决此类问题的方法是在协程函数中执行 GetComponent
。之后,我检查它是否为空。如果为空,请等待一帧然后重试 GetComponent
。
同样,这适用于脚本的执行顺序,而不是回调函数的顺序 invoked/called。
问题 1
根据https://docs.unity3d.com/Manual/class-ScriptExecution.html,您可以手动设置脚本加载的顺序。如果您不设置顺序,我猜 Unity 使用一些预定义的顺序(随机、字母顺序或其他顺序)
问题二
Awake() 始终在 Start() 之前 运行 秒,并在加载脚本时调用 https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html。
要按特定顺序加载脚本,您需要使用脚本执行顺序。为确保您 运行 每个对象的 Start() 函数都以特定顺序运行,您需要制作一个 "Manager" 对象,以您希望的顺序实例化对象。
Unity 的文档有这一行:
By default, the Awake, OnEnable and Update functions of different scripts are called in the order the scripts are loaded (which is arbitrary).
所以我这里有两个问题:
- 在这种情况下,"Arbitrary" 是什么意思?是随机的吗?
- 这是否还包括 Start() 和 Awake(),或者 Start() 是否有自己的特殊行为,不遵循脚本执行顺序,即使该顺序已在项目设置中更改?
我想知道 Unity "decides" 它在运行时的表现如何,特别是因为它似乎有时会起作用,但在其余时间它会导致崩溃或其他问题几乎没有解释,但文档并没有真正提到它,我似乎无法在其他地方找到太多信息。
这个说法有点令人困惑。
Awake、OnEnable 和 Update 将总是按顺序调用。
1.What does "Arbitrary" mean in this context? Is it random?
是的,是随机的。虽然,不是谈论Awake、OnEnable和Update功能。它在谈论脚本。脚本是随机选择执行的。
2.Does this also include Start() alongside Awake(), or does Start() have its own special behaviour that doesn't follow the script execution order, even if that order has been altered in the project settings?
答案 #1 也应该回答问题 #2。 这不影响 Start() Awake() 或 OnEnable() 等回调函数。
I've wondered for a while how Unity "decides" how it behaves when it runs, especially since it seems like something will work some of the time but the rest of the time it causes a crash or something with little to no explanation
是的,这是真的。这在过去也发生在我身上。当您有包含许多脚本的大型项目时,更容易发生这种情况。脚本是随机调用的。有时,您会收到空异常错误,因为 GetComponent
无法工作。这就是制作 Script Execution Order Settings 的原因,以便您始终可以设置脚本执行的顺序。
我解决此类问题的方法是在协程函数中执行 GetComponent
。之后,我检查它是否为空。如果为空,请等待一帧然后重试 GetComponent
。
同样,这适用于脚本的执行顺序,而不是回调函数的顺序 invoked/called。
问题 1
根据https://docs.unity3d.com/Manual/class-ScriptExecution.html,您可以手动设置脚本加载的顺序。如果您不设置顺序,我猜 Unity 使用一些预定义的顺序(随机、字母顺序或其他顺序)
问题二
Awake() 始终在 Start() 之前 运行 秒,并在加载脚本时调用 https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html。
要按特定顺序加载脚本,您需要使用脚本执行顺序。为确保您 运行 每个对象的 Start() 函数都以特定顺序运行,您需要制作一个 "Manager" 对象,以您希望的顺序实例化对象。