73 lines
2.4 KiB
C#
73 lines
2.4 KiB
C#
using DG.Tweening;
|
||
using UnityEngine;
|
||
|
||
namespace Ichni.Story.UI
|
||
{
|
||
/// <summary>
|
||
/// 对话时常驻转动的齿轮/轮盘,以及伴随浮动的指针。
|
||
/// </summary>
|
||
public class DialogWheel : MonoBehaviour
|
||
{
|
||
[Header("轮盘旋转")]
|
||
[Tooltip("要旋转的 RectTransform。如果不填,默认获取自身。")]
|
||
public RectTransform wheelRect;
|
||
|
||
[Tooltip("顺时针旋转一整圈(360度)所需的时间(秒)")]
|
||
public float rotationCycleDuration = 5f;
|
||
|
||
[Header("指针浮动")]
|
||
[Tooltip("常驻上下浮动的指针对象 (RectTransform)")]
|
||
public RectTransform pointerRect;
|
||
|
||
[Tooltip("指针单向浮动的距离(像素)")]
|
||
public float pointerFloatDistance = 5f;
|
||
|
||
[Tooltip("指针完成单向浮动所需的时间")]
|
||
public float pointerFloatDuration = 0.3f;
|
||
|
||
private Tween _rotateTween;
|
||
private Tween _pointerTween;
|
||
private float _pointerStartY;
|
||
|
||
private void Awake()
|
||
{
|
||
if (wheelRect == null) wheelRect = GetComponent<RectTransform>();
|
||
|
||
if (pointerRect != null)
|
||
{
|
||
_pointerStartY = pointerRect.anchoredPosition.y;
|
||
}
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
// 轮盘常驻匀速旋转
|
||
if (wheelRect != null && rotationCycleDuration > 0f)
|
||
{
|
||
// SetRelative(true) 保证无论当前角度是多少,都在其基础上转 -360 度
|
||
// LoopType.Restart 配合 Linear 缓动,实现无限丝滑旋转
|
||
_rotateTween = wheelRect.DORotate(new Vector3(0, 0, -360f), rotationCycleDuration, RotateMode.FastBeyond360)
|
||
.SetRelative(true)
|
||
.SetLoops(-1, LoopType.Restart)
|
||
.SetEase(Ease.Linear);
|
||
_rotateTween.Play();
|
||
}
|
||
|
||
// 指针常驻浮动,启动后永不停止
|
||
if (pointerRect != null)
|
||
{
|
||
_pointerTween = pointerRect.DOAnchorPosY(_pointerStartY + pointerFloatDistance, pointerFloatDuration)
|
||
.SetLoops(-1, LoopType.Yoyo)
|
||
.SetEase(Ease.InOutSine);
|
||
_pointerTween.Play();
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
_rotateTween?.Kill();
|
||
_pointerTween?.Kill();
|
||
}
|
||
}
|
||
}
|