Last active
April 18, 2025 08:19
-
-
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#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test