当前位置:网站首页>Unity script lifecycle day02
Unity script lifecycle day02
2022-07-04 15:21:00 【@Night Charm】
What is a script lifecycle ?
Unity The process of script from wake-up to destruction .
news : When certain conditions are met Unity Functions automatically called by the engine .
The chart is as follows :

Initial stage :
1、Awake Wake up the :
Called immediately when the object is loaded 1 Time ;
It is often used to initialize before the game starts , It can be judged that when certain conditions are met, execute this script this.enable = true
2、OnEnable When available : Called whenever the script object is enabled .
3、Start Start :
Called when the object is loaded and the script object is enabled 1 Time .
It is often used to initialize data or game logic , The execution time is later than Awake.
Physical stage :
1、FixedUpdate Fixed update : When the script is enabled , Fixed time called , Suitable for physical operation of game objects , For example, mobile, etc . Set update frequency :“Edit”--> "Project Setting" --> "Time" --> "Fixed Timestep" value , The default is 0.02.
2、OnCollisionXXX Collision : When the collision conditions are met, call .
3、OnTriggerXXX Trigger : Call... When the trigger condition is met .
4、OnMouseEnter Mouse migration : Move the mouse into the current Collider Called when the .
5、OnMouseOver Mouse over : The mouse passes through the current Collider Called when the .
6、OnMouseExit Mouse away : The mouse leaves the current Collider Called when the .
7、OnMouseDown The mouse click : Mouse down the current Collider Called when the .
8、OnMouseUp The mouse is raised : The mouse is in the current Collider Call... When lifting up .
The game logic :
1、Update to update :
When the script is enabled , Call every time you render a scene , The frequency is related to the performance of the device and the amount of rendering .
2、LateUpdate Delay update :
stay Update After the function is called, execute , Suitable for following logic .
Scene rendering :
1、OnBecameVisible When visible :
When Mesh Renderer Called when visible on any camera .
2、OnBecameInvisible When invisible :
When Mesh Renderer Called when not visible on any camera .
Closing phase :
1、OnDisable When not available :
This function is called when the object becomes unavailable or the attached game object is inactive .
2、OnDestroy When destroyed :
Called when the script is destroyed or the attached game object is destroyed .
3、OnApplicationQuit When the program ends :
Called when the application exits .
The code is as follows :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Script life cycle / Inevitable events / news Message
/// </summary>
public class Lifecycle : MonoBehaviour
{
// Script :.cs Text file for Class file
// Attach to game objects , Code that defines game object behavior instructions
// Serialize fields effect : Show private variables in the editor
[SerializeField]
private int a = 100 ;
// effect : Hide fields in the editor
[HideInInspector]
public float b;
[Range(0,100)]
public int c;
// attribute : Cannot display in editor , Usually it is not written in the script
public int A
{
get { return this.a; }
set { this.a = value; }
}
public Lifecycle()
{
Debug.Log(" Constructors ");
// Don't write constructors in scripts
// Cannot access main thread members in child threads
//b = Time.time;
}
//*********** Initial stage ****************
// Execution opportunity : Create game objects --> Execute now 1 Time ( Before start)
// effect : initialization
private void Awake()
{
Debug.Log("Awark-"+Time.time+"-"+this.name);
}
// Execution opportunity : Create game objects -> Script enabled -> To perform (1 Time )
// effect : initialization
private void Start()
{
Debug.Log("Start-"+Time.time+"-"+this.name);
}
//**************** Physical stage *****************
// Execution opportunity : At regular intervals ( Probably 0.02s) perform 1 Time .( Time can be modified )
// Applicability : Suitable for physical operation of objects ( Move 、 rotate ......), Not affected by rendering
private void FixedUpdate()
{
// Rendering time is not fixed ( The amount of rendering per frame is different 、 The performance of the machine is different )
//Debug.Log(Time.time);
}
private void OnMouseEnter()
{
Debug.Log("OnMouseEnter");
}
private void OnMouseDown()
{
Debug.Log("OnMouseDown");
}
//************* The game logic *****************
// Execution opportunity : Rendered frame execution , Execution interval is not fixed
// Applicability : Dealing with game logic
private void Update()
{
// Move forward 1m
// Check the program execution of a frame
// Start debugging F5 --> Running scenario --> Pause --> Add breakpoints to the lines that may go wrong --> Single frame operation --> debugging …… --> Stop debugging Shift + F5
int a = 1;
int b = 2;
int c = a + b;
}
}
边栏推荐
- Halcon knowledge: NCC_ Model template matching
- Unity脚本生命周期 Day02
- unity update 协程_Unity 协程的原理
- Ffmpeg Visual Studio development (IV): audio decoding
- Shell programming basics
- PXE network
- What are the concepts of union, intersection, difference and complement?
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
- 谈SaaS下如何迅速部署应用软件
猜你喜欢

How did the beyond concert 31 years ago get super clean and repaired?

lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04

Weibo and Huya advance into interest communities: different paths for peers

Deep learning network regularization

Numpy notes

go-zero微服务实战系列(九、极致优化秒杀性能)

Is BigDecimal safe to calculate the amount? Look at these five pits~~

Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)

Redis 发布和订阅

31年前的Beyond演唱会,是如何超清修复的?
随机推荐
大神详解开源 BUFF 增益攻略丨直播
TechSmith Camtasia studio 2022.0.2 screen recording software
数据湖治理:优势、挑战和入门
Optimization method of deep learning neural network
Redis 解决事务冲突之乐观锁和悲观锁
Preliminary exploration of flask: WSGI
hexadecimal
输入宽度!
Unity动画Animation Day05
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
.Net之延迟队列
都在说DevOps,你真正了解它吗?
开源人张亮的 17 年成长路线,热爱才能坚持
Implementation of web chat room
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
unity update 协程_Unity 协程的原理
找数字
Shell 编程基础
MySQL学习笔记——数据类型(数值类型)
They are all talking about Devops. Do you really understand it?