当前位置:   article > 正文

Untiy中控制Animation的播放速度_untiy中实时控制animation的播放速度

untiy中实时控制animation的播放速度

Unity组件里面的Animation是可以控制播放速度的,通过调整AnimationState的speed属性即可更改某个动画的速度。

在实际需求中,可以通过改变动画速度来更快地打开一个奖励或者过场等等,这样可以让用户减少等待时间,提升体验感。

官方也给出了相关的控制接口

https://docs.unity3d.com/ScriptReference/Animation.html

speed=1为缺省速度,大于1为加速,可以按照实际来改变速度。

  1. anim = GetComponent<Animation>();
  2. foreach (AnimationState state in anim)
  3. {
  4. state.speed = 0.5F;
  5. }

或者控制某个单独的

  1. public void SetAnimationSpeed(Animation ani, string name, float speed)
  2. {
  3. if (null == ani) return;
  4. AnimationState state = ani[name];
  5. if (!state) state.speed = speed;
  6. }

修改Animator的播放速度会影响整个状态机的速度

  1. public void SetAnimatorSpeed(Animator animator, float speed)
  2. {
  3. if (null == animator) return;
  4. animator.speed = speed;
  5. }

一般不要这样改,直接改clip的速度,或者改状态机中state的速度

在lua中写这样的接口:

注意到,获取AnimationState的属性

提供了一个get属性的this字段,利用这个字段可以改变速度:

  1. function SetAnimationSpeed(animation, name, speed)
  2. if animation == nil then return end
  3. local state = animation.this:get(name)
  4. if state then state.speed = speed end
  5. end

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/103551
推荐阅读
相关标签
  

闽ICP备14008679号