当前位置:网站首页>In unity, inherit the lifecycle of monobehavior game objects
In unity, inherit the lifecycle of monobehavior game objects
2022-06-09 02:38:00 【God of Luo Xiu】
In this article Unity Object lifecycle .
We know , Inherit monobehaviour Class , All of them should execute some event functions in a certain order , such as
awake, onenable, start, update etc. , If you want to Unity Related development work , These sequences must be mastered , To put it bluntly , Interview questions are required ! ha-ha
This flow chart is a screenshot of the official website

blog.csdnimg.cn/fbe3c826afac49f4b7379976cd3f9ccd.png)

As can be seen from the above flow chart , There are many event functions , Here we only record some commonly used code ,
1、Awake
Always in any Start This function is called before and after the prefab is instantiated. .( If the game object is inactive during startup , Will be called after activation Awake.) This function The entire cycle will only call once , And according to the introduction in brackets , Only after instantiation will .
2、OnEnable
stay Awake after , stay Start Previous call , This function may be executed many times , For example, in a script, you can use setActive(true), Then it will be transferred to the event function ,setActive(false) Will be called to OnDisable Time function , So it is equivalent to listening to the object setActive With .
3、Start
Only when the script instance is enabled , It will be called before the first frame update Start. It means that at the beginning of execution update Function before , This function must be called once , This function The entire cycle will only call once , It can be used as the constructor of a function , Initializing some game information can be done here .
4、FixedUpdate
Fixed frequency update update function , Generally used to update physical simulation , If you encounter a certain frame, it's too laggy , For example, at a certain frame GC Or complex rendering operations , It may cause this frame to get too laggy , So the next time we get here , This function will be executed many times , Ensure the correct frequency .
5、Update
Generally, some game logic is written here
6、LateUpdate
Generally, the direction and position of the camera are modified
7、PreCull
Call before the camera eliminates the scene . Culling determines which objects the camera can see . Just before calling out. OnPreCull. Here are a bunch of render related event function calls , Usually not very useful , Here is a simple record of
8、OnDisable
Pass... In the script setActive(false), Will call here
9、OnApplicationQuit
Call this function on all game objects before exiting the application . In the editor , When the user stops playing mode , Call function .
10、OnDestroy
After all frame updates have been completed for the last frame of the object's existence , Call this function
Small partner door as practice , You can create a new script ,Debug Under test , You can know the order , The code is as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FunOrderTest : MonoBehaviour {
GameObject Sphere;
void Awake() {
Debug.Log("Awake");
}
void OnEnable()
{
Debug.Log("OnEnable");
}
// Use this for initialization
void Start () {
Debug.Log("Start");
}
void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
// Update is called once per frame
void Update () {
Debug.Log("Update");
}
void LateUpdate()
{
Debug.Log("LateUpdate");
}
void OnPreCull()
{
Debug.Log("OnPreCull");
}
void OnGUI()
{
Debug.Log("OnGUI");
}
void OnApplicationQuit()
{
Debug.Log("OnApplicationQuit");
}
void OnDisable()
{
Debug.Log("OnDisable");
}
void OnDestroy()
{
Debug.Log("OnDestroy");
}
}
边栏推荐
- [untitled]
- Basic method for missing data filling (2) -- random forest (missforest) filling
- Deux facteurs importants affectant la défaillance de togglerowselection ()
- Dynamic programming / memo method n's K splitting n's maximum addend K splitting
- 接口测试系列——转转交易业务场景接口测试实践
- Greedy method / Huffman code
- FFmpeg的软、硬解码方式梳理
- Knowledge points of 2022 information security engineer examination: configuration and use of network security products
- 4426 divisible substring (enumeration + number theory)
- Greedy method / non 01 knapsack problem
猜你喜欢

Fight the high vision medical service of HKEx again, the gospel of short-sighted partners?

Navicat tool batch imports JSON format data to Doris

New textbook for self taught examination-p292

27 | discussion on high water level and leader epoch

S series · several postures for deleting folders

Problems and solutions in using renrenfast

Processes and threads

CVE-2020-3187

4426 divisible substring (enumeration + number theory)

Jsnpp框架的全链式语法初探
随机推荐
杰理之如何修改 ad_key 连接的引脚?【篇】
C # Fundamentals
Redis集群搭建
Summary of 14 anomaly detection methods
Kubernetes scheduling framework extension point
Self implemented web server
创建对象之 new 和 newInstance() 的区别
toggleRowSelection()失效的2個重要影響因素
21、ADS使用记录之E类功放设计(中)
Qt: get sender in slot (the control that triggers signal)
vins estimator ProcessImage
Jerry last IO_ Key how to use double keys? [chapter]
Export related knowledge
动态规划/备忘录方法 n的k拆分 n的最大加数k拆分
One month soft test | experience sharing of intermediate test for software designers (with learning materials)
【无标题】
[coding streaming] installation and use of SRS streaming media server
2022.6 download and installation tutorial of the latest version of mongodb
(10.3)【隐写缓解】隐写防护、隐写干扰、隐写检测
飞书要不要做生态?剖析第一家 All in 飞书的独立 SaaS 案例