131 lines
3.9 KiB
C#
131 lines
3.9 KiB
C#
using UnityEngine;
|
|
using Yarn.Unity;
|
|
using System;
|
|
using System.Collections;
|
|
using AibisDream;
|
|
using System.Linq;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
|
|
public class BookManager : MonoBehaviour
|
|
{
|
|
public Book book; // Reference to the book script
|
|
private AutoFlip flipmanager;
|
|
|
|
public Button CloseBookButton;
|
|
|
|
public Button OepnBookButton;
|
|
|
|
private RectTransform bookPos;
|
|
|
|
public event Action OnBookClosed;
|
|
|
|
public event Action OnBookOpen;
|
|
|
|
void Start()
|
|
{
|
|
CloseBookButton.onClick.RemoveAllListeners(); // 确保不会重复绑定
|
|
CloseBookButton.onClick.AddListener(() =>
|
|
{
|
|
// 仅关闭书本,不执行额外操作
|
|
CloseBook();
|
|
});
|
|
|
|
OepnBookButton.onClick.RemoveAllListeners(); // 确保不会重复绑定
|
|
OepnBookButton.onClick.AddListener(() =>
|
|
{
|
|
// 打开书本
|
|
ButtonOpenBook();
|
|
});
|
|
|
|
DialogController.Instance.RegisterOptionCommand("listen_closeBook", CombineCloseBook);
|
|
flipmanager = book.gameObject.GetComponent<AutoFlip>();
|
|
bookPos = book.gameObject.GetComponent<RectTransform>();
|
|
// Initially, the book is closed
|
|
CloseBook();
|
|
}
|
|
// Open the book and flip to a specific page
|
|
|
|
public void CombineCloseBook(YarnOption[] options)
|
|
{
|
|
var optionDic = options.ToDictionary(item => item.Text, item => item);
|
|
Debug.Log("Option dictionary keys: " + string.Join(", ", optionDic.Keys));
|
|
|
|
if (optionDic.ContainsKey("CloseBook"))
|
|
{
|
|
// 监听书关闭事件
|
|
OnBookClosed -= TriggerCloseBookOption; // 避免重复监听
|
|
OnBookClosed += TriggerCloseBookOption;
|
|
|
|
// 内部方法:当书关闭时触发该选项的逻辑
|
|
void TriggerCloseBookOption()
|
|
{
|
|
optionDic["CloseBook"].SelectOption();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ButtonOpenBook()
|
|
{
|
|
StartCoroutine(OpenBook());
|
|
}
|
|
|
|
[YarnCommand("OpenBook")]
|
|
public IEnumerator OpenBook()
|
|
{
|
|
OnBookOpen?.Invoke();
|
|
// 如果书还没有激活,先激活它
|
|
if (!book.gameObject.activeInHierarchy)
|
|
{
|
|
book.gameObject.SetActive(true);
|
|
}
|
|
|
|
// 使用 DoTween 创建一个动画序列
|
|
Sequence sequence = DOTween.Sequence();
|
|
|
|
// 设置初始位置为屏幕外或者其他你想要的位置(根据需求自行设置)
|
|
bookPos.anchoredPosition =
|
|
new Vector2(bookPos.anchoredPosition.x, -(Screen.height + bookPos.sizeDelta.y) / 2);
|
|
|
|
// 移动到目标位置
|
|
sequence.Append(bookPos.DOAnchorPos(Vector2.zero, 0.5f).SetEase(Ease.OutQuad));
|
|
|
|
// 等待序列完成
|
|
yield return sequence.WaitForCompletion();
|
|
}
|
|
|
|
[YarnCommand("Flip")]
|
|
public IEnumerator FlipToPageYarnCommand(int parameters)
|
|
{
|
|
// Parse the target page from the parameters
|
|
int targetPage = parameters;
|
|
// Start the FlipToPageCoroutine and wait for it to finish
|
|
yield return StartCoroutine(flipmanager.FlipToPageCoroutine(targetPage));
|
|
}
|
|
|
|
// Close the book
|
|
public void CloseBook(Action onCloseCallback = null)
|
|
{
|
|
StartCoroutine(CloseBookCoroutine(onCloseCallback));
|
|
}
|
|
|
|
private IEnumerator CloseBookCoroutine(Action onCloseCallback = null)
|
|
{
|
|
// 使用 DoTween 创建一个动画序列
|
|
Sequence sequence = DOTween.Sequence();
|
|
|
|
// 动画:将 book 移动回初始位置
|
|
sequence.Append(bookPos
|
|
.DOAnchorPos(
|
|
new Vector2(bookPos.anchoredPosition.x, -(Screen.height + bookPos.sizeDelta.y) / 2), 0.5f)
|
|
.SetEase(Ease.InQuad));
|
|
|
|
// 等待动画完成
|
|
yield return sequence.WaitForCompletion();
|
|
|
|
// 动画完成后隐藏 book
|
|
book.gameObject.SetActive(false);
|
|
|
|
OnBookClosed?.Invoke();
|
|
}
|
|
} |