当前位置:网站首页>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;
}
}
边栏推荐
- Introduction to asynchronous task capability of function calculation - task trigger de duplication
- Unity动画Animation Day05
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- Enter the width!
- 這幾年爆火的智能物聯網(AIoT),到底前景如何?
- 31年前的Beyond演唱会,是如何超清修复的?
- 找数字
- Unity预制件Prefab Day04
- MYSQL索引优化
- How did the beyond concert 31 years ago get super clean and repaired?
猜你喜欢
Redis sentinel mode realizes one master, two slave and three Sentinels
The performance of major mainstream programming languages is PK, and the results are unexpected
I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
Unity动画Animation Day05
flutter 报错 No MediaQuery widget ancestor found.
Redis 發布和訂閱
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
How to build a technical team that will bring down the company?
压力、焦虑还是抑郁? 正确诊断再治疗
Redis shares four cache modes
随机推荐
MYSQL索引优化
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
[local differential privacy and random response code implementation] differential privacy code implementation series (13)
Kubernets pod exists finalizers are always in terminating state
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Unity脚本API—Time类
c# 实现定义一套中间SQL可以跨库执行的SQL语句
The per capita savings of major cities in China have been released. Have you reached the standard?
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
2022 financial products that can be invested
一篇文章搞懂Go语言中的Context
.Net之延迟队列
Unity脚本API—Component组件
Redis publish and subscribe
Redis publier et s'abonner
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
TechSmith Camtasia studio 2022.0.2 screen recording software
Ffmpeg Visual Studio development (IV): audio decoding
hexadecimal