The solution provided by Vfxjex doesn’t work for me because the Animation component has to be marked as Legacy. I try this solution using Animator instead of Animation and it works for me:
using UnityEngine;
using UnityEngine.UI;
public class ControlAnimation : MonoBehaviour
{
private Animator anim;
public Slider slider; //Assign the UI slider of your scene in this slot
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
anim.speed = 0;
}
// Update is called once per frame
void Update()
{
anim.Play("YourAnimationName", -1, slider.normalizedValue);
}
}
Now you can set the 10.0f value to be the value you set by your slider. Something like below in C#:
using UnityEngine.UI; // Needs to be included at the top in order to be able to access the new UI elements.
Slider mySlider; // Assign your slider you want to use to control the animation time.
animation["AnimationClip"].time = mySlider.value;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimControl : MonoBehaviour {
Animation anim;
public Slider slider;
// Use this for initialization
void Start () {
anim = GetComponent<Animation> ();
anim.Play ("SphereAnim");
anim ["SphereAnim"].speed = 0;
}
// Update is called once per frame
void Update () {
anim["SphereAnim"].time = slider.value;
}
}
you need to play your animation so you can update via slider and just make the animation speed to zero.