unity 任务系统代码
时间: 2024-04-11 14:08:05 浏览: 11
以下是一个简单的Unity任务系统代码示例:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TaskManager : MonoBehaviour
{
public static TaskManager instance; // Singleton instance
public List<Task> tasks = new List<Task>(); // List of all tasks
private void Awake()
{
// Singleton initialization
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
// Add a new task to the list
public void AddTask(Task task)
{
tasks.Add(task);
}
// Remove a task from the list
public void RemoveTask(Task task)
{
tasks.Remove(task);
}
// Check if all tasks are completed
public bool CheckAllTasksCompleted()
{
foreach (Task task in tasks)
{
if (!task.completed)
{
return false;
}
}
return true;
}
}
public class Task
{
public string taskName; // Name of the task
public bool completed; // Whether the task is completed or not
// Constructor
public Task(string taskName)
{
this.taskName = taskName;
completed = false;
}
// Mark the task as completed
public void CompleteTask()
{
completed = true;
TaskManager.instance.RemoveTask(this);
}
}
```
使用示例:
```csharp
// Create a new task
Task task1 = new Task("Collect 10 coins");
// Add the task to the task manager
TaskManager.instance.AddTask(task1);
// Complete the task
task1.CompleteTask();
// Check if all tasks are completed
if (TaskManager.instance.CheckAllTasksCompleted())
{
Debug.Log("All tasks completed!");
}
```