首先,我对于动画师和动画系统的统一性非常了解。
我要实现的目标(并且我尝试使用viewPager.setAdapter(viewPagerAdapter);
组件)是一种随机攻击,只有当我按住鼠标按钮并压在敌人身上时,才能完成正在播放的攻击片段的执行即使我同时松开了按钮。
我尝试将2种攻击动画添加到列表中并使用
播放Animator
...但是我不知道问题是否在于当我不断按下一个动画时会取消另一个动画或什么。
因此,我更愿意问,因为我可能以错误的方式做事。
谢谢。
答案 0 :(得分:1)
是的,问题可能就是您所说的:您必须等到一个动画结束后才能开始新的动画,否则您将在每一帧开始一个新的动画。
您可以使用Coroutine(也请检查API)来做到这一点。
当然,即使不使用协程,也只能在Update
中实现相同的操作,但是大多数时候情况变得非常混乱,有时甚至处理起来更加复杂。而且,仅将其“导出”到协程中并没有任何损失或收益(关于性能)。
// Reference those in the Inspector or get them elsewhere
public List<AnimationClip> Clips;
public AnimationClip Idle;
private Animator _anim;
// A flag to make sure you can never start the Coroutine multiple times
private bool _isAnimating;
private void Awake()
{
_anim = GetComponent<Animator>();
}
private void Update()
{
if(Input.GetMouseButtonDown(0)
{
// To make sure there is only one routine running
if(!_isAnimating)
{
StartCoroutine(RandomAnimations());
}
}
// This would immediately interrupt the animations when mouse is not pressed anymore
// uncomment if you prefer this otherwise the Coroutine waits for the last animation to finish
// and returns to Idle state afterwards
//else if(Input.GetMouseButtonUp(0))
//{
// // Interrupts the coroutine
// StopCoroutine (RandomAnimations());
//
// // and returns to Idle state
// _anim.Play(Idle.name);
//
// // Reset flag
// _isAnimating = false;
//}
}
private IEnumerator RandomAnimations()
{
// Set flag to prevent another start of this routine
_isAnimating = true;
// Go on picking clips while mouse stays pressed
while(Input.GetMouseButton(0))
{
// Pick random clip from list
var randClip = Clips[Random.Range(0, Clips.Count)];
// Switch to the random clip
_anim.Play(randClip.name);
// Wait until clip finished before picking next one
yield return new WaitForSeconds(randClip.length);
}
// Even if MouseButton not pressed anymore waits until the last animation finished
// then returns to the Idle state
// If you rather want to interrupt immediately if the button is released
// skip this and uncomment the else part in Update
_anim.Play(Idle.name);
// Reset flag
_isAnimating = false;
}
请注意,这种随机方式不会提供诸如“不要连续播放同一动画两次”或“在重复一个动画之前先播放所有动画”之类的东西。
如果您想要此结帐this answer to a very similar question。在那里我使用了随机列表进行遍历,所以没有双打