Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of millisecond delay callback by Unity

Shulou Source: shulou.com Published: 2022-06-03 09:52:24 11月15日 Update

This article mainly shows you "how to achieve millisecond delay callback function in Unity". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to achieve millisecond delay callback function in Unity".

API

1: Time.deltaTime

In fact, it is the time of execution per frame.

Function realization

Simply talk about the implementation of the function, the source code will be posted directly below.

Each new task (callback) records the time the task was created and the delay, as well as its own event callback. Determine whether the time of the current frame is greater than that created by each frame (time of task + time of delay)

Code using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class TickManager: MonoBehaviour {/ / Start is called before the first frame update public int NOW; private List _ ticks = new List () Private void Update () {/ / Test-manually create a callback if (Input.GetKeyDown (KeyCode.A)) after 5 seconds {Tick tick = new Tick (()) = > {Debug.Log ("Task execution");}, 5000Now); _ ticks.Add (tick) } / / the millisecond time used in each frame uint deltaTime = (uint) (Time.deltaTime * 1000); / / traverses to determine whether the tasks in the collection execute for (int I = 0; I

< deltaTime; i++) { Debug.Log("帧数 " + NOW); for (int j = 0; j < _ticks.Count; j++) { _ticks[j].OnTick(NOW); } ++NOW; } } class Tick { //创建任务的时间 public int currentTime { get; set; } //需要延迟的时间 public int delayTime { get; set; } //延迟后的回调事件 public Action action { get; set; } //构造函数--初始化 public Tick(Action ac, int del, int now) { action = ac; delayTime = del; currentTime = now; } //判断该任务是否执行 public void OnTick(int now) { if (now >

= (currentTime + delayTime) {action ();} else {Debug.Log ("time not yet" + now);} above is all the content of the article "how to achieve millisecond delay callback in Unity". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Time function task content article learning help delay event code actually in fact manual easy to understand more organization source code knowledge editing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Microsoft OPPO Reno macOS