Task Utilities
January 26, 2026 ยท View on GitHub
Overview
The Task utilities provide a lightweight way to define and run time-measured tasks in Unity. Tasks are represented by the ITask interface and executed by the TaskManager, which tracks execution time and can log results.
Core Types
ITask: Defines the task contract (Execute,ShortName,TimeElapsed).TaskContext: Base class for shared execution context objects.TaskData: Base class for task-specific input data.TaskManager: Singleton that stores a list of tasks, executes them by index, and can log their elapsed time.
How to Use
- Create a class that implements
ITask. - Optionally define a
TaskContextandTaskDatasubclass to carry state and inputs. - Register task instances in
TaskManager(via Inspector or code). - Call
TaskManager.Executewith the task index, context, and data. - Use
ShowElapsedTimeForTasksto log execution times.
Example
using UnityEngine;
using GameUtils;
// Example context that can store shared state.
public sealed class ExampleTaskContext : TaskContext
{
// No class-level comments by project convention.
}
// Example data used as input to a task.
public sealed class ExampleTaskData : TaskData
{
// No class-level comments by project convention.
}
// Example task implementation.
public sealed class ExampleTask : ITask
{
// No class-level comments by project convention.
public double TimeElapsed { get; set; }
public string ShortName => "Example";
public void Execute(ref object context, ref object data)
{
// Comment: cast the incoming context/data to your concrete types.
var typedContext = context as ExampleTaskContext;
var typedData = data as ExampleTaskData;
// Comment: perform your task logic here.
Debug.Log("Executing ExampleTask");
// Comment: optionally update the passed-in context/data objects.
context = typedContext;
data = typedData;
}
}
public sealed class ExampleTaskRunner : MonoBehaviour
{
[SerializeField] private TaskManager _taskManager;
private void Start()
{
// Comment: create context and data instances for the task.
var context = new ExampleTaskContext();
var data = new ExampleTaskData();
// Comment: execute the first task in the TaskManager list.
_taskManager.Execute(0, context, data);
// Comment: log the elapsed time for all tasks.
_taskManager.ShowElapsedTimeForTasks();
}
}
Notes
Executeusesref objectto keep the interface generic. Cast to your concreteTaskContext/TaskDatatypes inside the task.TaskManagertracks elapsed time in milliseconds and stores it per task.- Ensure your task list order in the
TaskManagermatches the index used inExecute.