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.pswp.cn/news/251038.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/251038.shtml
英文地址,請注明出處:http://en.pswp.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…