Skip to content

Instantly share code, notes, and snippets.

@waimus
Last active April 18, 2025 08:19
Show Gist options
  • Save waimus/14a5b343c53df0979a539a0f3f916143 to your computer and use it in GitHub Desktop.
Save waimus/14a5b343c53df0979a539a0f3f916143 to your computer and use it in GitHub Desktop.
A component mimicking the behaviour of Godot Engine's Timer node in a simple way. Written for Unity C#
/*
* StandaloneTimer.cs
* A component mimcking the behaviour of Godot Engine's
* Timer node in a simple way. Written for Unity C#.
*
* LICENSE
*
* Copyright 2022-2025 waimus
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR O
* THER DEALINGS IN THE SOFTWARE.
*
*/
using UnityEngine;
using UnityEngine.Events;
public class StandaloneTimer : MonoBehaviour {
public float Duration { get { return m_duration; } }
public float TimeLeft { get { return m_processedTime; } }
public bool IsStopped { get { return m_isStopped; } }
public UnityAction onTimerCreated;
public UnityAction onTimerStart;
public UnityAction onTimerEnd;
private float m_duration = 1.0f;
private float m_processedTime;
private bool m_isImmediate;
private bool m_isStopped;
public static StandaloneTimer Create(float p_duration, bool p_isImmediate = true) {
var t = new GameObject("StandaloneTimer").AddComponent<StandaloneTimer>();
t.m_isImmediate = p_isImmediate;
t.m_duration = p_duration;
return t;
}
public void StartTimer() {
m_processedTime = m_duration;
m_isStopped = false;
onTimerStart?.Invoke();
}
private void Start () {
onTimerCreated?.Invoke();
if (m_isImmediate) {
StartTimer();
}
else {
m_processedTime = -1; // Make inactive
m_isStopped = true;
}
}
private void Update() {
if (m_processedTime > 0 && !m_isStopped) {
m_processedTime -= Time.deltaTime;
}
else {
if ((int)m_processedTime < 0) return; // -1 is inactive, do nothing
m_isStopped = true;
onTimerEnd?.Invoke();
Destroy(gameObject);
}
}
}
@waimus
Copy link
Author

waimus commented Apr 18, 2025

Test

public void TestTimer() {
        var tx = StandaloneTimer.Create(5f);
        tx.onTimerCreated += () => { Debug.Log("Timer X created"); };
        tx.onTimerStart += () => { Debug.Log("Timer X started"); };
        tx.onTimerEnd += () => {
            Debug.Log($"Timer X ended after {tx.Duration}");

            var ty = StandaloneTimer.Create(6f);
            var tz = StandaloneTimer.Create(7f, false);
            
            ty.onTimerCreated += () => { Debug.Log("Timer Y created"); };
            ty.onTimerStart += () => { Debug.Log("Timer Y started"); };
            ty.onTimerEnd += () => {
                Debug.Log($"Timer Y ended after {ty.Duration}");
                tz.StartTimer();
            };
            
            tz.onTimerCreated += () => { Debug.Log("Timer Z created"); };
            tz.onTimerStart += () => { Debug.Log("Timer Z started"); };
            tz.onTimerEnd += () => {
                Debug.Log($"Timer Z ended after {tz.Duration}");
            };
        };
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment