I have done the system, separate stand-alone system (2005), CS System (2010), Real-time system (2015), each system has a "delay" function: scheduled scheduling; This blog is about the delay function in real-time systems (based on the Unity3d game engine).
In Unity3d, the new script inherits Monobehaviour by default, where the update function is automatically dispatched by the engine, such as on my computer, the update function is called with a period of 0.012s.
So in Unity3d, is it only possible to implement the delay function through the update function? Is there any other implementation?
How do I make an interface show only 1 seconds and then perform other actions ~
For example, the game's victory screen only lets him display one second and then perform other actions.
The answer is that there are several delay functions (timers) One by one listed below:
Delay function |
Whether to inherit Monobehaviour |
Note |
Update function |
Is |
Functions used in conjunction with: Timer.timer |
Invoke |
Is |
Functions used in conjunction with: Cancelinvoke Invokerepeating Isinvoking |
Coroutine |
Is |
Functions used in conjunction with: Startcoroutine Stopallcoroutines Stopcoroutine |
Dotween (Hotween v2) |
Whether |
Assetstore Business License |
Visiontimer.vp_timer |
Whether |
Assetstore Business License |
In the following example, in the Showa function, the delay 5s calls the SHOWB function, and the code looks at the similarities and differences of each implementation method.
For convenience, I only intercept some of the key code, whether inherit Monobehaviour and so on self-add.
- Update function Implementation Timer
float M_timer = 0; void Update () {m_timer + = Time.time; if (M_timer >= 5) {SHOWB ();
M_timer = 0; }
} private void Showb () {
}
The
update function implements the timer, almost learning unity3d required lessons. However, in the actual combat project need to maintain m_timer this variable, and from the design point of view, the code is somewhat untidy.
- Invoke function Implementation Timer
Gamecontroller.get (). Cancelinvoke ();
Gamecontroller.get (). Invoke ("Showb", 5f);
private void Showb ()
{
Game. Showcardinturn ();
}
invoke is a function provided by monobehaviour that can delay the invocation of a function, and can control the stop of the scheduler through Cancelinvoke. However, invoke is in effect at run time, for SHOWB function, it is easy to lead to the code review phase was killed-after the function is deleted, compilation will not error, you may mistakenly think that this function is an obsolete function.
- Dotween Implementing Timers
Dotween Animation Plug-in, is in the Unity 4.6 era, in order to make up for Ugui non-animated script of congenital and cloud business plug-in, of course, the Rain Pine Momo "Ugui Institute of the interface Use Dotween (vii)" On the popularization of dotween work.
private void ShowA () {gameobject go;
Go.transform.DOMoveZ (0.1f, 5f). OnComplete (New Tweencallback (SHOWB)); } private void Showb () {
}
The main function of Dotween is not to do delay scheduling, but it has this function, and the thought is very cool.
- Vp_timer Implementing Timers
Vp_timer.handle Timer = new Vp_timer.handle ();
Vp_timer.in (5, new vp_timer.callback () =
{
Timer.cancel ();
SHOWB ();
}), Timer);
Vp_timer is one afternoon, in the online search, inadvertently found, its concise usage, really let a person in front of a bright. The current project, involving more than 10 places, greatly facilitates the implementation of the delay function, very good. It also saves a lot of time for the project.
Thank you for the popularity of this plugin blogger: "Unity clock Timer plugin--vision timer source analysis one ."
The above several methods, in the actual combat Unity3d project, are involved, I myself use more is dotween and Vp_timer, these 2 do not rely on inheritance monobehaviour, relatively flexible, and it is a commercial project, encapsulation or quite to force.
If you have other ways to achieve, but also welcome to share, we jointly improve and progress.
Reprint please indicate to the Unity3d project Practical notes (5): Several implementation of delay function
Unity3d Project Practical Notes (5): Several implementations of delay function