Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions Source/Timer.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,8 @@
*/

using UnityEngine;
using UnityEngine.SceneManagement;
using System;
using System.Linq;
using System.Collections.Generic;
using JetBrains.Annotations;
using Object = UnityEngine.Object;
Expand DownExpand Up@@ -91,7 +91,7 @@ public bool isDone
/// after the parent has been destroyed.</param>
/// <returns>A timer object that allows you to examine stats and stop/resume progress.</returns>
public static Timer Register(float duration, Action onComplete, Action<float> onUpdate = null,
bool isLooped = false, bool useRealTime = false, MonoBehaviour autoDestroyOwner = null)
bool isLooped = false, bool useRealTime = false, MonoBehaviour autoDestroyOwner = null, bool cancelOnSceneChange = true)
{
// create a manager object to update all the timers if one does not already exist.
if (Timer._manager == null)
Expand All@@ -105,10 +105,11 @@ public static Timer Register(float duration, Action onComplete, Action<float> on
{
GameObject managerObject = new GameObject { name = "TimerManager" };
Timer._manager = managerObject.AddComponent<TimerManager>();
GameObject.DontDestroyOnLoad(managerObject);
}
}

Timer timer = new Timer(duration, onComplete, onUpdate, isLooped, useRealTime, autoDestroyOwner);
Timer timer = new Timer(duration, onComplete, onUpdate, isLooped, useRealTime, autoDestroyOwner, cancelOnSceneChange);
Timer._manager.RegisterTimer(timer);
return timer;
}
Expand DownExpand Up@@ -314,12 +315,14 @@ private bool isOwnerDestroyed
private readonly MonoBehaviour _autoDestroyOwner;
private readonly bool _hasAutoDestroyOwner;

private readonly bool _cancelOnSceneChange;

#endregion

#region Private Constructor (use static Register method to create new timer)

private Timer(float duration, Action onComplete, Action<float> onUpdate,
bool isLooped, bool usesRealTime, MonoBehaviour autoDestroyOwner)
bool isLooped, bool usesRealTime, MonoBehaviour autoDestroyOwner, bool cancelOnSceneChange)
{
this.duration = duration;
this._onComplete = onComplete;
Expand All@@ -331,6 +334,8 @@ private Timer(float duration, Action onComplete, Action<float> onUpdate,
this._autoDestroyOwner = autoDestroyOwner;
this._hasAutoDestroyOwner = autoDestroyOwner != null;

this._cancelOnSceneChange = cancelOnSceneChange;

this._startTime = this.GetWorldTime();
this._lastUpdateTime = this._startTime;
}
Expand DownExpand Up@@ -410,6 +415,29 @@ private class TimerManager : MonoBehaviour
// buffer adding timers so we don't edit a collection during iteration
private List<Timer> _timersToAdd = new List<Timer>();

[UsedImplicitly]
public void OnEnable()
{
SceneManager.activeSceneChanged += ActiveSceneChanged;
}

[UsedImplicitly]
public void OnDisable()
{
SceneManager.activeSceneChanged -= ActiveSceneChanged;
}

private void ActiveSceneChanged(Scene from, Scene to)
{
foreach (Timer timer in this._timers)
{
if(timer._cancelOnSceneChange)
{
timer.Cancel();
}
}
}

public void RegisterTimer(Timer timer)
{
this._timersToAdd.Add(timer);
Expand Down