当前位置:网站首页>Unity script life cycle and execution sequence
Unity script life cycle and execution sequence
2022-06-30 04:49:00 【Answer-3】
Catalog
@( List of articles )
stay Unity in , A script can be understood as an instruction code attached to a game object to define the behavior of the game object . You must bind to a GameObject to start its lifecycle . Game objects can be understood as containers that can hold various components , All the components of the game object together determine the behavior of the object and the performance in the game .
Script life cycle
Unity Common inevitable events in scripts are shown in the following table
| name | trigger | purpose |
|---|---|---|
| Awake | Called when a script instance is created | Used for initialization of game objects , Be careful Awake The execution of is earlier than that of all scripts Start function |
| OnEnable | Called when an object becomes available or active | purpose |
| Start | Update The function is called before the first run | Used for initialization of game objects |
| Update | Once per frame | Used to update game scenes and status |
| FixedUpdate | Called once per fixed physical interval | Used to update the physical state |
| LateUpdate | Once per frame ( stay update Then call ) | Used to update game scenes and status , Camera related updates are usually placed here |
| OnGUI | Rendering and processing OnGUI event | purpose |
| OnDisable | Called when the current object is unavailable or inactive | purpose |
| OnDestroy | Called when the current object is destroyed | purpose |
Let's take a look at the call timing of these inevitable events with code
Create a new one C# Script , And add the following code , Then hang it on any GameObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTest : MonoBehaviour
{
private void Awake()
{
Debug.Log("Awake");
}
private void OnEnable()
{
Debug.Log("OnEnable");
}
// Start is called before the first frame update
void Start()
{
Debug.Log("Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update");
}
private void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
private void LateUpdate()
{
Debug.Log("LateUpdate");
}
private void OnGUI()
{
Debug.Log("OnGUI");
}
private void OnDisable()
{
Debug.Log("OnDisable");
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
}
}The print result is shown in the following figure :

You can find ,Awake, Start Functions are called once when the game object is created .
When you adjust the script's visible state during the game , Will call... Separately OnEnable, OnDisable function , and Awake and Start Will no longer call , That is, once the script is mounted ,Awake and Start There is and will only be executed once .
and Update, FixedUpdate, LateUpdate, OnGUI Function will be called many times during the game ( The number on the right side of the log window indicates the number of times the log information is printed ).
Finally, when the game object is destroyed , Will call in turn OnDisable, OnDestory function .
MonoBehavior Life cycle diagram
Here is a life cycle diagram drawn by a foreign friend

Script execution order
In game development , Inevitably, many scripts will be used , So how to determine the sequence of calls between different scripts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("start 1");
}
private void Awake()
{
Debug.Log("awake 1");
}
// Update is called once per frame
void Update()
{
Debug.Log("update 1");
}
} Add the above code to Test1, Test2 and Test3 Script ( Modify the printed log properly ), And mount to different game objects .
The order of mounting is to mount first Test3, Re mount Test2, Finally mount Test1
The printing result is shown in the figure below 
Print the result first Test1 Of , Re print Test2, Finally print Test3.
In fact, the execution order of the script is related to the sequence of attaching to the game object . The last execution that is mounted first , The last mounted is executed first ( If the reader has doubts , You can constantly adjust the mounting order of scripts , See whether the log printing is consistent with the above conclusion ).
It should be noted that , Regardless of the order in which multiple scripts are executed , But all the scripts Awake Functions must be better than all Start The function is executed first , be-all Start Functions must be better than all Update The function is executed first , Other ordered lifecycle functions are similar ( It can also be seen from the log information in the above figure ).
Custom execution order
Sometimes there may be such a need ,A Attribute instantiation in scripts may require B Properties in the script , So in A When the script attribute is instantiated , Must ensure B The script has been instantiated . Of course, we can do it by hanging it on A Script remount B Script to achieve . But in real development , Many scripts are used , It's hard to remember the order in which each script is mounted . therefore Unity Provides Script Execution Order Configuration item , To configure the execution order of multiple scripts .
Select a script file by clicking it in the project panel , The script details will appear in the property panel , Choose... In the upper right corner Execution Order..., Open the interface shown in the following figure

Click on “+” You can add scripts , Set it up order value ,order The smaller the value is, the earlier it is executed ,order The higher the value, the later the execution
边栏推荐
- Redis实现短信登入功能(一)传统的Session登入
- 【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
- Singapore must visit these scenic spots during the Spring Festival
- IO stream, buffer stream, automatic resource management
- Network high concurrency
- Mongodb learning
- Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
- 图的一些表示方式、邻居和度的介绍
- Static keyword
- The most comprehensive summary notes of redis foundation + advanced project in history
猜你喜欢

力扣589:N 叉树的前序遍历

Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)

SSL update method

Malignant bug: 1252 of unit MySQL export

HTC vive cosmos development - handle button event

Mongodb learning
![[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example](/img/d7/7bf43437edb87b69cdc5ae858f44e1.jpg)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example

Interprocess communication

Input / output and interrupt technology -- microcomputer Chapter 6 learning notes

一条命令运行rancher
随机推荐
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
力扣292周赛题解
Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
Efficiency test of adding and querying ArrayList and LinkedList
Arrays class
输入输出及中断技术——微机第六章学习笔记
為什麼win10開熱點後電腦沒有網絡?
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Differences between cookies and sessions
股票利益【非dp】
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
One interview question every day to talk about the process of TCP connection and disconnection
What to do when the alicloud SSL certificate expires
Malignant bug: 1252 of unit MySQL export
Threadlocal
Network high concurrency
What is SQL injection and how to avoid it?
[UAV] kinematic analysis from single propeller to four rotor UAV
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?