当前位置:网站首页>Slider controls the playback progress of animator animation

Slider controls the playback progress of animator animation

2022-06-24 21:34:00 Nangongming

Preface

In some application scenarios, you may need to use Slider Control the playback progress of animation , What click , Drag , Search and find that you are using Animation, After all, there are API It is quite convenient to call , can Animation It's an old version , Today, let's talk about how to control Animator Animation , The code is simple

Code

	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)
				{
				  // Get the current animation clip information 
					animatorStateInfo = Animator.GetCurrentAnimatorStateInfo(0);
					//normalizedTime  It's a 0-1 Value   Representative progress 
					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);
			// Play from the specified progress 
			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);
		}

	}
原网站

版权声明
本文为[Nangongming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241440056570.html