_xiaofang/xiaofang/Assets/Res/HYLUI/TaskPanel.cs
2024-12-12 11:04:48 +08:00

151 lines
3.4 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 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 坐标值
public List<Button> buttons; // 所有的任务
public Color selectedColor = new Color(0.5f, 0.5f, 1f, 1f); // 紫色
public Color defaultColor = Color.white; // 白色
// 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<TaskItem>();
item.SetInfo(taskId[i], JSONReader);
Button button = go.GetComponent<Button>();
button.onClick.AddListener(() => OnButtonClicked(button));
buttons.Add(button);
}
}
// 当点击任务时触发选中
void OnButtonClicked(Button clickedButton)
{
// 遍历所有按钮并重置颜色
foreach (Button button in buttons)
{
Image buttonImage = button.GetComponent<Image>();
if (button == clickedButton)
{
// 设置被点击按钮的颜色为紫色
buttonImage.color = selectedColor;
}
else
{
// 重置其他按钮为白色
buttonImage.color = defaultColor;
}
}
}
public async Task DestroyTaskAsync()
{
DeleteAllChildObjects();
await Task.Delay(10);
}
public void DeleteAllChildObjects()
{
// 遍历所有子物体并删除
foreach (Transform child in contentTrans)
{
Destroy(child.gameObject);
}
//清除按钮
buttons.Clear();
}
public void SetInfo(int id)
{
taskId.Add(id);
InitTask();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.J))
{
SetInfo(11001);
}
}
}