Hello!
Im trying to figure out how to make these sliders slide slowly up to 100% after i click the button.
And also how to slowly decrease to 0% with same button, but = !PCharge0.value = 0; wont work
Code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PhaActi : MonoBehaviour {
public Slider PCharge0;
public Slider PCharge1;
public Slider PCharge2;
public Slider PCharge3;
public void ClickButton(string buttonID)
{
PCharge0.value = 100;
PCharge1.value = 100;
PCharge2.value = 100;
PCharge3.value = 100;
}
}
Right now your button click sets the values to 100. You would have to trigger some type of loop to get it to basically assign a value from 100 - 0 over time. However, may I suggest looking into a tween engine. Most are free. The one I use is leantween where you can declare a start and end value and a time to take to move between them. Then there is a setonupdate call that lets you set the current value to the slider as it moves between the values.
cdId = LeanTween.value(gameObject, cdSlider.maxValue, 0, cdSlider.maxValue).setEase(LeanTweenType.linear).setOnUpdate((float val) =>
{
cdSlider.value = val;
}).setOnComplete(() =>
{
cdEnd();
}).id;
Here is an example of how it would work with leanTween. Basically taking the maxvalue of the slider and wanting to move towards 0. I use the maxValue to also determine time it takes to move there. EaseType just determines how it moves. (different ease types allow for burst of speed at the start or end, etc).
setOnUpdate then takes the float which is whatever value the leantween is currently at and I assign that to the slider. So it starts with the maxValue and then slowly reduces it.
setOnComplete will run something once the leanTween has completed the task and reached the end value(in this case 0).
By getting the id and assigning that to an int, I can use that id to pause, resume, or even cancel the leantween.
There is also iTween and dotTween, but they will have different ways of doing this.
I actually use leantween on a circular slider to simulate a cooldown on a skill and it works really well.