Unity3D事件函数的执行顺序
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脚本中,有一些按照预定顺序执行的事件函数,脚本即是按照此顺序执行的。这个执行顺序描述如下:
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.
Awake:这个函数是在Start函数之前以及预制物体实例化后被调用。 - OnEnable: (only called if the Object is active): This function is called just after the object is enabled.
OnEnable:(仅在对象激活状态下可用):这个函数在对象可用之后被调用。
Before the first frame update 第一帧更新之前
- Start: Start is called before the first frame update only if the script instance is enabled.
Start:如果脚本实例化被启用,则Start函数在第一帧更新之前被调用。
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.
OnApplicationPause:若暂停被检测到,当前帧执行后就调用此函数,在正常的运行期间调用是有效的。在OnApplicationPause被调用后,额外的用一帧来显示图像表明暂停状态。
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()是基于可靠的定时器的,不受帧率的影响。 - Update: Update() is called once per frame. It is the main workhorse function for frame updates.
Update() 每帧调用一次。这是帧更新的主要函数。 - LateUpdate: LateUpdate() is called once per frame, after Update() has finished. Any calculations that are performed in Update() will have completed when LateUpdate() begins. 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.
LateUpdate: 在Update()执行后,LateUpdate() 也是每帧都被调用。在Update()中执行的任何计算都会在LateUpdate()开始之前完成。LateUpdate()的一个常见应用就是第三人称控制器的相机跟随。如果你把角色的移动和旋转放在Update()中,那么你就可以把所有相机的移动旋转放在LateUpdate()。这是为了在相机追踪角色位置之前,确保角色已经完成移动。
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.
OnPreCull:在相机剔除场景前被调用。剔除取决于物体在相机中是否可见。OnPreCull仅在剔除执行之前被调用。 - OnBecameVisible/OnBecameInvisible: Called when an object becomes visible/invisible to any camera.
OnBecameVisible/OnBecameInvisible:当物体在任何相机中可见/不可见时被调用。 - OnWillRenderObject: Called once for each camera if the object is visible.
OnWillRenderObject:如果物体可见,它将为每个摄像机调用一次。 - OnPreRender: Called before the camera starts rendering the scene.
OnPreRender: 在相机渲染场景之前被调用。 - OnRenderObject: Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.
OnRenderObject:在所有固定场景渲染之后被调用。此时你可以使用GL类或者Graphics.DrawMeshNow来画自定义的几何体。 - OnPostRender: Called after a camera finishes rendering the scene.
OnPostRender: 在相机完成场景的渲染后被调用。 - OnRenderImage(Pro only): Called after scene rendering is complete to allow postprocessing of the screen image.
OnRenderImage(仅专业版):在场景渲染完成后被调用,用来对屏幕的图像进行后处理。 - 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.
OnGUI: 每帧被调用多次用来回应GUI事件。布局和重绘事件先被执行,接下来是为每一次的输入事件执行布局和键盘/鼠标事件。 - OnDrawGizmos Used for drawing Gizmos in the scene view for visualisation purposes.
OnDrawGizmos:为了可视化的目的在场景视图中绘制小图标。
Coroutine 协同程序(协程)
Normal coroutine updates are run after the Update function returns. A coroutine is function that can suspend its execution (yield) until the given given YieldInstruction finishes. Different uses of Coroutines:
正常情况下协程是在Update函数返回时执行。协程的功能是,延缓其执行(yield) ,直到给定的YieldInstruction完成。协程的不同用途:
- yield; The coroutine will continue after all Update functions have been called on the next frame.
yield:协程在所有的Update函数于下一帧调用后继续执行。 - yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
yield WaitForSeconds(2):在一个指定的时间延迟后继续执行,在所有的Update函数被调用之后。 - yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
yield WaitForFixedUpdate():在所有脚本上所有的FixedUpdate被调用之后继续执行。 - yield WWW Continue after a WWW download has completed.
yield WWW:在WWW加载完成之后继续执行。 - yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
yield StartCoroutine(MyFunc):用于链接协程,此将等待MyFunc协程完成先。
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).
OnDestroy: 这个函数在所有帧更新之后被调用,在对象存在的最后一帧(对象将销毁来响应Object.Destroy或关闭一个场景)。
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. In the web player it is called when the web view is closed.
OnApplicationQuit:在应用退出之前所有的游戏对象都会调用这个函数。在编辑器中当用户停止播放时它将被调用。在webplayer中,当网页关闭时被调用。 - OnDisable: This function is called when the behaviour becomes disabled or inactive.
OnDisable: 当行为不可用或非激活时,这个函数被调用。
So in conclusion, this is the execution order for any given script:
因此结论是,对于任意给定的脚本的执行顺序为:
- All Awake calls 所有Awake调用
- All Start Calls 所有Start调用
- while (stepping towards variable delta time)
- All FixedUpdate functions 所有FixedUpdate函数
- Physics simulation 物理模拟
- OnEnter/Exit/Stay trigger functions OnEnter/Exit/Stay触发函数
- OnEnter/Exit/Stay collision functions OnEnter/Exit/Stay碰撞函数
- Rigidbody interpolation applies transform.position and rotation
刚体插值应用于transform.position 和 rotation - OnMouseDown/OnMouseUp etc. events OnMouseDown/OnMouseUp等事件
- All Update functions 所有Update函数
- Animations are advanced, blended and applied to transform 高级动画、混合并应用到变换
- All LateUpdate functions 所有LateUpdate函数
- Rendering 渲染
Hints 提示
- If you start a coroutine in LateUpdate it will also be called after LateUpdate just before rendering.
如果你在LateUpdate中开启一个协程,它将在LateUpdate之后渲染之前也会被调用。 - Coroutines are executed after all Update functions.
协程在所有的Update函数完成后执行。
Unity3D事件函数的执行顺序的更多相关文章
- Unity3D事件函数的执行顺序 - 包含渲染等模块的完整版,中英文对照
原文地址: http://www.cnblogs.com/ysdyaoguai/p/3746828.html In Unity scripting, there are a number of eve ...
- unity3D技术之事件函数的执行顺序[转]
unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703 在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...
- Unity3D中自带事件函数的执行顺序(转)
原文:http://www.jianshu.com/p/1d93ece664e2 在Unity3D脚本中,有几个Unity3D自带的事件函数按照预定的顺序执行作为脚本执行.其执行顺序如下: 编辑器(E ...
- Unity3D中自带事件函数的执行顺序
在Unity3D脚本中,有几个Unity3D自带的事件函数按照预定的顺序执行作为脚本执行.其执行顺序如下: 编辑器(Editor) Reset:Reset函数被调用来初始化脚本属性当脚本第一次被附到对 ...
- Unity3D中脚本的执行顺序和编译顺序
http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...
- 【转】Unity3D中脚本的执行顺序和编译顺序(vs工程引用关系)
http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...
- (转)Unity3D中脚本的执行顺序和编译顺序(vs工程引用关系)
自:http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行 ...
- 【转】Unity3D中脚本的执行顺序和编译顺序
支持原文,原文请戳: Unity3D中脚本的执行顺序和编译顺序 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与脚本有关的也就是编译和执行啦 ...
- Unity3D中组件事件函数的运行顺序
事件函数的运行顺序 Unity 脚本中有很多按预设顺序以脚本身份执行的事件函数. 其执行顺序例如以下: 载入第一个场景 启动场景时调用这些函数(为场景中的每一个对象调用一次). Awake: 始终在调 ...
随机推荐
- 留学Essay写作关键:Intensive Reading
留学生的日常除了写写写还是写写写,有时候还是要换换口味.在自己没有作业压力的时候可以尝试去读一些相关书籍或者一些优秀的essay.当然了,这里的阅读可不是走马观花,囫囵吞枣的读,而是用心去“精读”.那 ...
- 创建一个简单的Spring应用
环境已经安装完成,接下来创建一个简单的Spring应用. 创建Spring应用步骤: 创建一个maven项目 添加spring库依赖 创建Bean类 添加Bean的xml装配文件 创建主类 运行应用程 ...
- Linux_Program 前台后台 切换 查看 kill 实用 mark
有时当我们在linux 上 输入 yum repolist 或 curl www.XXX. 时程序由已 :Intel或system 原因 按下 ctrl+z .在Linux终端运行命令的 ...
- UVA - 11582 Colossal Fibonacci Numbers! (巨大的斐波那契数!)
题意:输入两个非负整数a.b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负 ...
- exit(0)与exit(1)
exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数中,则会退出函数并返回一值. 详细说: 1. return返回函数值,是关键字 ...
- 二十八、CI框架之自己写分页类,符合CI的路径规范
一.参照了CSDN上某个前辈写的一个CI分页类,自己删删改改仿写了一个类似的分页类,代码如下: 二.我们在模型里面写2个数据查询的函数,一个用于查询数据数量,一个用于查询出具体数据 三.我们在控制器里 ...
- 发送邮件的几种方法(C#发邮件 和 js前台实现都有)C#后台自动发邮件 js发邮件
1.后台自动发邮件 1)首先设置邮件参数,这里写在configuration里面 <appSettings> <add key="SMTP" value=&quo ...
- 经验分享:Essay写作遇到困难请你这样做
很多留学生在essay写作中可能会遇到很多困难,要么是essay写作内容出现问题,又或者是对于essay写作格式的不了解,导致自己无法顺利完成essay.今天小编就收集了几位留学生的写作经验分享,希望 ...
- 《机实战》第2章 K近邻算法实战(KNN)
1.准备:使用Python导入数据 1.创建kNN.py文件,并在其中增加下面的代码: from numpy import * #导入科学计算包 import operator #运算符模块,k近邻算 ...
- 201771010123汪慧和《面向对象程序设计Java》第十八周实验总结
一.总复习纲要 1. Java语言特点与开发环境配置(第1章.第2章) 2. Java基本程序结构(第3章) 3. Java面向对象程序结构(第4章.第5章.第6章) 4. 类.类间关系.类图 5. ...