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:
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.
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, or a function in any script attached to it is called.)
- 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.
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.
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.
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.
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 becauseFixedUpdate is called on a reliable timer, independent of the frame rate.
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 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.
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(Pro only): Called after scene rendering is complete to allow postprocessing of the screen image.
- 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. In the web player it is called when the web view is closed.
- OnDisable: This function is called when the behaviour becomes disabled or inactive.
Execution Order of Event Functions的更多相关文章
- Event Functions
[Event Functions] A key concept in games programming is that of making changes to position, state an ...
- Unity3D Script Execution Order ——Question
我 知道 Monobehaviour 上的 那些 event functions 是 在主线程 中 按 顺序调用的.这点从Manual/ExecutionOrder.html 上的 一张图就可以看出来 ...
- unity 脚本执行顺序设置 Script Execution Order Settings
通过Edit->Project Settings->Script Execution Order打开MonoManager面板 或者选择任意脚本在Inspector视图中点击Execu ...
- Execution Order for the ApiController
Execution Order for the ApiController Assuming the request goes into the ApiController scope, the op ...
- Unity3d - 初学篇 Event Functions 的 继承 机制
我们知道Start() Update() 等之类的 事件函数 在Unity 主线程中是依次调用的.至于调用的顺序可以查手册. 由此继承机制也会发生一些改变. 测试一: public class MyT ...
- Test execution order
刚开始的时候,JUnit并没有规定测试方法的调用执行顺序.方法通过映射的API返回的顺序进行调用.然 而,使用JVM顺序是不明智的,因为Java平台没有规定任何特定的顺序,事实上JDK7或多或少的返回 ...
- Execution Order In a Test Plan
1.Config Element 2.Pre Processors 3.Timer 4.Sampler 5.Post Processors 6.Assertions 7.Listener
- Unity 脚本生命周期流程图
渲染 OnPreCull: 在相机剔除场景之前调用此函数.相机可见的对象取决于剔除.OnPreCull 函数调用发生在剔除之前. OnBecameVisible/OnBecameInvisible: ...
- NGUI 基础知识
UIRoot 管理 scalePixelPerfect : 像素匹配,图片不会被缩放,除非屏幕高度小于 Minimum Height 或者大于 maximum Height,如果那样的话,就使用 F ...
随机推荐
- leveldb 学习笔记之log结构与存取流程
log文件的格式 log文件每一条记录由四个部分组成: CheckSum,即CRC验证码,占4个字节 记录长度,即数据部分的长度,2个字节 类型,这条记录的类型,后续讲解,1个字节 数据,就是这条记录 ...
- 05 Apache Solr: 管理员界面(Admin UI)
为了方便管理员和工程师调整Solr的配置和访问在线文档和其他的帮助,Solr提供了一个Web界面去查看Solr的配置详情,运行查询语句和分析文档字段.这个界面在第三篇里面提到过就是管理员界面 ...
- android xml特殊字符
@ 对应 @ : 对应 : 对应 空格 对应 空格 ! 对应 ! " 对应 " # 对应 # $ 对应 $ % 对应 % & 对应 & ' 对应 ´ ( 对应 ...
- Redis GetTypedClient
C#操作Redis 未包含GetTypedClient定义 原创,转载请注明出处. VS版本:2015 用NuGet安装了Redis的包 按照百度的各种教程来操作,但是发现网上下载的DEMO可以正 ...
- MyElipes遇到 source not found解决方案(查看.class文件源码一劳永逸的解决方法)
在用Myeclipse 或者是eclipse进行开发时候经常遇到这个问题. File class editor source not found 问题.原因很简单,就是因为这是一个源码包,相应的没有编 ...
- My97 设置近3天日期
1.引用my97 js 和css 2.前台代码 <div class="FormItem"> <label> 申请时间</label> < ...
- python 整齐输出与编码读写
# -*- coding:utf-8 -*- # Author:mologa for x in range(1,11): print(repr(x).rjust(2),repr(x*x).rjust( ...
- java多线程详解(8)-volatile,Atomic比较
在变成过程中我们需要保证变量的线程安全,在java中除了使用锁机制或者Threadlocal等保证线程安全,还提供了 java.util.concurrent.atomic.Atomic*(如Atom ...
- Linux系统下的Nginx安装
nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好. ububtu平台编译环 ...
- PHP与memcache安装使用说明
最近网站流量上来后,数据库连接数一直偏高,分析了下,都是正常请求,只是网站功能分的细,单页面数据库查询句偏多了,很多数据是没必要实时查询,缓存起来就可以的!考虑必须用memcache缓存了,减轻mys ...