Awake()和Start() [英] Awake() and Start()

查看:135
本文介绍了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.

让我引用文档:

[...]初始化所有对象后,将调用Awake,因此您可以安全地与其他对象讲话或使用例如来查询它们. GameObject.FindWithTag.每个GameObject的Awake均以对象之间的随机顺序进行调用.因此,您应该使用Awake在脚本之间设置引用,并使用Start()来回传递任何信息.始终在任何启动功能之前调用唤醒.这使您可以命令脚本初始化.醒着不能充当协程.

[...] 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()的信息:

在任何脚本之前启用脚本时,会在框架上调用开始 第一次调用Update方法.

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

就像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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆