赞
踩
在某些应用场景中可能需要用Slider控制动画的播放进度,什么点击啊,拖拽啊,搜索一番发现都是使用的是Animation,毕竟直接有API可以调用相当的方便,可Animation是旧版的东西了,今天来说说怎么控制Animator动画,代码还是很简单的
public class UISlider :MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerDownHandler { bool drag = false; public Animator Animator; AnimatorStateInfo animatorStateInfo; public Slider component public void SetAnimTarget(Animator target) { Animator = target; } private void OnEnable() { component.value = 0; drag = false; } private void Update() { if (!drag) { if (Animator) { //获取当前动画片段信息 animatorStateInfo = Animator.GetCurrentAnimatorStateInfo(0); //normalizedTime 是个0-1的值 代表进度 component.value = animatorStateInfo.normalizedTime; } else { Hide(); } } } public void OnBeginDrag(PointerEventData eventData) { drag = true; } public void OnEndDrag(PointerEventData eventData) { drag = false; } public void UpdateAuto_Anim(float normalizedTime) { animatorStateInfo = Animator.GetCurrentAnimatorStateInfo(0); //从指定进度开始播放 Animator.Play(animatorStateInfo.fullPathHash, 0, normalizedTime); } public void OnDrag(PointerEventData eventData) { drag = true; UpdateAuto_Anim(component.value); } public void OnPointerDown(PointerEventData eventData) { UpdateAuto_Anim(component.value); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。