https://github.com/ugnmura/chronity
:hourglass: Library for running functions after a delay by creating timers in Unity3D.
https://github.com/ugnmura/chronity
csharp game-development game-programming gamedev openupm openupm-cli timer unity unity3d
Last synced: 4 months ago
JSON representation
:hourglass: Library for running functions after a delay by creating timers in Unity3D.
- Host: GitHub
- URL: https://github.com/ugnmura/chronity
- Owner: ugnmura
- License: mit
- Created: 2021-09-10T05:42:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-25T21:51:54.000Z (over 2 years ago)
- Last Synced: 2025-04-14T01:36:47.285Z (10 months ago)
- Topics: csharp, game-development, game-programming, gamedev, openupm, openupm-cli, timer, unity, unity3d
- Language: C#
- Homepage: https://sushiwaumai.github.io/chronity/
- Size: 2.5 MB
- Stars: 56
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://sushiwaumai.github.io/Chronity)
[](https://github.com/SushiWaUmai/Chronity/releases)
[](https://openupm.com/packages/com.sushiwaumai.chronity/)
[](https://github.com/SushiWaUmai/Chronity/blob/main/LICENSE)
[](https://unity3d.com/get-unity/download/archive)
[](https://github.com/SushiWaUmai/Chronity/stargazers)
:hourglass: A library for running functions after a delay in Unity.
This package is a fork of the [UnityTimer](https://github.com/akbiggs/UnityTimer) made by [akbiggs](https://github.com/akbiggs).
To get started, read the [docs](https://sushiwaumai.github.io/chronity) or follow this [README](README.md) file.
## Table of Contents
- [Getting Started](https://github.com/SushiWaUmai/chronity#getting-started-rocket)
- [Installation](https://github.com/SushiWaUmai/chronity#installation)
- [Quick Start](https://github.com/SushiWaUmai/chronity#quick-start-mortar_board)
- [Features](https://github.com/SushiWaUmai/chronity#features-sparkles)
- [License](https://github.com/SushiWaUmai/chronity#license-scroll)
## Getting Started :rocket:
### Installation
Please follow the instructions in the manual about [Installing a package from a Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html).
Use the following URL to install the latest version of the package:
https://github.com/SushiWaUmai/chronity.git?path=/com.sushiwaumai.chronity
### Quick Start :mortar_board:
**This is how to call a function after a delay in Chronity.**
```c#
// Log "Hello World" after five seconds.
Timer.Register(5f, () => Debug.Log("Hello World"));
```
## Features :sparkles:
**Make a timer repeat by setting `isLooped` to true.**
```c#
// Log "Hello World" every 10 seconds.
Timer.Register(10f, () => Debug.Log("Hello World"), isLooped: true);
```
**Cancel a timer after calling it.**
```c#
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.
```c#
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.**
```c#
// 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.**
```c#
// 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.**
```c#
// 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 :scroll:
Code released under [the MIT License](https://github.com/SushiWaUmai/Chronity/blob/main/LICENSE).