当前位置:网站首页>十天学习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对象从出生到消亡整个生命周期中
会通过反射自动调用的一些特殊函数

边栏推荐
- What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
- flink+sklearn——使用jpmml实现flink上的机器学习模型部署
- Based on the least squares linear regression equation coefficient estimation
- Yolov5 official code reading - prior to transmission
- MATLAB绘制平面填充图入门详解
- What should I do if I install a solid-state drive in Win10 and still have obvious lags?
- Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
- Redis的线程模型
- [STM32 Learning 1] Basic knowledge and concepts are clear
- MATLAB图形加标注的基本方法入门简介
猜你喜欢

Win11 system cannot find dll file how to fix

Codeforces Round #605 (Div. 3)

轻量化AlphaPose

C语言函数参数传递模式入门详解

Win10 cannot directly use photo viewer to open the picture

二叉树创建之层次法入门详解

Win11 computer off for a period of time without operating network how to solve

Detailed introduction to the hierarchical method of binary tree creation

Redis common interview questions

word方框怎么打勾?
随机推荐
Summarize computer network super comprehensive test questions
1. Development community homepage, register
The SSE instructions into ARM NEON
【STM32学习1】基础知识与概念明晰
MATLAB绘图命令fimplicit绘制隐函数图形入门详解
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
Configure clangd for vscode
奇技淫巧-位运算
Letter combination of LeetCode2 phone number
Codeforces Round #624 (Div. 3)
1.开发社区首页,注册
Mysql lock
专硕与学硕
C语言函数参数传递模式入门详解
How to update Win11 sound card driver?Win11 sound card driver update method
jest test, component test
Doubled and sparse tables
LeetCode 2344. 使数组可以被整除的最少删除次数 最大公约数
4.发布帖子,评论帖子
动态数组-vector
