79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using UnityEngine;
|
||
|
||
public class ModelController : MonoBehaviour
|
||
{
|
||
private Animator animator; // 用于控制动画
|
||
public delegate void AnimationFinished(); // 动画结束事件
|
||
public event AnimationFinished OnAnimationFinished; // 动画结束事件
|
||
|
||
// 脚本启动时调用
|
||
void Start()
|
||
{
|
||
animator = GetComponent<Animator>();
|
||
if (animator == null)
|
||
{
|
||
Debug.LogError($"模型 {gameObject.name} 未找到Animator组件");
|
||
}
|
||
}
|
||
|
||
// 播放动画
|
||
public void PlayAnimation()
|
||
{
|
||
if (animator != null)
|
||
{
|
||
animator.Play("Base Layer.AnimationName", 0, 0f); // 替换为实际动画名称
|
||
Debug.Log($"模型 {gameObject.name} 开始播放动画");
|
||
}
|
||
}
|
||
|
||
// 设置动画进度
|
||
public void SetAnimationProgress(float progress)
|
||
{
|
||
if (animator != null)
|
||
{
|
||
animator.Play("Base Layer.AnimationName", 0, progress); // 替换为实际动画名称
|
||
animator.speed = 0f; // 暂停动画以保持进度
|
||
Debug.Log($"模型 {gameObject.name} 设置动画进度: {progress:F2}");
|
||
}
|
||
}
|
||
|
||
// 重置动画
|
||
public void ResetAnimation()
|
||
{
|
||
if (animator != null)
|
||
{
|
||
animator.Play("Base Layer.AnimationName", 0, 0f); // 替换为实际动画名称
|
||
animator.speed = 1f; // 恢复正常播放速度
|
||
Debug.Log($"模型 {gameObject.name} 重置动画");
|
||
}
|
||
}
|
||
|
||
// 检查动画是否正在播放
|
||
public bool IsPlayingAnimation()
|
||
{
|
||
if (animator != null)
|
||
{
|
||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||
return stateInfo.normalizedTime < 1.0f && animator.speed > 0f;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
// 获取当前动画进度
|
||
public float GetAnimationProgress()
|
||
{
|
||
if (animator != null)
|
||
{
|
||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||
return Mathf.Clamp01(stateInfo.normalizedTime);
|
||
}
|
||
return 0f;
|
||
}
|
||
|
||
// 动画结束时调用(需在Animator中设置)
|
||
public void OnAnimationEnd()
|
||
{
|
||
OnAnimationFinished?.Invoke();
|
||
Debug.Log($"模型 {gameObject.name} 动画播放完成");
|
||
}
|
||
} |