vs_Community.exe --layout "F:\linson\vs2017 comm\offline" --lang zh-CN

学习unity3d,感觉事件顺序很重要。就翻译一下官方文档吧。

Execution Order of Event Functions

事件函数的执行顺序

In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:

Unity 脚本中,有大量的事件按照预定的顺序作为脚本来执行。

Editor

  • Reset: Reset is called to initialize the script’s properties when it is first attached to the object and also when the Reset command is used.
  • 当第一次附加到对象或执行Reset命令时,Reset事件会被调用,来初始化脚本的属性?(成员变量)

First Scene Load

These functions get called when a scene starts (once for each object in the scene).

  • Awake: This function is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active)
  • Awake函数在所有的Start函数前,且在Prefab 初始化之后执行(如果GameObject 没有激活,那么对于此gameobject,awake事件阶段,就不会执行此gameobject的awake函数)
  • OnEnable: (only called if the Object is active): This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.
  • 在gameobject是active状态下,当gameobject被实例化的时候,会先初始化gameobject 中的特殊compenent:monobehaviour, 而在这些之后,会执行脚本的onenable 事件。
  • OnLevelWasLoaded: This function is executed to inform the game that a new level has been loaded.

Note that for objects added to the scene, the Awake and OnEnable functions for all scripts will be called before Start, Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.

对于scene中的所有object,脚本中的awake和onenable函数会在start,update等等函数之前调用,基本上,在对象初始化期间这是不可强制执行的。

Before the first frame update

  • Start: Start is called before the first frame update only if the script instance is enabled.

For objects added to the scene, the Start function will be called on all scripts before Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.

start在第一帧更新之前调用。当然脚本必须是enable。因为是在第一帧之前调用,所以也就是说只会执行一次。

In between frames

每帧之间

  • OnApplicationPause: This is called at the end of the frame where the pause is detected, effectively between the normal frame updates. One extra frame will be issued after OnApplicationPause is called to allow the game to show graphics that indicate the paused state.

Update Order

更新顺序

When you’re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update function, but there are also other functions you can use.

在处理游戏逻辑,交互,动画,摄像机位置等等的时候,有一些不同的事件是可以使用的。

大多数会采用在update方法中来处理,这是常用的方式。当然也有一些其他的函数可以使用。

  • FixedUpdate: FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate. When applying movement calculations inside FixedUpdate, you do not need to multiply your values by Time.deltaTime. This is because FixedUpdate is called on a reliable timer, independent of the frame rate.

fixedupdate:fixedupdate通常比update调用周期更短,如果帧率比较低,那么帧之间它可能被多次调用,,而如果帧率比较高那么可能有帧之间没有调用的情况。当fixedupdate调用之后,所有的物理计算和更新会立马更新。

当在fixedupdate中计算运动的时候,不需要使用time.deltatime来乘你的数值,因为fixedupdate会独立于帧率,并依靠一个可靠的定时器来调用。

(以下猜想:所以一般会把某些物理特性,交互等修改的时机放入到fixupdate中,因为之后引擎会固定调用物理和交互的更新,所以避免目标机显卡性能差,导致帧率过低,那么恰当的方式就是把物理特性和交互放入到fixupdate中,这样虽然显示慢,但整个游戏的内部逻辑运作是正常的)

  • Update: Update is called once per frame. It is the main workhorse function for frame updates.
  • LateUpdate: LateUpdate is called once per frame, after Update has finished. Any calculations that are performed in Update will have completed when LateUpdatebegins. A common use for LateUpdate would be a following third-person camera. If you make your character move and turn inside Update, you can perform all camera movement and rotation calculations in LateUpdate. This will ensure that the character has moved completely before the camera tracks its position.

Rendering

  • OnPreCull: Called before the camera culls the scene. Culling determines which objects are visible to the camera. OnPreCull is called just before culling takes place.
  • OnBecameVisible/OnBecameInvisible: Called when an object becomes visible/invisible to any camera.
  • OnWillRenderObject: Called once for each camera if the object is visible.
  • OnPreRender: Called before the camera starts rendering the scene.
  • OnRenderObject: Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.
  • OnPostRender: Called after a camera finishes rendering the scene.
  • OnRenderImage: Called after scene rendering is complete to allow post-processing of the image, see Post-processing Effects.
  • OnGUI: Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.
  • OnDrawGizmos Used for drawing Gizmos in the scene view for visualisation purposes.

Coroutines

Normal coroutine updates are run after the Update function returns. A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes. Different uses of Coroutines:

  • yield The coroutine will continue after all Update functions have been called on the next frame.
  • yield WaitForSeconds Continue after a specified time delay, after all Update functions have been called for the frame
  • yield WaitForFixedUpdate Continue after all FixedUpdate has been called on all scripts
  • yield WWW Continue after a WWW download has completed.
  • yield StartCoroutine Chains the coroutine, and will wait for the MyFunc coroutine to complete first.

When the Object is Destroyed

  • OnDestroy: This function is called after all frame updates for the last frame of the object’s existence (the object might be destroyed in response to Object.Destroy or at the closure of a scene).

When Quitting

These functions get called on all the active objects in your scene:

  • OnApplicationQuit: This function is called on all game objects before the application is quit. In the editor it is called when the user stops playmode.
  • OnDisable: This function is called when the behaviour becomes disabled or inactive.

Script Lifecycle Flowchart

The following diagram summarises the ordering and repetition of event functions during a script’s lifetime.

转载于:https://www.cnblogs.com/lsfv/p/8360654.html

Execution Order of Event Functions, unity 3d 事件函数的执行顺序相关推荐

  1. Unity基础开发之事件函数的执行顺序

    Unity基础开发之事件函数的执行顺序 文章目录 前言 一.Initializtion阶段 Awake() OnEnable() 二.Editor阶段 Reset() OnValidate() 三.第 ...

  2. Unity3D中自带事件函数的执行顺序

    在Unity3D脚本中,有几个Unity3D自带的事件函数按照预定的顺序执行作为脚本执行.其执行顺序如下: 编辑器(Editor) Reset:Reset函数被调用来初始化脚本属性当脚本第一次被附到对 ...

  3. Unity脚本生命周期与执行顺序

    目录 脚本生命周期 MonoBehavior生命周期图 脚本执行顺序 自定义执行顺序 @(文章目录) 在Unity中,脚本可以理解为附加在游戏对象上的用于定义游戏对象行为的指令代码.必须绑定在游戏对象 ...

  4. Unity常用事件函数与变量

    一.常用必然事件 Awake:脚本实例被创建时调用(用于游戏对象的初始化,注意Awake的执行早于所有脚本的Start函数) Start: Update函数第一次运行之前调用(用于游戏对象的初始化) ...

  5. jquery之ajax——全局事件引用方式以及各个事件(全局/局部)执行顺序

    jquery中各个事件执行顺序如下: 1.ajaxStart(全局事件) 2.beforeSend(局部事件) 3.ajaxSend(全局事件) 4.success(局部事件) 5.ajaxSucce ...

  6. html alert 确认加事件,js事件中有alert执行顺序的问题

    我运行是弹一个框改变一个颜色,并没有出现所有alert再变色的情况 代码如下 #div1{ width:300px; height:300px; border:1px solid black; } # ...

  7. Unity 3D游戏十:血条制作

    前言 中山大学数据科学与计算机学院3D游戏课程学习记录博客. 游戏代码: gitee 参考师兄的博客: 师兄博客 游戏视频:bilibili 游戏要求 血条(Health Bar)的预制设计. 具体要 ...

  8. Unity学习之常用事件函数Update深度解析

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.常规更新函数 Update() FixedUpdate() LateUpdate() 总结 前言 Unity与常规 ...

  9. javascript中的事件冒泡、事件捕获和事件执行顺序

    谈起JavaScript的 事件,事件冒泡.事件捕获.阻止默认事件这三个话题,无论是面试还是在平时的工作中,都很难避免. DOM事件标准定义了两种事件流,这两种事件流有着显著的不同并且可能对你的应用有 ...

最新文章

  1. 用Java写一个植物大战僵尸简易版!
  2. 网站核心关键词一定要控制在五个之内更方便集中优化
  3. Servlet线程安全2
  4. Git新建临时分支进行开发后合并至master
  5. js原生捕鱼达人(一)
  6. webflux系列--reactor源码(一)
  7. 位置子段最大子段和 hdu 1003 max sum ACM的开始
  8. python yield使用
  9. geoiplookup命令没有任何输出问题
  10. Linux系统的账号管理
  11. 电信网关-天翼网关-GPON-HS8145C设置桥接路由拨号认证
  12. 胡晓明的“出埃及记”,他将带领支付宝驶向何方?
  13. 可编程渲染管线10 多细节层次(LOD)
  14. 商务英语中最易犯的五个错误
  15. 论文笔记 General Advantage Estimation(GAE)
  16. 读书百客:《临江仙引·画舸》赏析
  17. BugKu CTF(杂项篇MISC)---哥哥的秘密
  18. 未分配虚拟网络-清理注册表
  19. 生成微信公众号对应二维码的两种简单方法
  20. 微信小程序(五)--- Vant组件库,API Promise化,MboX全局数据共享,分包相关

热门文章

  1. 【OS】操作系统运行环境
  2. php 定时缓存,php页面设置缓存时间实例代码
  3. 深度学习之循环神经网络(11)LSTM/GRU情感分类问题实战
  4. C语言入门经典题目及其答案
  5. 信息竞赛进阶指南--中缀表达式转后缀表达式并求值(模板)
  6. 如何在Ubuntu中用apt命令删除/卸载软件包(remove/clean/purge)
  7. 基本机器学习面试问题 --- 理论/算法2
  8. 【语义分割】CVPR2021_Rethinking BiSeNet For Real-time Semantic Segmentation
  9. GRE tunnel源码分析之发送流程
  10. 基础004_V7-DSP Slice