如何统一使用Slider控制3D对象的动画 [英] How to Control an Animation of 3D Object Using Slider in unity

查看:201
本文介绍了如何统一使用Slider控制3D对象的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们的场景上有一个3d游戏对象,上面有单个动画,因此我应该怎么做才能使用滑块控制其动画,例如如果更改动画的滑块值将根据该值进行播放

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

推荐答案

我假设您已经创建了带有其动画的动画制作器。

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

您只需要知道每次修改滑块值时都会调用回调onValueChanged。因此,您可以在其中设置新的动画模式。

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);
        }
    }
}

这篇关于如何统一使用Slider控制3D对象的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆