[Unity] An article to understand the life cycle


foreword

Opened a new hole, this series is mainly to record the dry goods of Unity, unlike the previous ones that were a little bit slobbery 233 Of course I will correct it

After half a month, I have gone through a series of miscellaneous things such as the start of school and changing classrooms, and I am on the right track~

No miscellaneous introductions, no word count, just get started! (violent theory)


1. Life cycle

编写顺序会严格按照真实的生命周期顺序 自上而下 具体图片请baidu

1. Editor (compile)

variable declaration assignment

变量最终的值只看最后调用的方法,因为会逐步覆盖
Variable declaration and direct assignment>>Inspection panel assignment>>Awake>>OnEnable>>Start>>External assignment (call variable assignment in other scripts)

We generally assign a value to an object, or want to change the value of a property or member variable from an external object if the object already exists (meaning that the Strart method has been initialized and has been called), so the external assignment will be followed by Start method to call

Reset() (reset)

初始化数值 用于角色等信息更改后初始化

  • Calling situation
    This function can only be called in editor mode (not running)
  • Calling time, times and effects
    Called when the script is mounted to the object for the first time or after the Reset command is used.
    To initialize the various properties of the script, the Reset method is most commonly used to provide good default values ​​​​in the inspection panel
  • use
    • Hang the script on the object
    • Click the three dots in the upper right corner of the script and click Reset to execute the Reset command, which is generally used for reassignment
      insert image description here

2. Initialization

Awark() (wake up)

从非激活状态转激活状态会调用一次 创建游戏物体后会调用一次 场景启动会调用一次

  • call situation
    • When initializing the GameObject containing the script's active state when loading the scene
    • GameObject transitions from inactive to active
    • After initializing the GameObject created with Instantite
  • Call time, frequency and effect
    • Unity only calls Awake-times during the lifetime of the script instance. The lifetime of a script lasts until the scene containing it is unloaded.
    • The order in which Unity calls the Awake of each Game0b ject is uncertain, and human intervention (that is, design) is used to ensure the correctness and stability of the program
    • Awake is used instead of the constructor for initialization. In Unity, the initialization of the component does not use the constructor

OnEnable (activate)

游戏物体被激活 调用
脚本组件被激活 调用

  • call situation
    • The game object is activated
    • script component is activated
  • Call time, frequency and effect
    • Repeated assignment changes back to the initial state

Start() (start)

只会在首次和初次调用,且仅调用一次

  • call situation
    • The game object is activated
    • script component is activated
  • Call time, frequency and effect
    • Called before Update on the first frame when the script instance is active
    • After execution with Awake, it is convenient to control the order of calling before and after the logic

external assignment

For example, use class B to hold a reference to class A to assign values ​​to variables in it.
If you assign values ​​​​in the Start method of class B, the order is unknown
because the execution order of the same initialization method of different classes is random.


3. Physis (physical detection)

FixedUpdate() (fixed refresh)

每0.02秒嗲用一次,且早于Update

  • call situation
    • The game object is activated
    • script component is activated
  • Call time, frequency and effect
    • Used every 0.02 seconds, earlier than Update

yield WaitForFixedUpdate

暂时挂起直到固定更新执行一次

OnTriggerXXX (trigger detection)

OnCollisionXXX (collision detection)


4. Input events (input detection)

OnMouseXXX

鼠标输入检测


5. Game logic

Update() (update)

每帧调用

  • call situation
    • The game object is activated
    • script component is activated
  • Call time, frequency and effect
    • Called per frame, is the most commonly used function, called about 60 times per second (according to the performance and status of the current computer)
    • Update data in real time, receive input data

yield null (suspend one frame, and continue processing in the next frame)

here

yield WaitForSeconds (coroutine waiting method)

yield WWW (wait for web request to complete (revert to WaitForSeconds or null))

yield StartCoroutine (open coroutine)

LateUpdate() (late update)

一帧中 在Update之后调用 / 每帧的末尾被调用
例如摄像机移动,可以放在这,人物移动放在Update 因为摄像机大部分都是要比人物后移动

  • call situation
    • The game object is activated
    • script component is activated
  • Call time, frequency and effect
    • LateUpdate is called after all Update functions are called, and the effect is called 60 times per second

6. Scene rendering

OnWillRenderObject (will render the object)

物体被当前场景的摄像机看见(不能是UI物体)调用

  • call situation
    • OnWillRenderObject is called for each camera if the object is visible and not a UI element
    • This function will not be called if the MonoBehaviour is disabled.
    • This function is called during culling processing (i.e. when each culled object is rendered)
    • Doesn't work when called from a UI element
  • Call time, frequency and effect
    • Each frame will be called multiple times, and the calling frequency is visually similar to Update
  • experimental code
    • Just mount it on the object you want to be seeninsert image description here

OnPreCull

``It’s hard to say anything, there are big guys who know that you can leave a message - call the situation
-be called before the camera blanks the scene.
- `This function is called before the camera starts rendering the scene.
- This function is called during culling processing (i.e. when each culled object is rendered)
- This function is only used in scripts where the camera is hosted. This message is triggered when this camera culls a rendered scene

  • Call time, frequency and effect
    • It seems that it will be called before or after rendering

OnBecameVisible&OnBecameInvisible (about courseware & about to be invisible)

表示当该物体上挂载的脚本中有这个函数时,进入当前摄像机渲染的画面时调用该函数

  • call situation

    • When running in the Editor, the Scene view camera will also cause this function to be called, other cameras will not trigger the call if the object has already been rendered by this camera
      insert image description here

    As shown in the figure, the object (cube) has been rendered by the Scene view camera, and the OnBecameVisible function is called once. When the main camera (Main Camera) enters, it will no longer be triggered, and the OnBecameInvisible function will not be triggered when it leaves

    • `Will not be called if the MeshRenderer component is missing or not activated.
    • There are multiple cameras in the scene, if the object (cube) is rendered by one of the cameras, the other cameras will not trigger the OnBecameVisible function again when they enter again, and the OnBecameInvisible function will not be triggered when they leave (same as 1)
  • Call time, frequency and effect

    • Rendering succeeds once and leaves once

OnPreRender (about to render)

``Event function called by Unity before the camera renders the scene. 个人感觉编辑器环境下 Scene窗口已经渲染完成了 所以不能触发 个人拙见

OnPostRender (rendering complete)

``Event function called by Unity after the camera renders the scene. 个人感觉编辑器环境下 Scene窗口已经渲染完成了 所以不能触发 个人拙见
The above two seem to have to be mounted to the camera before execution

OnRenderImage

效果和上述一致
For specific principles, please refer to this podcast (now it’s too good =0=)

Principle of OnRenderImage


7.Gizmo rendering (self-written tools)

OnGrawGizmos (Gizmos rendering)

Gizmos are generally used by developers, referring to objects such as cameras and wireframes displayed in the scene editor during development. Therefore, the content in this method generally does not need to be released to the production environment.


8. Gui rendering

OnGUI (The work of user interface rendering will be performed in this step.)


9. End of frame

yield WaitForEndOfFrame (coroutine: end of frame)

暂时挂起直到关闭窗口

This coroutine will be executed after the current frame is completely over. :


10. Pausing (pause phase)

OnApplicationPause (application pause)

应用暂停时会调用此方法,取消暂停后会从FixedUpdate开始重新执行。


11. OnDisable/Enable (script is disabled/activated)

游戏物体被禁用 调用
脚本组件被禁止 调用
游戏物体被销毁 调用

  • call situation
    • game object is disabled
    • script component is destroyed
  • Call time, frequency and effect
    • Immediately called once when the calling conditions are met, used for state reset of some objects, resource recovery and cleanup

12. Decommissioning (exit stage)

OnDestroy (destroy)

场景或游戏结束/关闭 调用
脚本的删除 调用
挂载物体的删除 调用

  • call situation
    • end of scene or game
    • Stopping playback mode will terminate the application when
    • When the web view is closed
    • when the current script is removed
    • When the game object the current script is attached to is deleted
  • Call time, frequency and effect
    • It is called immediately when the calling conditions are met, and is used for the destruction of some game objects

OnApplicationQuit (program exits)

程序退出 调用

  • call situation
    • All game objects will call this function before the program exits
    • In the editor, when the user terminates playback
    • when the webview is closed
  • Call time, frequency and effect
    • Immediately called once when the call condition is met, used to handle some logic after the game exits

Summarize

After brushing the API, I found that I still have a lot of graphics rendering sequences and processes that I don’t understand; X
hopes that I can continue to update this article in the future, and try to understand everything!

Guess you like

Origin blog.csdn.net/weixin_46172181/article/details/126917927