gpt4 book ai didi

unity-game-engine - 唤醒()和开始()

转载 作者:行者123 更新时间:2023-12-03 07:34:19 28 4
gpt4 key购买 nike

我发现我们可以在 Awake()Start() 中初始化变量,并且 Awake() 将在 之前调用开始()

我们应该什么时候在AwakeStart中进行初始化才能获得最佳性能?

最佳答案

通常,如果某些值或脚本相互依赖,则使用 Awake() 进行初始化,如果其中一个值或脚本初始化得太晚(awake 在游戏开始之前运行),则会导致错误。每个脚本实例也仅调用一次 Awake。

让我引用文档:

[...] Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start() to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

关于Start():

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

最后一部分有很大不同

回答你的问题:

如果在游戏开始时启用脚本,并且您不需要初始化变量,开始将节省性能 因为 awake() 无论如何都会被调用...
每个变量都会在一开始就被初始化。至少这是我做出的逻辑假设。

关于unity-game-engine - 唤醒()和开始(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652036/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com