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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/251038.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ES6-3 let进阶、const、全部变量与顶层对象

一 const 1. 定义常量 1.1 引入模块时 const test require(http)1.2 定义时必须赋值(初始化)且不可修改 const a; // Uncaught SyntaxError: Missing initializer in const declaration若赋值为原始值,不可修改若赋值为引用值,对于的地址不可修改&a…

前后端如何通信

目录 前后端如何通信URL . URI . URN第一部分:传输协议第二部分:域名第三部分:端口号第四部分:请求资源文件的路径名称第五部分:问号传参第六部分:HASH值前后端如何通信 前段:客户端 后端&#…

vue --- 获取子组件数据的一个应急方案$refs

使用$refs需要注意以下2点: 1.html方法使用子组件时,需使用ref “xxx” 声明. 2.在父组件中使用,this.refs.xxx.msg 获取数据 <!DOCTYPE html> <html> <head> <meta charset"utf-8"> </head> <body><div id"app"…

Mysql 根据出生日期计算年龄

最近因为业务要求需要根据出生日期计算年龄&#xff0c;在网上查了好多的方法&#xff0c;在这里总结一下。 网上的计算方法好多都提到了格里高利历法&#xff0c;特意去查了下资料&#xff0c;普及点知识。 格里高利历是公历的标准名称&#xff0c;是一种源自于西方社会的历法…

ES6-4/5 解构赋值、函数默认值、数组解构、对象解构

ES-4 解构赋值、函数默认值、数组解构、对象解构 ES-5 隐式转换、函数参数解构、解构本质、()用法 一 解构赋值 1 虚值 含义&#xff1a;在Boolean转换结果为假的值falsy 2 函数默认值 ES6 内部使用严格相等运算符&#xff08;&#xff09;&#xff0c;判断一个位置是否有值…

springboot之session、cookie

1- 获取session的方案 session: https://blog.csdn.net/yiifaa/article/details/77542208 2- session什么时候创建&#xff1f; 一个常见的误解是以为session在有客户端访问时就被创建&#xff0c;然而事实是直到某server端程序调用HttpServletRequest.getSession(true)这样…

echarts --- 多折线图按段显示颜色规则订制

描述: 图中有4个序列,序列1和序列2在同一个x轴下,显示不同的颜色.(如,在-40到-30,序列一是红色,而序列2是黑色) 关键: VisualMap中的seriesIndex属性(根据不同的系列,制定不同的颜色规则). 下面是代码,可以直接复制到 echart实例 中进行调试 var symbolSize 20; var data [[…

Git-分布式版本控制系统

一、版本控制 版本控制系统是记录若干文件内容变化&#xff0c;以便将来查阅修订特定版本或还原部分文件的系统 分为&#xff1a;集中式版本控制系统&#xff08;svn&#xff09;简称cvcs 都有一个单一集中管理服务器&#xff0c;保存所有文件修订版本&#xff0c;开发人员通…

ES6-6 - this指向、箭头函数基本形式、rest运算符

一 chrome断点调试 观察函数调用栈 // 25min var x 1; function foo(x, y function () { x 2; console.log(2) }) {var x 3;y();console.log(x) } foo() console.log(x) // 2 3 1var x 1; function foo(x, y function () { x 2; console.log(x) }) {x 3;y();console.…

【二分答案】Problem C:木材加工

Problem C:木材加工 Time Limit:1000MS Memory Limit:65536K Total Submit:48 Accepted:20 Description 【问题描述】 木材厂有一些原木&#xff0c;现在想把这些木头切割成一些长度相同的小段木头&#xff08;木头有可能有剩余&#xff09;&#xff0c;需要得到的小段的数目是…

vue --- vue.js实战基础篇课后练习

练习1:在输入框聚焦时,增加对键盘上下键按键的支持,相当于加1和减1 练习2:增加一个控制步伐的prop-step,比如设置为10,点击加号按钮,一次增加10 思路: // 考虑到子模板的复用性,即在父模板中复用如下: <input-number v-model"value" :max"10" :min&qu…

js打字效果

//文字依次出来效果 $.fn.autotype function() {var $text $(this);// console.log(this, this);var str $text.html(); //返回被选 元素的内容var index 0;var x $text.html();//$text.html()和$(this).html()有区别var timer setInterval(function() {//substr(index, …

ES6-7 - 箭头函数的实质、箭头函数的使用场景

箭头函数返回对象 // 这种情况要要用(),否则会将对象的{}解释为块 const fn (a, b) > ({a:1, b:2})箭头函数的特点 this指向由外层函数的作用域来决定&#xff0c;它本身没有this&#xff0c;不能通过call、apply、bind改变不能作为构造函数使用不可以使用arguments对象&…

mybatis比hibernate处理速度快的原因

mybatis:是面向结果集的。当要展示的页面需要几个字段时&#xff0c;springmvc会提供这几个字段并将其拼接成结果集&#xff0c;在转化为相应的对象。 hibernate&#xff1a;是面向对象的。要展示的页面需要某些字段时&#xff0c;会将所有字段都查出来&#xff0c;在转化为相应…

zabbix 从入门到精通

https://www.cnblogs.com/clsn/p/7885990.html 转载于:https://www.cnblogs.com/learningJAVA/p/8376589.html

javasript --- 一个日期规范(x秒前,x分前...)

Time函数(通俗易懂,自己根据实际需求修改吧- -) // time.js var Time {// 获取当前时间戳getUnix: function () {var date new Date();return date.getTime();},// 获取今天0点0分0秒的时间戳getTodayUnix: function () {var date new Date();date.setHours(0);date.setMin…

ES6-8 - 函数名/对象拓展、描述符、getter/setter

函数名 有两种特殊情况&#xff1a;bind方法创造的函数&#xff0c;name属性返回bound加上原函数的名字&#xff1b;Function构造函数创造的函数&#xff0c;name属性返回anonymous。 bind函数名 // 以bound开头 function foo() { } const fnName foo.bind().name console.lo…

javascript --- 再识闭包

看下面一个例子: function zipCode(code, location) {let _code code;let _location location || ;return {code: function () {return _code;},location: function() {return _location;}} }再上述封闭的函数中,code的匿名函数根据作用域链可以访问到外面的_code变量. con…

iframe.contentWindow介绍

一、在使用iframe的页面&#xff0c;要操作这个iframe里面的DOM元素可以用&#xff1a; contentWindow、contentDocument(测试的时候chrome浏览器&#xff0c;要在服务器环境下) 1、先获取iframe里面的window对象&#xff0c;再通过这个对象&#xff0c;获取到里面的DOM元素 例…

ES6-9 对象密封4种方式、assign、取值函数的拷贝

一 对象密封 1 Object.preventExtensions 禁止对象拓展&#xff0c;仍可删除 严格模式下报错 const origin {a: 1 } const fixed Object.preventExtensions(origin) console.log(origin fixed) // true console.log(Object.isExtensible(origin)) // false 不可拓展 orig…