当前位置:网站首页>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
边栏推荐
- Software digital signature certificate
- 力扣2049:统计最高分的节点数目
- 史上最全的Redis基础+进阶项目实战总结笔记
- What is multimodal interaction?
- 0 foundation starts self-study unit notes control direction becomes larger
- 【Paper】2017_ Distributed control for high-speed trains movements
- Implementation of one interview question one distributed lock every day
- 破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
- A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river
- Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
猜你喜欢
【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
【Paper】2017_ Distributed control for high-speed trains movements
Software digital signature certificate
Transport layer protocol tcp/udp
Introduction to system programming
史上最全的Redis基础+进阶项目实战总结笔记
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
This connection is not a private connection this website may be pretending to steal your personal or financial information
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
随机推荐
深度学习------不同方法实现Inception-10
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
Wildcard SSL certificate issuing time
Learn about threads
2021-03-16
Webots learning notes
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Qos(Quality of Service)
【Paper】2017_ Distributed control for high-speed trains movements
Unreal 4 learning notes - data storage using blueprints
SCM learning notes: interrupt learning
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
Summary of the reasons why transactional doesn't work
Check London attractions suitable for parents and children in winter vacation
National Museum of Singapore - give you spiritual and physical satisfaction
Differences between beanfactory and factorybean
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
Implementation of one interview question one distributed lock every day
图的一些表示方式、邻居和度的介绍