当前位置:网站首页>十天学习Unity3D脚本(一)九个回调
十天学习Unity3D脚本(一)九个回调
2022-08-02 14:11:00 【哈哈哈啦啦啦123】
目录
二、持续执行的函数
一、游戏开始时调用一次的
1 Awake
描述:哟咻开始时第一个调用的回调函数,调用一次
特点:当脚本组件处于不可用时,依然执行
/// <summary>
/// 在游戏开始时执行一次
/// </summary>
private void Awake()
{
//若没有继承MonoBehaviour,打印信息可以debug.Log();
Debug.Log("Awake运行了");
Debug.LogWarning("警告");
Debug.LogError("出错了");
//继承了MonoBehaviour,可以print打印
print("打印");
}打印情况如下:

2 OnEnable
描述:在脚本组件设置为可用时调用一次
作用:需要当一个对象被激活(inspector打勾)时进行一些逻辑处理,可以写在本函数中
3. Start
特点:运行受到脚本组件是否勾选影响。
描述:在OnEnable之后调用,但在整个游戏过程中,只会执行一次;
它是在对象进行帧更新之前才会被执行
Awake、Start均在游戏开始时运行一次
-----Awake、OnEnable、Start运行顺序

多个脚本时的执行顺序,可以设置
Edit —> Project Settings —>Script Exception Order

二、持续执行的函数
1. FixedUpdate
描述:每个一定的时间间隔执行一次(可以设置),默认是0.02秒
用于进行物理相关的更新,如碰撞检测
设置间隔方法:
Edit---Project Settings----Time---Fixed Timestep

2. Update
描述:每帧直行一次
作用:用于处理游戏核心逻辑更新
问题:Update和FixedUpdate哪个执行的次数多

---实现小功能
方块向x轴正方向移动,每帧移动0.02米
void Update()
{
//Debug.Log("Update运行了");
transform.position += new Vector3(0.01f, 0, 0);
}3. LateUpdate
作用:摄像机位置更新相关内容
/// <summary>
/// 每帧执行一次(适用于一个物体执行后跟随)
/// </summary>
private void LateUpdate()
{
Debug.Log("LateUpdate");
}4. OnGUI
作用:用于渲染旧版UI,每帧大约执行两次
三、收尾
1. OnDisable
描述:依附的GameObject对象每次失活时被调用(对象被销毁时也会被调用)
作用:需要当一个对象失活时 进行一些逻辑处理,就可以写在本函数中
2. OnDestroy
作用:对象被销毁时被调用(依附的GameObject对象被删除时),当删除的时候调用,销毁前也会执行一次OnDisable
生命周期函数介绍
我们需要利用Unity的生命周期的规则来执行游戏逻辑
所有继承MonoBehavior的脚本 最终都会挂载到GameObject游戏对象上
生命周期函数就是该脚本对象依附的GameObject对象从出生到消亡整个生命周期中
会通过反射自动调用的一些特殊函数

边栏推荐
- 为vscode配置clangd
- 轻量化AlphaPose
- 二叉树创建之层次法入门详解
- 【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
- STM32LL library use - SPI communication
- 第三十三章:图的基本概念与性质
- 项目:数据库表的梳理
- Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
- Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
- In-depth understanding of Golang's Map
猜你喜欢
随机推荐
General code for pytorch model to libtorch and onnx format
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
第三十章:普通树的存储和遍历
pygame image rotate continuously
Fast advanced TypeScript
pygame绘制弧线
How to solve Win11 without local users and groups
Mapreduce环境详细搭建和案例实现
5. Transaction management
总结计算机网络超全面试题
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
Win10安装了固态硬盘还是有明显卡顿怎么办?
win10 system update error code 0x80244022 how to do
二叉排序树与 set、map
软件测试基础知识(背)
第二十七章:时间复杂度与优化
Installation and configuration of Spark and related ecological components - quick recall
Compilation error D8021: Invalid numeric argument '/Wextra' cl command line error d8021 invalid numeric argument '/Wextra'
Win10 computer can't read U disk?Don't recognize U disk how to solve?
How to update Win11 sound card driver?Win11 sound card driver update method










