Giter VIP home page Giter VIP logo

chronity's Introduction

chronity

Release OpenUPM LISENCE Compatibility GitHub Repo stars

โŒ› A library for running functions after a delay in Unity.

This package is a fork of the UnityTimer made by akbiggs.

To get started, read the docs or follow this README file.

Table of Contents

Getting Started ๐Ÿš€

Installation

Please follow the instructions in the manual about Installing a package from a Git URL.

Use the following URL to install the latest version of the package: https://github.com/SushiWaUmai/chronity.git?path=/com.sushiwaumai.chronity

Quick Start ๐ŸŽ“

This is how to call a function after a delay in Chronity.

// Log "Hello World" after five seconds.

Timer.Register(5f, () => Debug.Log("Hello World"));

Features โœจ

Make a timer repeat by setting isLooped to true.

// Log "Hello World" every 10 seconds.

Timer.Register(10f, () => Debug.Log("Hello World"), isLooped: true);

Cancel a timer after calling it.

Timer timer;

private void Start()
{
    timer = Timer.Register(2f, () => Debug.Log("You won't see this text if you press X."));
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.X)) 
    {
        timer.Cancel();
    }
}

Attach the timer to a MonoBehaviour so that the timer is destroyed when the MonoBehaviour is.

Very often, a timer called from a MonoBehaviour will 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 will automatically cancel the timer when the MonoBehaviour is detected as null.

public class CoolMonoBehaviour : MonoBehaviour
{
    private void Start() 
    {
        // Use the AttachTimer extension method to create a timer that is destroyed when this
        // object is destroyed.
        this.AttachTimer(5f, () => {
      
            // If this code runs after the object is destroyed, a null reference will be thrown,
            // which could corrupt game state.
            this.gameObject.transform.position = Vector3.zero;
        });
    }
   
    private void Update() 
    {
        // This code could destroy the object at any time!
        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 color = Color.white;
float transitionDuration = 5f;

Timer.Register(transitionDuration,
   onUpdate: secondsElapsed => color.r = 255 * (secondsElapsed / transitionDuration),
   onComplete: () => Debug.Log("Color is now red"));

Make a timer presist through scene changes using the cancelOnSceneChange parameter.

// Make a timer that will persist through scene changes.
Timer.Register(5f, () => Debug.Log("Hello World"), cancelOnSceneChange: false);

// Change scene from another script 

// Logs "Hello World" after 5 seconds.

Make a timer run in the editor by using the EditorTimer class.

// Logs "Hello World" after 5 seconds in the editor

EditorTimer.Register(5, () => Debug.Log("Hello World"));

A number of other useful features are included!

  • time.Pause()
  • timer.Resume()
  • timer.TimePassed
  • timer.TimeRemaining
  • timer.RatioComplete
  • timer.IsDone

License ๐Ÿ“œ

Code released under the MIT License.

chronity's People

Contributors

sushiwaumai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.