当前位置:网站首页>Unity - Common APIs (continued time class)
Unity - Common APIs (continued time class)
2022-07-18 21:19:00 【Small digital media members】
I've sorted it out today Time class !
+ An interesting understanding API Graph
( This picture is inspired by How to understand API,API How it works _ Brother Renjie's blog -CSDN Blog _api)

Catalog
practice : Use Text Make a countdown prefab
Time
Time.time In seconds , From the beginning of the game
Time.deltaTime Time consumed per frame ( Time interval of each frame )
Public void Update()// Rendering update
{ Move at a constant speed in the rendered frame 、 rotate
// Execute once per rendered frame , Spin one degree
// Multiple frames —>1 Second rotation speed is fast hope 1 Frame rotation is small
// Fewer frames —>1 Second rotation speed is slow hope 1 Frame rotation is large
This.transform.Rotate(0,1*Time.deltaTime,0)
}
Rotation speed * Time consumed per frame , It can ensure that the rotation speed is not affected by rendering
perhaps
public void FixedUpdate()// Physical update
{
this.transform.Rotate(0,1,0);
)
Time.timeScale Every frame interval affected by scaling
Time.timeScale=0 Game pause Time.timeScale=1 Keep playing
The object is in FixedUpdate() Move , spot “ Pause ”、“ continue ” All good.
( Even if Time.timeScale Not in the FixedUpdate() in )
because FixedUpdate() Execute once at a fixed time , It has nothing to do with rendering , suffer Time.timeScale influence
stay Update() It's hard to work in , because Update() When rendering a scene , Not subject to Time.timeScale influence , but Time.deltaTime Will receive Time.timeScale influence
demand : After the game pauses , Individual objects are not affected
stay Updete In doing
use this.transform.Rotatel(0,1*Time.unscaledDeltaTime,0)
Time.unscaledDeltaTime Every frame interval unaffected by scaling
practice : Use Text Make a countdown prefab
from 02:00 Start , Last 10 The second font is red , Time is 00:00 Stop after
Last 10 The second font is red —>
Hierachy Right click —>UI—>Text

quote using UnityEngine.UI;
If(second<=0)
txtTimer.color=Color.red;
Time is 00:00 Stop after —>
If(second<=0)
CancelInvoke(“Timer”)// Cancel calling a method
Ideas :
1. Find component references
2. Defining variables : second
3.120—>02:00
119—>01:59
4. Modify the text
5. How to modify once per second
* stay Update In the method executed per frame , Individual statements are executed once at a specified interval
Method 1 :Time.time
private float nextTime = 1;// Next modification time
private void Update()
{
if (Time.time >= nextTime)
{
second--;
txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
if (second <= 10)
txtTimer.color = Color.red;
// Set the next modification time
nextTime = Time.time + 1;// Every other second
}
}
Suitable for firing bullets , Press the left button to launch , Then accumulate strength ( Waiting time ) Limit the frequency
Method 2 :Time.deltaTime
private float totalTime;// Cumulative The interval of each frame
private void Update()
{
totalTime += Time.deltaTime;
if (totalTime >= 1)
{
second--;
txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
totalTime = 0;
}
}
Wait before you do
Method 3 :InvokeRepeating()
// Repeated calls to ( Name of the method to be executed , The first execution time , Every execution interval )
void Start()
{
InvokeRepeating("Timer", 1, 1);
}
private void Timer()
{
second--;
txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
}
Invoke( The method to be carried out , The time when the call started )
Code implementation :
using System.Collections;
using System.Collections.Generic;
using UnityEngine ;
using UnityEngine.UI;
public class TimeDemo : MonoBehaviour
{
private Text txtTimer;
public int second = 120;
private float nextTime = 1;// Next modification time
void Start()
{
txtTimer = this.GetComponent<Text>();
}
private void Update()
{
Timer1();
}
void Timer1()
{
if (Time.time >= nextTime)
{
second--;
txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
if (second <= 10)
txtTimer.color = Color.red;
// Set the next modification time
nextTime = Time.time + 1;
if(second<=0)
{
CancelInvoke("Timer");
}
}
}
}边栏推荐
- (手工)【sqli-labs54-57】限制注入次数:联合注入、报错回显、GET注入
- Discussion on ble Bluetooth battery service
- 摸清企业选址动机及需求,高效开展招商引资工作
- What is industrial planning? How to make industrial planning for industrial parks
- Based on servlet project -- blog system
- VMware 恢复快照出现 无法创建 5040 MB 的匿名分页文件: 系统资源不足,无法完成请求的服务
- Data statistical analysis cases (comparative analysis, sales comparison analysis, year-on-year, double coordinate diagram, month on month, shift, contribution analysis (Pareto Law), differentiation an
- High numbers | calculation of double integral 1 | high numbers | handwritten notes
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping quantile of dataframe data
- putchar()
猜你喜欢

激活navicat提示rsa public key not find的问题

Tableau JDBC连接GraphDB

Sword finger offer 57 - ii Continuous positive sequence with sum s

Skywalking's load balancing and automatic capacity expansion practice for grpc

High numbers | calculation of double integral 1 | high numbers | handwritten notes

剑指 Offer 55 - I. 二叉树的深度

General Administration of market supervision: 15 batches of ice cream products such as Pudding Ice cream are unqualified

Sword finger offer 53 - I. find the number I in the sorted array

Sword finger offer 55 - I. depth of binary tree

What is industrial planning? How to make industrial planning for industrial parks
随机推荐
R语言使用data.table包对dataframe行数据进行排序(基于多字段、变量进行数据行排序,不重新排序实际的数据变化)、并计算排序后分组的累积加和值
Sword finger offer 53 - I. find the number I in the sorted array
js 加快播放视频速度
MySQL common commands for viewing database performance
Electron安装配置
初始 Redis(认识Redis以及常见命令)
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping quantile of dataframe data
Machine learning BP (back propagation) neural network
产业园区如何做好精细化运营管理
剑指 Offer 53 - II. 0~n-1中缺失的数字
Win10 如何将FAT32格式磁盘不用格式化无损转化为NFTS格式
Uniapp request request encapsulation method
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组分位数(quantile)
剑指 Offer 55 - II. 平衡二叉树
动态添加路由刷新页面会出现白屏
I2C communication protocol realizes data display on OLED display screen
How to do fine operation management in industrial parks
[enterprise wechat self built application development]
Apache log related
Offre de doigts 57. Et deux chiffres pour s