{"id":19210424,"url":"https://github.com/ryanslikesocool/clockkit","last_synced_at":"2025-05-12T19:23:59.839Z","repository":{"id":45195623,"uuid":"425641171","full_name":"ryanslikesocool/ClockKit","owner":"ryanslikesocool","description":"Timer and update handling for Unity","archived":false,"fork":false,"pushed_at":"2024-12-01T20:37:47.000Z","size":151,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-12T19:23:54.543Z","etag":null,"topics":["coroutines","delay","timer","unity3d"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanslikesocool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-11-07T23:30:15.000Z","updated_at":"2024-03-04T11:56:52.000Z","dependencies_parsed_at":"2024-02-27T14:29:11.836Z","dependency_job_id":"bb3e2f30-18af-462d-8c84-9e3823eb22f3","html_url":"https://github.com/ryanslikesocool/ClockKit","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FClockKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FClockKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FClockKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FClockKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanslikesocool","download_url":"https://codeload.github.com/ryanslikesocool/ClockKit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253806600,"owners_count":21967195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutines","delay","timer","unity3d"],"created_at":"2024-11-09T13:36:04.858Z","updated_at":"2025-05-12T19:23:59.821Z","avatar_url":"https://github.com/ryanslikesocool.png","language":"C#","readme":"# ClockKit\nTimer functions for Unity\n\n## Installation\n**Recommended Installation** (Unity Package Manager)\n- \"Add package from git URL...\"\n- `https://github.com/ryanslikesocool/ClockKit.git`\n\n**Alternate Installation** (Not recommended)\n- Get the latest [release](https://github.com/ryanslikesocool/ClockKit/releases)\n- Open with the desired Unity project\n- Import into the Plugins folder\n\n## Usage\nClockKit will automatically create a game object and attach scripts the first time it's accessed.\n\n### Essentials\n\n\\- __CKClock__\\\nThe static `CKClock` class provides access to most functionality, including:\n- starting and stopping timers\n- adding and removing update delegates\n\n\\- __Queues__\\\nThree `CKQueue`s are provided by default, each associated with a Unity-provided update loop.\nThe Update queue is used by default unless otherwise specified.\n\n\\- __Instant__\\\nA `CKInstant` struct is provided with essential current time information that can be used by both timers and delegates.\\\n`CKInstant` includes:\n- queue\n- local time\n- delta time\n- update count\n\n### Starting Timers\nThe `CKClock` class contains multiple functions and overloads to start timers.\n```cs\nCKClock.Delay(queue: Queue.Update, seconds: 2.5f, onComplete: () =\u003e {\n    Debug.Log(\"2.5 seconds have passed.\");\n})\n```\n\nEach function provides overloads allowing omission of some values, such as the queue.\n```cs\n// uses Queue.Default\nCKClock.Delay(seconds: 2.5f, onComplete: () =\u003e { /* ... */ });\n```\n\n### Stopping Timers\nAll `CKClock` functions that start timers will return a `CKKey` object that can be used to stop the associated timer.\n```cs\nCKKey delayKey = CKClock.Delay(seconds: 2.5f, onComplete: () =\u003e {\n    Debug.Log(\"2.5 seconds have passed.\");\n});\n\nbool success = CKClock.StopTimer(delayKey);\n```\nThe `StopTimer` function returns a `bool`, returning `true` if a timer associated with the key exists and was stopped, or `false` if there was no timer.\n\nBy default, the `StopTimer` function will attempt to stop the timer on all queues.\nAn overload is provided that allows a timer to be stopped on a certain `CKQueue`.\n\nA `StopTimers` function is also provided to stop multiple timers at once.\n\n### Custom Timers\nImplementing the `ICKTimer` or `ICKFixedDurationTimer` allows for timers with custom logic to be created.\n\nThe timer interfaces have an `OnUpdate` function.  Return `true` to mark the timer as complete and stop it.\n```cs\n// This example creates a timer that stops early if a value is true, or after certain amount of time has passed.\n\npublic struct MyCustomTimer: ICKFixedDurationTimer {\n    public delegate bool CompletionPredicate(in CKInstant instant);\n    public delegate void CompletionCallback(in CKInstant instant);\n\n    public float StartTime { get; }\n    public float Duration { get; }\n\n    public readonly CompletionCallback completionPredicate;\n    public readonly CompletionCallback onComplete;\n\n    public bool IsComplete { get; private set; }\n\n    public FixedDurationDelayingTimer(float startTime, float maxDuration, CompletionPredicate completionPredicate, CompletionCallback onComplete = null) {\n        this.StartTime = startTime;\n        this.Duration = maxDuration;\n        this.completionPredicate = completionPredicate;\n        this.onComplete = onComplete;\n        this.IsComplete = false;\n    }\n\n    public bool OnUpdate(in CKInstant instant) {\n        // Safeguard\n        if (IsComplete) {\n            return true;\n        }\n\n        // Calcualate the \"local time\" of the timer\n        float localTime = information.time - StartTime;\n\n        // Create relevant timer information for the callback\n        CKInstant timerInstant = new CKInstant(\n            instant.queue,\n            localTime,\n            instant.deltaTime,\n            instant.updateCount\n        );\n\n        // Invoke the predicate\n        IsComplete = completionPredicate(timerInstant);\n\n        if (IsComplete) {\n            Debug.Log(\"Timer predicate returned true.\");\n        }\n\n        // Check if the max time has elapsed\n        IsComplete |= localTime \u003e= Duration;\n\n        if (IsComplete) {\n            onComplete?.Invoke(timerInstant);\n        }\n        return IsComplete;\n    }\n}\n```\nCustom timers can be started similarly to built-in timers...\n```cs\nMyCustomTimer.CompletionPredicate completionPredicate = (in CKInstant _) =\u003e {\n    bool isComplete = SomeFunction();\n    return isComplete;\n};\n\n// Create the timer object\nICKTimer customTimer = new MyCustomTimer(\n    startTime: Time.time,\n    maxDuration: 5.0f,\n    completionPredicate:\n    onComplete: (in CKInstant instant) =\u003e { Debug.Log($\"Timer is complete after {instant.localTime} seconds\"); }\n);\n\n// Start the timer\nCKKey customTimerKey = CKClock.StartTimer(queue: Queue.LateUpdate, timer: customTimer);\n```\n... and stopped in the same way\n```cs\nCKClock.StopTimer(queue: Queue.LateUpdate, key: customTimerKey);\n```\n\n### With Other Packages\nClockKit has optional support for another package.\n\nIf [EaseKit](https://github.com/ryanslikesocool/EaseKit) (3.0.0 or later) is included in the project, convenience functions to start timers with easings and interpolators are enabled.\nIf EaseKit 3.1.0 or later is included, additional convenience functions with Spring support are enabled.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanslikesocool%2Fclockkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanslikesocool%2Fclockkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanslikesocool%2Fclockkit/lists"}