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 RectTransform buttonRect; // 你的按钮 RectTransform public Button hideBtn; private bool isHidden = false; // 用来判断按钮是否已经隐藏 public float moveDuration = 0.5f; // 动画持续时间 public float hidePositionX = 125f; // 隐藏到右边的 X 坐标值,具体值根据屏幕大小调整 public float showPositionX = -198f; // 显示时按钮的 X 坐标值 // Start is called before the first frame update void Start() { instance = this; hideBtn.onClick.AddListener(OnClickHideButton); } 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() { 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(); } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.J)) { SetInfo(11001); } } }