本文介绍了Awake()和Start()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到我们可以在Awake()Start()中初始化Variable,并且Awake()将在Start()之前调用.

I see that we can initialize Variable in Awake() or Start() and Awake() will be called before Start().

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

When should we initialize in Awake and Start to have the best performance?

推荐答案

通常Awake()用于初始化某些值或脚本是否相互依赖,并且如果其中之一初始化得太晚会导致错误(清醒运行)在游戏开始之前).每个脚本实例也只能调用一次Awake.

Usually Awake() is used to initialize if certain values or script are dependent on each other and would cause errors if one of them is initialized too late (awake runs before the game starts).Awake is also called only once for every script instance.

让我引用文档:

以及有关Start()的信息:

就像Awake函数一样,Start在生命周期中仅被调用一次 的脚本.但是,当脚本对象为 初始化,无论是否启用了脚本. 开始 如果脚本不是,则可能无法在与唤醒"相同的帧上调用 在初始化时启用.

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.

最后一部分有很大的不同

Where the last part makes one big difference

要提出您的问题:

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

If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start would be saving performance as awake() would be called regardless...
every variable would be initialized at the very beginning. At least that's the logical assumption I make.

这篇关于Awake()和Start()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 02:12