当前位置:网站首页>Unity中,继承MonoBehaviour游戏对象的生命周期
Unity中,继承MonoBehaviour游戏对象的生命周期
2022-06-09 02:36:00 【罗修之神】
本文介绍下Unity对象的生命周期。
我们知道,继承monobehaviour的类,都要按照一定的顺序执行一些事件函数,比如
awake, onenable, start, update等,如果要从事Unity相关的开发工作,这些顺序是必须要掌握的,说白了,面试必考题!哈哈

blog.csdnimg.cn/fbe3c826afac49f4b7379976cd3f9ccd.png)

从上述流程图中可以看出,有好多的事件函数,这里只记录一些写代码比较常用的,
1、Awake
始终在任何 Start 函数之前并在实例化预制件之后调用此函数。(如果游戏对象在启动期间处于非活动状态,则在激活之后才会调用 Awake。)这个函数整个周期只会调用一次,而且按照括号里面的介绍,只有在实例化之后才会调用。
2、OnEnable
在Awake之后,在Start之前调用,这个函数有可能会被执行多次,比如在脚本中通过setActive(true),那么就会调到这个事件函数里面来,setActive(false)会调用到OnDisable时间函数中去,所以相当于是监听该对象的setActive用的。
3、Start
仅当启用脚本实例后,才会在第一次帧更新之前调用 Start。意思就是在开始执行update函数之前,一定会调用一次这个函数,这个函数整个周期只会调用一次,可以作为函数的构造函数使用,初始化一些游戏信息可以在这里面做。
4、FixedUpdate
固定频率更新的update函数,一般用作更新物理模拟,如果遇到某一帧很卡,比如在某一帧进行GC了或者复杂的渲染操作,有可能会导致这一帧很卡,那么在下一次执行到这里的时候,会执行多次这个函数,保证其正确的频率。
5、Update
一般些游戏逻辑写在这里面
6、LateUpdate
一般做相机方向及其位置修改
7、PreCull
在摄像机剔除场景之前调用。剔除操作将确定摄像机可以看到哪些对象。正好在进行剔除之前调用 OnPreCull。这里有一堆渲染相关的事件函数调用,通常不怎么用得到,在这里简单额记录下
8、OnDisable
在脚本中通过setActive(false),会调用到这里
9、OnApplicationQuit
在退出应用程序之前在所有游戏对象上调用此函数。在编辑器中,用户停止播放模式时,调用函数。
10、OnDestroy
对象存在的最后一帧完成所有帧更新之后,调用此函数
小伙伴门当作练习,可以新建一个脚本,Debug测试下,就可以知道顺序了,代码如下
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");
}
}
边栏推荐
- CVE-2022-30525漏洞複現
- Jerry: if the user doesn't need to use all the keys, how should other keys be set? [chapter]
- 得物技术埋点自动化验证的探索和最佳实践
- The latest investment report of animoca brands: 340+ investment projects, with a total investment of US $1.5 billion and a total reserve capital of more than 5billion
- 力扣解法汇总1037-有效的回旋镖
- CVE-2020-3187
- [suctf 2018]multisql MySQL preprocessing
- Fight the high vision medical service of HKEx again, the gospel of short-sighted partners?
- Jericho's several descriptions on SPI host configuration parameters]
- 说说你印象中比较深刻的 Bug
猜你喜欢

21. Class E power amplifier design of ads usage record (medium)

How does the technical leader bring down a team?

Blue Bridge Cup_ Frog date_ Extended Euclid

【HomeAssistant外网访问(cpolar)】

Tiflash source code reading (III) design and implementation analysis of tiflash deltatree storage engine - Part 1

How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)

20、ADS使用记录之E类功放设计(上)

Binary tree chain structure

Go to MFC from Win32

得物技术埋点自动化验证的探索和最佳实践
随机推荐
[coding streaming] installation and use of SRS streaming media server
MySQL data type enum enumeration type
【HomeAssistant外网访问(cpolar)】
20. Class E power amplifier design for ads usage record (Part 1)
贪心法/哈夫曼编码
vins feature_ track
Jerry: if the user doesn't need to use all the keys, how should other keys be set? [chapter]
动态规划/n的k拆分 n的最大加数k拆分
杰理之关于 SPI 主机配置参数的几个说明:【篇】
Indonesia widya robotics and Huawei cloud make the safety of construction sites visible
Embracing out of hospital prescription drugs, Internet medicine should also "get rid of virtual reality"?
Jedis工具类、适配单个redis以及redis集群
Go技术日报(2022-06-07)——go程序员开发效率神器汇总
2022年信息安全工程师考试知识点:网络安全产品的配置与使用
21、ADS使用记录之E类功放设计(中)
NFT chain game system development | defi+nft technology construction
Modbus RTU communication routine between Delta Eh3 series PLC and thermostat
Go to MFC from Win32
FRP construction
(10.3)【隐写缓解】隐写防护、隐写干扰、隐写检测