[GitHub]-Unity Timer

Source: Internet
Author: User

Https://github.com/akbiggs/UnityTimer#unity-timer

Run actions after a delay in Unity3d.

This library has been battle-tested and hardened throughout numerous projects, including the award-winning pitfall Planet.

Written by Alexander Biggs + Adam Robinson-yu.

installation

To get the latest release of Unitytimer, head through to the releases page and download the Timer.unitypackage file from the Latest release. Then if you had a Unity project open, you can open the. unitypackage file to install the scripts into your project.

Alternatively, if you like to live on the bleeding edge, you can clone this repository into your project ' s Assets folder. However, we don't guarantee this would give you a stable version.

Basic Example

The Unity Timer package provides the following method for creating timers:

<Summary>Register a new timer that should fire the event after a certain amount of timehas elapsed.</Summary>///<param  Name= "Duration" >the time to wait before the timer Should fire, in Seconds.</param> ///<param name=  "Oncomplete" >an action to fire when the timer Completes.</param>public static  Timer register (float duration,  Action oncomplete);            

The method is called like this:

Log "Hello World" after five seconds. Timer.register (5f, () = Debug.Log ("Hello World");  
Motivation

Out of the box, without this library, there is both main ways of handling timers in Unity:

    1. Use a coroutine with the Waitforseconds method.
    2. Store the time that your timer started in a private variable (e.g. startTime = Time.time ) and then check in an Update call if Time.time - startTime >= timerDuration .

The first method is verbose, forcing-refactor your code to use IEnumerator functions. Furthermore, it necessitates have access to a Monobehaviour instance to start the coroutine, meaning that solution would Not work in non-monobehaviour classes. Finally, there is no-prevent waitforseconds from being affected by changes to the time scale.

The second method is Error-prone, and hides away the actual game logic which is trying to express.

This library alleviates both of these concerns, making it easy-to-add an easy-to-read, expressive timer to any class in yo ur Unity project.

Features

Make a timer repeat by setting to isLooped true.

Call the player's jump method every the seconds. Timer.register (2f, player. Jump, islooped:true);

Cancel a timer after calling it.

Timer  Start () {   timer = timer.register (2f, () = Debug.Log ("You won ' t see the text if you press x." ));} if (Input.getkeydown (keycode.x)) {timer.cancel (Timer);}}    

Measure time by Realtimesincestartup instead of scaled game time by setting to useRealTime true.

Let's say you pause your game by setting the timescale to 0. Time.timescale = 0f; // ... Then set userealtime so this timer would still fire even though the game time isn ' t progressing. Timer.register (1f, this. Handlepausedgamestate, userealtime:true);  

Attach the timer to a monobehaviour so, the timer is destroyed when the Monobehaviour is.

Very often, a timer called from a monobehaviour would manipulate that behaviour's state. Thus, it is common practice to cancel the timer in the OnDestroy method of the Monobehaviour. We ' ve added a convenient extension method that attaches a Timer to a monobehaviour such that it'll automatically cancel The timer when the Monobehaviour is detected as null.

Publicclass coolmonobehaviour: monobehaviour {void start () {//use the Attachtimer extension method to create a timer that Is destroyed-//object is destroyed. this. Attachtimer (5f, () = {//If This code runs after the object is destroyed, a Nu ll reference would be thrown, //which could corrupt the game state. this.gameobject.transform.position = Vector3.zero;}); } void update () {//This code could destroy the object at any T Ime! if (Input.getkeydown (keycode.x)) {Gameobject.destroy (this.gameObject);}}}  

Update a value gradually over time using the onUpdate callback.

Change a color from white to red over the course of five seconds.   color.white;  5f; 255 * (secondselapsed/transitionduration), OnComplete: () = Debug.Log ("Color is now Red"); 

A number of other useful features is included!

    • Timer. Pause ()
    • Timer. Resume ()
    • Timer. Gettimeremaining ()
    • Timer. Getratiocomplete ()
    • Timer.isdone

A Test scene + script demoing all the features are included with the "the" in the Timer/Example folder.

Usage notes/caveats
    1. This library does does currently seem to work on the Hololens. We are looking to a solution for this.
    2. All timers is destroyed when changing scenes. This behaviour is typically desired, and it happens because timers be updated by a timercontroller so is also destroyed When the scene changes.  Note that as a result of this, creating a Timer when the scene was being closed, e.g. in an object ' s OnDestroy method, would result in a Unity error when the Timercontroller is spawned.

[GitHub]-Unity Timer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.