当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

大神详解开源 BUFF 增益攻略丨直播

AI has surpassed Dr. CS in question making?

numpy笔记

Helix Swarm中文包发布,Perforce进一步提升中国用户体验
![[Dalian University of technology] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination

Flutter reports an error no mediaquery widget ancestor found

They are all talking about Devops. Do you really understand it?

Preliminary exploration of flask: WSGI

【大连理工大学】考研初试复试资料分享

MySQL学习笔记——数据类型(2)
随机推荐
Flutter reports an error no mediaquery widget ancestor found
数据湖治理:优势、挑战和入门
Exploration and practice of eventbridge in the field of SaaS enterprise integration
Logstash~Logstash配置(logstash.yml)详解
Weibo and Huya advance into interest communities: different paths for peers
夜天之书 #53 Apache 开源社群的“石头汤”
大神详解开源 BUFF 增益攻略丨直播
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
小数,指数
An article learns variables in go language
What is the future of the booming intelligent Internet of things (aiot) in recent years?
LeetCode 58. 最后一个单词的长度
Helix swarm Chinese package is released, and perforce further improves the user experience in China
暑期复习,一定要避免踩这些坑!
MySQL学习笔记——数据类型(2)
.Net之延迟队列
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Enter the width!
进制乱炖
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture