49 lines
1.2 KiB
C#
49 lines
1.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class AnimationController : MonoBehaviour
|
|
{
|
|
private Animation animationComponent;
|
|
private bool isPlaying = false; // 用于跟踪动画是否正在播放
|
|
private bool loopAnimation = true;
|
|
|
|
void Start()
|
|
{
|
|
animationComponent = GetComponent<Animation>();
|
|
}
|
|
|
|
public void PlayAnimation(string animationName)
|
|
{
|
|
if (!isPlaying) // 如果动画当前未在播放
|
|
{
|
|
StartCoroutine(PlayAnimationLoop(animationName));
|
|
}
|
|
}
|
|
|
|
IEnumerator PlayAnimationLoop(string animationName)
|
|
{
|
|
isPlaying = true; // 标记动画正在播放
|
|
loopAnimation = true;
|
|
while (loopAnimation)
|
|
{
|
|
animationComponent.Play(animationName);
|
|
|
|
// 等待动画播放完毕
|
|
yield return new WaitForSeconds(animationComponent[animationName].length/2);
|
|
|
|
// 检查是否应该继续循环
|
|
if (!loopAnimation)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
isPlaying = false; // 标记动画播放结束
|
|
}
|
|
|
|
// 调用此方法来停止循环播放动画
|
|
public void StopAnimationLoop()
|
|
{
|
|
loopAnimation = false;
|
|
}
|
|
} |