using System;
using UnityEditor.Build.Pipeline.Interfaces;
namespace UnityEditor.Build.Pipeline.Utilities
{
///
/// Keeps track of the SBP build progress.
///
public class ProgressTracker : IProgressTracker, IDisposable
{
const long k_TicksPerSecond = 10000000;
///
/// Stores the number of tasks
///
public int TaskCount { get; set; }
///
/// Stores the amount of progress done as a decimal.
///
public float Progress { get { return CurrentTask / (float)TaskCount; } }
///
/// Stores the amount of updates per second.
///
public uint UpdatesPerSecond
{
get { return (uint)(k_TicksPerSecond / UpdateFrequency); }
set { UpdateFrequency = k_TicksPerSecond / Math.Max(value, 1); }
}
bool m_Disposed = false;
///
/// Stores the id of currently running task.
///
protected int CurrentTask { get; set; }
///
/// Stores the name of the currently running task.
///
protected string CurrentTaskTitle { get; set; }
///
/// Stores current the time stamp.
///
protected long TimeStamp { get; set; }
///
/// Stores the task update frequency.
///
protected long UpdateFrequency { get; set; }
///
/// Stores information about the current task.
///
public ProgressTracker()
{
CurrentTask = 0;
CurrentTaskTitle = "";
TimeStamp = 0;
UpdateFrequency = k_TicksPerSecond / 100;
}
///
/// Updates the progress bar to reflect the new running task.
///
/// The name of the new task.
/// Returns true if the progress bar is running. Returns false if the user cancels the progress bar.
public virtual bool UpdateTask(string taskTitle)
{
CurrentTask++;
CurrentTaskTitle = taskTitle;
TimeStamp = 0;
return !EditorUtility.DisplayCancelableProgressBar(CurrentTaskTitle, "", Progress);
}
///
/// Updates the information displayed for currently running task.
///
/// The task information.
/// Returns true if the progress bar is running. Returns false if the user cancels the progress bar.
public virtual bool UpdateInfo(string taskInfo)
{
var currentTicks = DateTime.Now.Ticks;
if (currentTicks - TimeStamp < UpdateFrequency)
return true;
TimeStamp = currentTicks;
return !EditorUtility.DisplayCancelableProgressBar(CurrentTaskTitle, taskInfo, Progress);
}
///
/// Disposes of the progress tracker instance.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Disposes of the progress tracker instance and clears the popup progress bar.
///
/// Set to true to clear the popup progress bar. Set to false to leave the progress bar as is.
protected virtual void Dispose(bool disposing)
{
if (m_Disposed)
return;
if (disposing)
EditorUtility.ClearProgressBar();
m_Disposed = true;
}
}
}