_xiaofang/xiaofang/Assets/Res/HYLUI/TaskPanel.cs
2024-12-06 11:52:15 +08:00

101 lines
1.8 KiB
C#

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<int> taskId = new List<int>();
public Transform contentTrans;
public GameObject taskPrefab;
public JSONReader JSONReader;
public Dictionary<int, Language> taskDic = new Dictionary<int, Language>();
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<TaskItem>();
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);
}
}
}