Files
aibis-dream/Assets/Scripts/Clue/FavoritesManager.cs
T

192 lines
5.7 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using DG.Tweening;
public class FavoritesManager : MonoBehaviour
{
public static FavoritesManager Instance;
public Button toggleFavoritesButton; // 用于打开/关闭收藏夹的按钮
private void Awake()
{
Instance = this;
}
[HideInInspector]
public List<Clue> punchTapeFavorites = new List<Clue>();
//public List<ScreenshotClue> screenshotFavorites = new List<ScreenshotClue>();
public GameObject favoritesUI; // 收藏夹UI
private RectTransform favoritesUIRectTransform;
public Transform punchTapeContent; // 打孔带分类的Content
//public Transform screenshotContent; // 截图分类的Content
public GameObject clueItemPrefab; // 收藏夹中线索Item的预制件
public GameObject imageClueItemPrefab; // 收藏夹中线索Item的预制件
//public Button punchTapeTabButton; // 切换到打孔带
//public Button screenshotTabButton; // 切换到截图
//public ScrollRect scrollRect; // 滚动视图
public GameObject punchTapePanel; // 打孔带分类面板
//public GameObject screenshotPanel; // 截图分类面板
private Vector2 hiddenPosition; // 收藏夹隐藏时的位置
private Vector2 visiblePosition; // 收藏夹显示时的位置
public bool isFavoritesOpen = false; // 收藏夹是否打开的状态
private void Start()
{
favoritesUIRectTransform=favoritesUI.GetComponent<RectTransform>();
hiddenPosition = new Vector2(favoritesUIRectTransform.anchoredPosition.x, -Screen.height);
visiblePosition = new Vector2(favoritesUIRectTransform.anchoredPosition.x, 0);
// // 设置收藏夹的初始位置为隐藏
favoritesUIRectTransform.anchoredPosition = hiddenPosition;
favoritesUI.SetActive(false);
// // 绑定按钮事件
toggleFavoritesButton.onClick.AddListener(ToggleFavorites);
// 绑定按钮事件
//punchTapeTabButton.onClick.AddListener(ShowPunchTapeFavorites);
//screenshotTabButton.onClick.AddListener(ShowScreenshotFavorites);
// 默认显示打孔带收藏
ShowPunchTapeFavorites();
}
public void ToggleFavorites()
{
if (isFavoritesOpen)
{
CloseFavorites();
}
else
{
OpenFavorites();
}
}
public void AddClue(Clue clue)
{
if (clue.ClueType==ClueType.PunchTape)
{
if (!punchTapeFavorites.Contains(clue))
{
punchTapeFavorites.Add(clue);
UpdateFavoritesUI();
}
}
else if (clue.ClueType==ClueType.ImageClue)
if (!punchTapeFavorites.Contains(clue))
{
punchTapeFavorites.Add(clue);
UpdateFavoritesUI();
}
}
public void RemoveClue(Clue clue)
{
if (clue.ClueType==ClueType.PunchTape)
{
if (punchTapeFavorites.Contains(clue))
{
punchTapeFavorites.Remove(clue);
UpdateFavoritesUI();
}
}
if (clue.ClueType==ClueType.ImageClue)
{
if (punchTapeFavorites.Contains(clue))
{
punchTapeFavorites.Remove(clue);
UpdateFavoritesUI();
}
}
}
private void UpdateFavoritesUI()
{
// 清空现有的UI元素
foreach (Transform child in punchTapeContent)
{
Destroy(child.gameObject);
}
// 重新创建剩余线索的UI
foreach (Clue clue in punchTapeFavorites)
{
GameObject clueItemGO;
ClueItem clueItem;
ClueInteraction clueInteraction;
// 根据 ClueType 实例化不同的预制件
if (clue.ClueType == ClueType.PunchTape)
{
clueItemGO = Instantiate(clueItemPrefab, punchTapeContent);
}
else if (clue.ClueType == ClueType.ImageClue)
{
clueItemGO = Instantiate(imageClueItemPrefab, punchTapeContent);
}
else
{
continue; // 如果有其他 ClueType,不处理并继续
}
// 设置 ClueItem 和 ClueInteraction
clueItem = clueItemGO.GetComponent<ClueItem>();
clueInteraction = clueItemGO.GetComponent<ClueInteraction>();
clueItem.Setup(clue);
clueInteraction.SetIsFavorited(true);
}
}
public void OpenFavorites()
{
UpdateFavoritesUI();
favoritesUI.SetActive(true);
ShowPunchTapeFavorites();
isFavoritesOpen = true;
// 用DOTween从右侧移动到可见区域
favoritesUIRectTransform.DOAnchorPos(visiblePosition, 0.5f); // 移动动画,持续0.5秒
}
public void CloseFavorites()
{
isFavoritesOpen = false;
// 用DOTween移动回右侧屏幕外
favoritesUIRectTransform.DOAnchorPos(hiddenPosition, 0.5f).OnComplete(() =>
{
favoritesUI.SetActive(false); // 移动完成后隐藏UI
});
}
public void ShowPunchTapeFavorites()
{
UpdateFavoritesUI();
punchTapePanel.SetActive(true);
//screenshotPanel.SetActive(false);
}
// public void ShowScreenshotFavorites()
// {
// punchTapePanel.SetActive(false);
// screenshotPanel.SetActive(true);
// // 清空内容
// foreach (Transform child in screenshotContent)
// {
// Destroy(child.gameObject);
// }
// // 显示截图收藏
// foreach (var screenshot in screenshotFavorites)
// {
// GameObject item = Instantiate(clueItemPrefab, screenshotContent);
// item.GetComponent<ClueItem>().Setup(screenshot);
// }
// }
}