feat: initial commit
This commit is contained in:
61
Assets/Common/Timer/.gitignore
vendored
Normal file
61
Assets/Common/Timer/.gitignore
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
# This .gitignore file should be placed at the root of your Unity project directory
|
||||
#
|
||||
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
|
||||
#
|
||||
/[Ll]ibrary/
|
||||
/[Tt]emp/
|
||||
/[Oo]bj/
|
||||
/[Bb]uild/
|
||||
/[Bb]uilds/
|
||||
/[Ll]ogs/
|
||||
/[Mm]emoryCaptures/
|
||||
|
||||
# Asset meta data should only be ignored when the corresponding asset is also ignored
|
||||
!/[Aa]ssets/**/*.meta
|
||||
|
||||
# Uncomment this line if you wish to ignore the asset store tools plugin
|
||||
# /[Aa]ssets/AssetStoreTools*
|
||||
|
||||
# Autogenerated Jetbrains Rider plugin
|
||||
[Aa]ssets/Plugins/Editor/JetBrains*
|
||||
|
||||
# Visual Studio cache directory
|
||||
.vs/
|
||||
.config
|
||||
|
||||
# Gradle cache directory
|
||||
.gradle/
|
||||
|
||||
# Autogenerated VS/MD/Consulo solution and project files
|
||||
ExportedObj/
|
||||
.consulo/
|
||||
*.csproj
|
||||
*.unityproj
|
||||
*.sln
|
||||
*.suo
|
||||
*.tmp
|
||||
*.user
|
||||
*.userprefs
|
||||
*.pidb
|
||||
*.booproj
|
||||
*.svd
|
||||
*.pdb
|
||||
*.mdb
|
||||
*.opendb
|
||||
*.VC.db
|
||||
|
||||
# Unity3D generated meta files
|
||||
*.pidb.meta
|
||||
*.pdb.meta
|
||||
*.mdb.meta
|
||||
|
||||
# Unity3D generated file on crash reports
|
||||
sysinfo.txt
|
||||
|
||||
# Builds
|
||||
*.apk
|
||||
*.unitypackage
|
||||
|
||||
# Crashlytics generated file
|
||||
crashlytics-build.properties
|
||||
|
||||
12
Assets/Common/Timer/CHANGELOG.md
Normal file
12
Assets/Common/Timer/CHANGELOG.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.0.0] - 2025-11-24
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release of the Unity package.
|
||||
7
Assets/Common/Timer/CHANGELOG.md.meta
Normal file
7
Assets/Common/Timer/CHANGELOG.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b59e8b83170e79e49bb382cbd8bb2e47
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
0
Assets/Common/Timer/Documentation~/common.timer.md
Normal file
0
Assets/Common/Timer/Documentation~/common.timer.md
Normal file
21
Assets/Common/Timer/LICENSE.md
Normal file
21
Assets/Common/Timer/LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Joe Siu
|
||||
|
||||
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 OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
7
Assets/Common/Timer/LICENSE.md.meta
Normal file
7
Assets/Common/Timer/LICENSE.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 26de4860f9f29e146a42655c17acb7a8
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Timer/README.md
Normal file
8
Assets/Common/Timer/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# com.joesiu.common.timer
|
||||
|
||||
## Installation
|
||||
|
||||
1. Open `Package Manager`
|
||||
2. Click `+`
|
||||
3. Select `Add package from git URL...`
|
||||
4. Paste `https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Timer`
|
||||
7
Assets/Common/Timer/README.md.meta
Normal file
7
Assets/Common/Timer/README.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ba592ce62241954c91a14eb83c32366
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Timer/Runtime.meta
Normal file
8
Assets/Common/Timer/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fab66aac1f04504db2f990f40887a73
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Assets/Common/Timer/Runtime/IStopwatch.cs
Normal file
20
Assets/Common/Timer/Runtime/IStopwatch.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public interface IStopwatch : ITimer
|
||||
{
|
||||
public float Duration { get; set; }
|
||||
public bool DeactivateOnTimeReached { get; set; }
|
||||
public float RemainingTime { get; set; }
|
||||
public float Progress { get; set; }
|
||||
public bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public void SetDuration(float value);
|
||||
public void SetRemainingTime(float value);
|
||||
public void SetProgress(float value);
|
||||
public void EndTime();
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/IStopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/IStopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d400d4cccd55e94088df52dfd4cbb96
|
||||
23
Assets/Common/Timer/Runtime/ITimer.cs
Normal file
23
Assets/Common/Timer/Runtime/ITimer.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public interface ITimer
|
||||
{
|
||||
public bool IsActive { get; }
|
||||
public float CurrentTime { get; set; }
|
||||
public float DeltaTime { get; }
|
||||
|
||||
public void Tick();
|
||||
public void Activate(bool resetTime = false);
|
||||
public void Deactivate();
|
||||
public void ResetTime();
|
||||
|
||||
public void SetTime(float value);
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/ITimer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/ITimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166ad9d07eff7ce409885714f6cddd55
|
||||
20
Assets/Common/Timer/Runtime/JoeSiu.Common.Timer.asmdef
Normal file
20
Assets/Common/Timer/Runtime/JoeSiu.Common.Timer.asmdef
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "Common.Timer.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||
"GUID:324caed91501a9c47a04ebfd87b68794",
|
||||
"GUID:d7f6647327e7fa54faec2c8e2a49621d",
|
||||
"GUID:70e478b7eb804446bc79813af716d369"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4867374f8b89f5b4587d9170bed9e856
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
Assets/Common/Timer/Runtime/MonoStopwatch.cs
Normal file
104
Assets/Common/Timer/Runtime/MonoStopwatch.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public class MonoStopwatch : MonoTimer, IStopwatch
|
||||
{
|
||||
public override float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Clamp(value, 0f, Duration);
|
||||
}
|
||||
|
||||
[Title("Settings (Stopwatch)")]
|
||||
[SerializeField]
|
||||
protected float duration;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Duration
|
||||
{
|
||||
get
|
||||
{
|
||||
duration = Mathf.Max(0.001f, duration);
|
||||
return duration;
|
||||
}
|
||||
set => duration = Mathf.Max(0.001f, value);
|
||||
}
|
||||
[SerializeField]
|
||||
protected bool deactivateOnTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => deactivateOnTimeReached;
|
||||
set => deactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float RemainingTime
|
||||
{
|
||||
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
|
||||
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Progress
|
||||
{
|
||||
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
|
||||
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
|
||||
public virtual bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public override void Tick()
|
||||
{
|
||||
if (IsTimeReached) return;
|
||||
|
||||
base.Tick();
|
||||
if (!IsActive) return;
|
||||
|
||||
if (CurrentTime >= Duration) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value)
|
||||
{
|
||||
Duration = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetRemainingTime(float value)
|
||||
{
|
||||
RemainingTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetProgress(float value)
|
||||
{
|
||||
Progress = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void EndTime()
|
||||
{
|
||||
currentTime = Duration;
|
||||
InvokeOnTimeReached();
|
||||
if (DeactivateOnTimeReached) Deactivate();
|
||||
}
|
||||
|
||||
protected virtual void InvokeOnTimeReached()
|
||||
{
|
||||
OnTimeReached?.Invoke();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("End Time"), HideInEditMode]
|
||||
private void EndTimer_Editor() => EndTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/MonoStopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/MonoStopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7152de60fabb33c41aebca0a58b2096b
|
||||
109
Assets/Common/Timer/Runtime/MonoTimer.cs
Normal file
109
Assets/Common/Timer/Runtime/MonoTimer.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
[DeclareBoxGroup("Debug")]
|
||||
[DeclareBoxGroup("Internal")]
|
||||
// TODO: Create a MonoTimerBase similar to TimerBaseSO instead
|
||||
public class MonoTimer : MonoBehaviour, ITimer
|
||||
{
|
||||
[Title("Settings")]
|
||||
[SerializeField]
|
||||
protected bool isActive;
|
||||
public virtual bool IsActive
|
||||
{
|
||||
get => isActive;
|
||||
protected set => isActive = value;
|
||||
}
|
||||
|
||||
protected float currentTime;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Max(currentTime, 0f);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Max(value, 0f);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float DeltaTime => Time.deltaTime;
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
Tick();
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public virtual void Tick()
|
||||
{
|
||||
if (!IsActive) return;
|
||||
|
||||
CurrentTime += DeltaTime;
|
||||
InvokeOnTick();
|
||||
}
|
||||
|
||||
public virtual void Activate(bool resetTime = false)
|
||||
{
|
||||
IsActive = true;
|
||||
InvokeOnActivate();
|
||||
if (resetTime) ResetTime();
|
||||
}
|
||||
|
||||
public virtual void Deactivate()
|
||||
{
|
||||
IsActive = false;
|
||||
InvokeOnDeactivate();
|
||||
}
|
||||
|
||||
public virtual void ResetTime()
|
||||
{
|
||||
CurrentTime = 0f;
|
||||
InvokeOnTimeReset();
|
||||
}
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void InvokeOnTick() => OnTick?.Invoke();
|
||||
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
|
||||
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
|
||||
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
|
||||
|
||||
#endregion
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("Activate"), HideInEditMode]
|
||||
private void Activate_Editor() => Activate(false);
|
||||
|
||||
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
|
||||
private void ActivateResetTime_Editor() => Activate(true);
|
||||
|
||||
[Group("Debug"), Button("Deactivate"), HideInEditMode]
|
||||
private void Deactivate_Editor() => Deactivate();
|
||||
|
||||
[Group("Debug"), Button("Tick"), HideInEditMode]
|
||||
private void Tick_Editor() => Tick();
|
||||
|
||||
[Group("Debug"), Button("Reset Time"), HideInEditMode]
|
||||
private void ResetTime_Editor() => ResetTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/MonoTimer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/MonoTimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34fac720fb4287843aea355b0594eda1
|
||||
8
Assets/Common/Timer/Runtime/ScriptableObjects.meta
Normal file
8
Assets/Common/Timer/Runtime/ScriptableObjects.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84762aa781ebe084290160322bc621e3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal file
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "Stopwatch", menuName = "JoeSiu/Common/Stopwatch")]
|
||||
public class StopwatchSO : TimerBaseSO, IStopwatch
|
||||
{
|
||||
protected override ITimer Timer => _stopwatch;
|
||||
|
||||
[SerializeField, InlineProperty, HideLabel]
|
||||
[InfoBox("The stopwatch in ScriptableObject will be deactivated by default")]
|
||||
private Stopwatch _stopwatch = new(false, 10f);
|
||||
|
||||
public float Duration
|
||||
{
|
||||
get => _stopwatch.Duration;
|
||||
set => _stopwatch.Duration = value;
|
||||
}
|
||||
public float RemainingTime
|
||||
{
|
||||
get => _stopwatch.RemainingTime;
|
||||
set => _stopwatch.RemainingTime = value;
|
||||
}
|
||||
public float Progress
|
||||
{
|
||||
get => _stopwatch.Progress;
|
||||
set => _stopwatch.Progress = value;
|
||||
}
|
||||
public bool IsTimeReached => _stopwatch.IsTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => _stopwatch.DeactivateOnTimeReached;
|
||||
set => _stopwatch.DeactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
public event Action OnTimeReached
|
||||
{
|
||||
add => _stopwatch.OnTimeReached += value;
|
||||
remove => _stopwatch.OnTimeReached -= value;
|
||||
}
|
||||
|
||||
protected override void Activate_Internal(bool resetTime = false)
|
||||
{
|
||||
_stopwatch.Activate(resetTime);
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value) => _stopwatch.SetDuration(value);
|
||||
|
||||
public virtual void SetRemainingTime(float value) => _stopwatch.SetRemainingTime(value);
|
||||
|
||||
public virtual void SetProgress(float value) => _stopwatch.SetProgress(value);
|
||||
|
||||
public void EndTime() => _stopwatch.EndTime();
|
||||
|
||||
public override void Reinitialize()
|
||||
{
|
||||
_stopwatch = new(false, _stopwatch.Duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bef3fff7f3888464aab61a599e9a460a
|
||||
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal file
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
public abstract class TimerBaseSO : ScriptableObject, ITimer
|
||||
{
|
||||
protected abstract ITimer Timer { get; }
|
||||
|
||||
public bool IsActive => Timer.IsActive;
|
||||
public float CurrentTime
|
||||
{
|
||||
get => Timer.CurrentTime;
|
||||
set => Timer.CurrentTime = value;
|
||||
}
|
||||
public float DeltaTime => Timer.DeltaTime;
|
||||
|
||||
public void Tick() => Timer.Tick();
|
||||
|
||||
public void Activate(bool resetTime = false)
|
||||
{
|
||||
AddToManager();
|
||||
Activate_Internal(resetTime);
|
||||
}
|
||||
|
||||
public void Deactivate() => Timer.Deactivate();
|
||||
|
||||
public void ResetTime() => Timer.ResetTime();
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
public event Action OnTick
|
||||
{
|
||||
add => Timer.OnTick += value;
|
||||
remove => Timer.OnTick -= value;
|
||||
}
|
||||
public event Action OnActivate
|
||||
{
|
||||
add => Timer.OnActivate += value;
|
||||
remove => Timer.OnActivate -= value;
|
||||
}
|
||||
public event Action OnDeactivate
|
||||
{
|
||||
add => Timer.OnDeactivate += value;
|
||||
remove => Timer.OnDeactivate -= value;
|
||||
}
|
||||
public event Action OnTimeReset
|
||||
{
|
||||
add => Timer.OnTimeReset += value;
|
||||
remove => Timer.OnTimeReset -= value;
|
||||
}
|
||||
|
||||
protected abstract void Activate_Internal(bool resetTime = false);
|
||||
|
||||
private void AddToManager()
|
||||
{
|
||||
if (!TimerManager.GetInstance()) TimerManager.CreateInstance();
|
||||
TimerManager.Instance.Add(this);
|
||||
}
|
||||
|
||||
private void RemoveFromManager()
|
||||
{
|
||||
if (!TimerManager.GetInstance()) return;
|
||||
TimerManager.Instance.Remove(this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected virtual void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
if (state == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
Reinitialize();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected virtual void OnValidate()
|
||||
{
|
||||
if (!EditorApplication.isPlaying) Timer.Deactivate();
|
||||
}
|
||||
#endif
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
#endif
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
||||
#endif
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Reinitialize();
|
||||
#endif
|
||||
}
|
||||
|
||||
[Button]
|
||||
public abstract void Reinitialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 312c8c00ee578df428063b28e6322aed
|
||||
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal file
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "Timer", menuName = "JoeSiu/Common/Timer")]
|
||||
public class TimerSO : TimerBaseSO
|
||||
{
|
||||
protected override ITimer Timer => _timer;
|
||||
|
||||
[SerializeField, InlineProperty, HideLabel]
|
||||
[InfoBox("The timer in ScriptableObject will be deactivated by default")]
|
||||
private Timer _timer = new(false);
|
||||
|
||||
protected override void Activate_Internal(bool resetTime = false)
|
||||
{
|
||||
_timer.Activate(resetTime);
|
||||
}
|
||||
|
||||
public override void Reinitialize()
|
||||
{
|
||||
_timer = new(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc4e8df518cd7244dbdfc3464c604bed
|
||||
120
Assets/Common/Timer/Runtime/Stopwatch.cs
Normal file
120
Assets/Common/Timer/Runtime/Stopwatch.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class Stopwatch : Timer, IStopwatch
|
||||
{
|
||||
public override float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Clamp(value, 0f, Duration);
|
||||
}
|
||||
|
||||
[Title("Settings (Stopwatch)")]
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_duration")]
|
||||
protected float duration;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Duration
|
||||
{
|
||||
get
|
||||
{
|
||||
duration = Mathf.Max(0.001f, duration);
|
||||
return duration;
|
||||
}
|
||||
set => duration = Mathf.Max(0.001f, value);
|
||||
}
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_deactivateOnTimeReached")]
|
||||
protected bool deactivateOnTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => deactivateOnTimeReached;
|
||||
set => deactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float RemainingTime
|
||||
{
|
||||
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
|
||||
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Progress
|
||||
{
|
||||
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
|
||||
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
|
||||
public virtual bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public Stopwatch(bool isActive, float duration, bool deactivateOnTimeReached = false) : base(isActive)
|
||||
{
|
||||
this.duration = duration;
|
||||
this.deactivateOnTimeReached = deactivateOnTimeReached;
|
||||
}
|
||||
|
||||
public override void Tick()
|
||||
{
|
||||
if (IsTimeReached) return;
|
||||
|
||||
base.Tick();
|
||||
if (!IsActive) return;
|
||||
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public override void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value)
|
||||
{
|
||||
Duration = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetRemainingTime(float value)
|
||||
{
|
||||
RemainingTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetProgress(float value)
|
||||
{
|
||||
Progress = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void EndTime()
|
||||
{
|
||||
currentTime = Duration;
|
||||
InvokeOnTimeReached();
|
||||
if (DeactivateOnTimeReached) Deactivate();
|
||||
}
|
||||
|
||||
protected virtual void InvokeOnTimeReached()
|
||||
{
|
||||
OnTimeReached?.Invoke();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("End Time"), HideInEditMode]
|
||||
private void EndTimer_Editor() => EndTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/Stopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/Stopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: deafc70d1d6a826429baf2f506837603
|
||||
114
Assets/Common/Timer/Runtime/Timer.cs
Normal file
114
Assets/Common/Timer/Runtime/Timer.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// C# implementation of a timer. Remember to call Tick() on this Timer object via script!
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DeclareBoxGroup("Debug")]
|
||||
[DeclareBoxGroup("Internal")]
|
||||
public class Timer : ITimer
|
||||
{
|
||||
[Title("Settings")]
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_isActive")]
|
||||
protected bool isActive;
|
||||
public virtual bool IsActive
|
||||
{
|
||||
get => isActive;
|
||||
protected set => isActive = value;
|
||||
}
|
||||
|
||||
protected float currentTime;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Max(currentTime, 0f);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Max(value, 0f);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float DeltaTime => Time.deltaTime;
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
|
||||
public Timer(bool isActive)
|
||||
{
|
||||
this.isActive = isActive;
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public virtual void Tick()
|
||||
{
|
||||
if (!IsActive) return;
|
||||
|
||||
CurrentTime += DeltaTime;
|
||||
InvokeOnTick();
|
||||
}
|
||||
|
||||
public virtual void Activate(bool resetTime = false)
|
||||
{
|
||||
IsActive = true;
|
||||
InvokeOnActivate();
|
||||
if (resetTime) ResetTime();
|
||||
}
|
||||
|
||||
public virtual void Deactivate()
|
||||
{
|
||||
IsActive = false;
|
||||
InvokeOnDeactivate();
|
||||
}
|
||||
|
||||
public virtual void ResetTime()
|
||||
{
|
||||
CurrentTime = 0f;
|
||||
InvokeOnTimeReset();
|
||||
}
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void InvokeOnTick() => OnTick?.Invoke();
|
||||
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
|
||||
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
|
||||
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
|
||||
|
||||
#endregion
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("Activate"), HideInEditMode]
|
||||
private void Activate_Editor() => Activate(false);
|
||||
|
||||
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
|
||||
private void ActivateResetTime_Editor() => Activate(true);
|
||||
|
||||
[Group("Debug"), Button("Deactivate"), HideInEditMode]
|
||||
private void Deactivate_Editor() => Deactivate();
|
||||
|
||||
[Group("Debug"), Button("Tick"), HideInEditMode]
|
||||
private void Tick_Editor() => Tick();
|
||||
|
||||
[Group("Debug"), Button("Reset Time"), HideInEditMode]
|
||||
private void ResetTime_Editor() => ResetTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/Timer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/Timer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64b5ad6c3a3b3934892167377d2ff419
|
||||
41
Assets/Common/Timer/Runtime/TimerManager.cs
Normal file
41
Assets/Common/Timer/Runtime/TimerManager.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using Common.Timer.Runtime.ScriptableObjects;
|
||||
using JoeSiu.Common.Infrastructure.Runtime;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public class TimerManager : MonoSingleton<TimerManager>
|
||||
{
|
||||
[InfoBox("As ScriptableObject doesn't have Update() function, use this class to control the ScriptableObject timer's tick")]
|
||||
[SerializeField]
|
||||
private List<TimerBaseSO> _timerSOs = new();
|
||||
public IReadOnlyList<TimerBaseSO> TimerSOs => _timerSOs.AsReadOnly();
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
DontDestroyOnLoad(this);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timerSOs.ForEach(so => so?.Tick());
|
||||
}
|
||||
|
||||
public void Add(TimerBaseSO timerBaseSO)
|
||||
{
|
||||
if (_timerSOs.Contains(timerBaseSO)) return;
|
||||
_timerSOs.Add(timerBaseSO);
|
||||
Debug.Log($"Added TimerBaseSO '{timerBaseSO.name}' to {nameof(TimerManager)}", this);
|
||||
}
|
||||
|
||||
public void Remove(TimerBaseSO timerBaseSO)
|
||||
{
|
||||
if (!_timerSOs.Contains(timerBaseSO)) return;
|
||||
_timerSOs.Remove(timerBaseSO);
|
||||
Debug.Log( $"Removed TimerBaseSO '{timerBaseSO.name}' from {nameof(TimerManager)}", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/TimerManager.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/TimerManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 477f0034741527640a767bbcb0c42a01
|
||||
8
Assets/Common/Timer/Runtime/UI.meta
Normal file
8
Assets/Common/Timer/Runtime/UI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6346ab71a8da1784c84e99bf10a88384
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
68
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs
Normal file
68
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using JoeSiu.Common.Serialization.Runtime;
|
||||
using TMPro;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.UI
|
||||
{
|
||||
public class TimerDebugText : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private SerializableInterface<ITimer> _timer = new();
|
||||
public TMP_Text text;
|
||||
public string prefix;
|
||||
public int decimalPlaces = 2;
|
||||
|
||||
public ITimer Timer
|
||||
{
|
||||
get => _timer.Value;
|
||||
set => _timer.Value = value;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
text = GetComponent<TMP_Text>();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateText(Timer);
|
||||
}
|
||||
|
||||
public void UpdateText(ITimer timer)
|
||||
{
|
||||
if (!text) return;
|
||||
if (timer == null) return;
|
||||
|
||||
var format = $"F{decimalPlaces}";
|
||||
var info = string.IsNullOrEmpty(prefix) ? "" : $"{prefix}\n";
|
||||
if (timer is Behaviour behaviour) info += $"enabled: {behaviour.enabled}\n";
|
||||
info += $"active: {timer.IsActive}\n";
|
||||
|
||||
switch (timer)
|
||||
{
|
||||
// Check if timer implements IStopwatch
|
||||
case IStopwatch stopwatch:
|
||||
info += $"isTimeReached: {stopwatch.IsTimeReached}\n";
|
||||
info += $"{Math.Round(stopwatch.CurrentTime, decimalPlaces).ToString(format)}s / " +
|
||||
$"{Math.Round(stopwatch.Duration, decimalPlaces).ToString(format)}s " +
|
||||
$"(remain: {Math.Round(stopwatch.RemainingTime, decimalPlaces).ToString(format)}s)\n";
|
||||
break;
|
||||
// Fallback for base Timer class
|
||||
default:
|
||||
info += $"{Math.Round(timer.CurrentTime, decimalPlaces).ToString(format)}s\n";
|
||||
break;
|
||||
}
|
||||
|
||||
text.text = info;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9449292013eda4945bf3f8bc3ba3b52d
|
||||
79
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs
Normal file
79
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using JoeSiu.Common.Serialization.Runtime;
|
||||
using TMPro;
|
||||
using TriInspector;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.UI
|
||||
{
|
||||
[ExecuteAlways]
|
||||
public class TimerDurationText : MonoBehaviour
|
||||
{
|
||||
public TMP_Text text;
|
||||
[SerializeField]
|
||||
private SerializableInterface<ITimer> _timer = new();
|
||||
#if UNITY_EDITOR
|
||||
[ShowIf(nameof(IsStopwatch))]
|
||||
#endif
|
||||
public bool useRemainingTimeForStopwatch = true;
|
||||
[Range(0, 15)]
|
||||
public int precision = 0;
|
||||
[Range(0, 15)]
|
||||
public int padLeft = 0;
|
||||
[Range(0, 15)]
|
||||
public int padRight = 0;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private bool IsStopwatch => Timer is IStopwatch;
|
||||
#endif
|
||||
|
||||
public ITimer Timer
|
||||
{
|
||||
get => _timer?.Value;
|
||||
set => _timer.Value = value;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
text = GetComponent<TMP_Text>();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateText(Timer);
|
||||
}
|
||||
|
||||
public void UpdateText(ITimer timer)
|
||||
{
|
||||
if (!text) return;
|
||||
if (timer == null) return;
|
||||
|
||||
float time;
|
||||
|
||||
if (useRemainingTimeForStopwatch && timer is IStopwatch stopwatch)
|
||||
{
|
||||
time = stopwatch.RemainingTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
time = timer.CurrentTime;
|
||||
}
|
||||
|
||||
time = (float)Math.Round(time, precision);
|
||||
var timeString = time.ToString();
|
||||
// TODO: PadLeft / PadRight doesn't work in all cases (e.g., given time of 1, it can't turn into 01.00)
|
||||
// TODO: Use a custom user defined formatter instead?
|
||||
if (padLeft > 0) timeString = timeString.PadLeft(padLeft, '0');
|
||||
if (padRight > 0) timeString = timeString.PadRight(padRight, '0');
|
||||
text.SetText(timeString);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a872f72d4d5d8e94895cff23a747e3f0
|
||||
20
Assets/Common/Timer/package.json
Normal file
20
Assets/Common/Timer/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "com.joesiu.common.timer",
|
||||
"version": "1.0.0",
|
||||
"description": "Set of timer and stopwatch components.",
|
||||
"displayName": "Timer",
|
||||
"unity": "2019.4",
|
||||
"author": {
|
||||
"name": "Joe Siu",
|
||||
"url": "https://github.com/JoeSiu"
|
||||
},
|
||||
"changelogUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/CHANGELOG.md",
|
||||
"dependencies": {
|
||||
"com.codewriter.triinspector": "https://github.com/codewriter-packages/Tri-Inspector.git",
|
||||
"com.joesiu.common.infrastructure": "https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Infrastructure",
|
||||
"com.joesiu.common.serialization": "https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Serialization"
|
||||
},
|
||||
"documentationUrl": "https://github.com/JoeSiu/UnityPackages/",
|
||||
"keywords": [],
|
||||
"licensesUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/LICENSE.md"
|
||||
}
|
||||
7
Assets/Common/Timer/package.json.meta
Normal file
7
Assets/Common/Timer/package.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb89a7fa74c613b41b84b47ad22e88aa
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user