How to Control an Animation of 3D Object Using Sli

2019-08-27 15:58发布

Let's Say that we have a 3d game object on our scene with single Animation on it so what should i have to do to control its Animation using slider like if change the values of slider the Animation will play according to that values

2条回答
闹够了就滚
2楼-- · 2019-08-27 16:38

I assume that you have created an animator with their animations.

You only have to know that your callback onValueChanged is called every time you modify the slider value. So there is where you want to set the new animation modes.

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class SliderAnimator : MonoBehaviour
{
    public Slider mainSlider;
    public Animator anim;
    public void Start()
    {
        //Adds a listener to the main slider and invokes a method when the value changes.
        mainSlider.onValueChanged.AddListener(delegate {ValueChangeCheck(); });
    }

    // Invoked when the value of the slider changes.
    public void ValueChangeCheck()
    {           
        //Here we set the animation            
        switch((int)mainSlider.value){
          case 0:
            //Set first animation
            anim.SetBool("FirstAnimationName", true);
            break;
          case 1:
            //Set second animation
            anim.SetBool("SecondAnimationName", true);
            break;
          default:            
            break;
        }

        //To avoid casting the mainSlider.value
        if(mainSlider.value >= 0 && mainSlider.value < 0.5f)
        {
            //Set first animation
            anim.SetBool("FirstAnimationName", true);
        }
        if(mainSlider.value >= 0.5f && mainSlider.value <= 1f)
        {
            //Set second animation
            anim.SetBool("SecondAnimationName", true);
        }
    }
}
查看更多
Evening l夕情丶
3楼-- · 2019-08-27 16:59

I suppose you want something similar to a VideoPlayer(youtube video player for example) but with animations. Start here: https://docs.unity3d.com/ScriptReference/AnimationState-time.html

using UnityEngine;
using System.Collections;

    public class ExampleScript : MonoBehaviour
    {
        public Animation anim;
        public float animationTime;// attach your slider value to this float

        void Update()
        {
            anim["Walk"].time = animationTime;
        }
    }
查看更多
登录 后发表回答