using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using System.Threading.Tasks; public class TaskPanel : Base { public static TaskPanel instance; public Transform contentTrans; // 任务列表容器 public GameObject taskPrefab; // 任务项预制体 public JSONReader JSONReader; // JSON 数据读取类 public RectTransform buttonRect; // 按钮 RectTransform public Button hideBtn; // 隐藏按钮 private bool isHidden = false; // 是否隐藏状态 public float moveDuration = 0.5f; public float hidePositionX = 125f; public float showPositionX = -198f; public List taskIDs = new List(); private List taskItems = new List(); // 当前显示的任务列表 void Start() { instance = this; hideBtn.onClick.AddListener(OnClickHideButton); InitList(); InitTask(taskIDs); } public void InitList() { foreach (Task_ task in JSONReader.TaskDictionary.Values) { if (int.TryParse(task.ID, out int taskId)) { // 转换成功,添加到 taskIDs 列表 taskIDs.Add(taskId); } } } // 将任务栏收起或显示 public void OnClickHideButton() { if (isHidden) { buttonRect.DOAnchorPosX(showPositionX, moveDuration).SetEase(Ease.InOutCubic); hideBtn.transform.Rotate(0, 0, 180f); isHidden = false; } else { buttonRect.DOAnchorPosX(hidePositionX, moveDuration).SetEase(Ease.InOutCubic); hideBtn.transform.Rotate(0, 0, 180f); isHidden = true; } } // 初始化任务列表 public async void InitTask(List taskIds) { await ClearTaskItems(); foreach (int id in taskIds) { AddTask(id); } } // 添加新任务 public void AddTask(int taskId) { GameObject go = Instantiate(taskPrefab, contentTrans); TaskItem taskItem = go.GetComponent(); taskItem.SetInfo(taskId, JSONReader); taskItems.Add(taskItem); } // 更新任务UI public void UpdateTaskUI(TaskItem taskItem) { // 查找对应的UI项,更新进度或状态 TaskItem item = taskItems.Find(t => t.taskId == taskItem.taskId); if (item != null) { item.UpdateTxt(); } } // 移除任务 public void RemoveTask(int taskId) { TaskItem taskItem = taskItems.Find(t => t.taskId == taskId); if (taskItem != null) { taskItems.Remove(taskItem); Destroy(taskItem.gameObject); } } // 清空任务 public async Task ClearTaskItems() { foreach (Transform child in contentTrans) { Destroy(child.gameObject); } taskItems.Clear(); await Task.Delay(10); } }