唤醒()和开始() [英] Awake() and Start()

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

问题描述

我看到我们可以在 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 总是在任何 Start 函数之前调用.这允许您对脚本的初始化进行排序.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的 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 在生命周期中只被调用一次的脚本.然而,当脚本对象被调用时,Awake 被调用初始化,无论脚本是否启用.开始如果脚本不是,则可能不会在与 Awake 相同的帧上调用在初始化时启用.

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.

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

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