133 lines
3.4 KiB
C#
133 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PlayAni : MonoBehaviour
|
|
{
|
|
[Header("按钮设置")]
|
|
[SerializeField] private Button playAniButton; // 播放动画按钮
|
|
[SerializeField] private Button playVideoButton; // 播放视频按钮
|
|
[SerializeField] private Slider progressSlider; // 动画进度条
|
|
|
|
[Header("动画物体")]
|
|
[SerializeField] private Animator prefabAnimator; // 场景中直接拖拽的物体
|
|
|
|
private bool isPlaying = false;
|
|
private float animationLength; // 动画时长(秒)
|
|
private float currentTime = 0f; // 当前播放时间
|
|
|
|
void Start()
|
|
{
|
|
// 初始化按钮事件
|
|
ButtonOnclick();
|
|
|
|
if (prefabAnimator != null)
|
|
{
|
|
InitializeSlider();
|
|
|
|
// 防止一开始自动播放
|
|
prefabAnimator.Play(0, 0, 0);
|
|
prefabAnimator.speed = 0;
|
|
isPlaying = false;
|
|
}
|
|
else
|
|
{
|
|
progressSlider.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (prefabAnimator == null) return;
|
|
|
|
if (isPlaying)
|
|
{
|
|
currentTime += Time.deltaTime;
|
|
|
|
if (currentTime > animationLength)
|
|
{
|
|
currentTime = animationLength;
|
|
isPlaying = false;
|
|
}
|
|
|
|
float normalizedTime = currentTime / animationLength;
|
|
prefabAnimator.Play(0, 0, normalizedTime);
|
|
|
|
progressSlider.SetValueWithoutNotify(normalizedTime * 100f);
|
|
}
|
|
}
|
|
|
|
private void ButtonOnclick()
|
|
{
|
|
// 播放动画按钮
|
|
playAniButton.onClick.RemoveAllListeners();
|
|
playAniButton.onClick.AddListener(() =>
|
|
{
|
|
if (prefabAnimator == null) return;
|
|
|
|
progressSlider.gameObject.SetActive(true); // 显示 Slider
|
|
PlayPrefabAni();
|
|
});
|
|
|
|
// 播放视频按钮
|
|
playVideoButton.onClick.RemoveAllListeners();
|
|
playVideoButton.onClick.AddListener(() =>
|
|
{
|
|
ResetAnimationToStart(); // 重置动画到 0 帧
|
|
progressSlider.gameObject.SetActive(false); // 隐藏 Slider
|
|
|
|
});
|
|
}
|
|
|
|
private void InitializeSlider()
|
|
{
|
|
if (progressSlider == null) return;
|
|
|
|
progressSlider.minValue = 0;
|
|
progressSlider.maxValue = 100;
|
|
progressSlider.value = 0;
|
|
|
|
// 获取动画时长
|
|
AnimatorClipInfo[] clips = prefabAnimator.GetCurrentAnimatorClipInfo(0);
|
|
animationLength = (clips.Length > 0) ? clips[0].clip.length : 0;
|
|
|
|
// 添加拖动事件
|
|
progressSlider.onValueChanged.RemoveAllListeners();
|
|
progressSlider.onValueChanged.AddListener(OnSliderValueChanged);
|
|
|
|
progressSlider.gameObject.SetActive(false); // 初始隐藏
|
|
}
|
|
|
|
private void PlayPrefabAni()
|
|
{
|
|
if (prefabAnimator == null) return;
|
|
|
|
currentTime = 0f;
|
|
isPlaying = true;
|
|
}
|
|
|
|
private void OnSliderValueChanged(float value)
|
|
{
|
|
if (prefabAnimator == null) return;
|
|
|
|
float normalizedTime = value / 100f;
|
|
currentTime = normalizedTime * animationLength;
|
|
|
|
prefabAnimator.Play(0, 0, normalizedTime);
|
|
prefabAnimator.speed = 0;
|
|
isPlaying = false;
|
|
}
|
|
|
|
private void ResetAnimationToStart()
|
|
{
|
|
if (prefabAnimator == null) return;
|
|
|
|
currentTime = 0f;
|
|
isPlaying = false;
|
|
prefabAnimator.Play(0, 0, 0); // 回到第一帧
|
|
prefabAnimator.speed = 0;
|
|
progressSlider.value = 0;
|
|
}
|
|
}
|