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 List taskId = new List(); public Transform contentTrans; public GameObject taskPrefab; public JSONReader JSONReader; public Dictionary taskDic = new Dictionary(); public Button closeBtn; private bool isOpen = false; // Start is called before the first frame update void Start() { instance = this; closeBtn.onClick.AddListener(CloseBtn); } public async void InitTask() { await DestroyTaskAsync(); for(int i = 0; i < taskId.Count;i++) { GameObject go = GameObject.Instantiate(taskPrefab, contentTrans); go.transform.name = "Task_" + i; TaskItem item = go.GetComponent(); item.SetInfo(taskId[i], JSONReader); } } public async Task DestroyTaskAsync() { DeleteAllChildObjects(); await Task.Delay(10); } public void DeleteAllChildObjects() { // 遍历所有子物体并删除 foreach (Transform child in contentTrans) { Destroy(child.gameObject); } } public void SetInfo(int id) { taskId.Add(id); InitTask(); } public void CloseBtn() { if (isOpen)//关闭任务面板 { isOpen = false; } else//打开任务面板 { isOpen = true; } } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.J)) { SetInfo(11001); } } }