Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: empty on June 21, 2013, 02:54:55 PM

Title: Tweening a Slider's Foreground
Post by: empty on June 21, 2013, 02:54:55 PM
I'm working on creating a resource bar (health, mana, etc) using NGUI's UISlider (as a progressbar). I'm updating sliderValue to change the foreground, but this causes the foreground to jump. Is there a way to use tweening to soften the changes? My guess would be adding the component to the foreground and destroying it once the animation is done or updating it if the sliderValue changes again. But if I add the component manually, how do I know what the scale size should be?

I took a look at the documentation and didn't see any hints toward tweening it and Google is "catering" my results too much to be useful. Below is a driver and the resource bar script. Ideally the tweening would be inside UpdateResource()

  1. #pragma strict
  2.  
  3. public var bar : ResourceBar;
  4.  
  5. function Start() {
  6.         bar.SetResource(125, 125);
  7. }
  8.  
  9. private var lastUpdate : long;
  10. private var updateGap  : int = 3;
  11.  
  12. function Awake() {
  13.         lastUpdate = Time.time + updateGap;
  14. }
  15.  
  16. function Update () {
  17.         if(Time.time >= lastUpdate) {
  18.                 bar.SetResource( Random.Range(0, maxResource) );
  19.                 lastUpdate = Time.time + updateGap;
  20.         }
  21. }
  22.  

  1. #pragma strict
  2.  
  3. public var progressBar : UISlider;
  4. public var foreground  : UISprite;
  5. public var label       : UILabel;
  6.  
  7. private var currentResource : int;
  8. private var maxResource : int;
  9.  
  10. function SetResource(current : int, max : int) {
  11.         maxResource = max;
  12.         SetResource(current);
  13. }
  14.  
  15. function SetResource(current : int) {
  16.         currentResource = current;
  17.         UpdateResource();
  18. }
  19.  
  20. private function UpdateResource() {
  21.         label.text = "" + currentResource + " / " + maxResource;
  22.         // TODO: Tweening
  23.         progressBar.sliderValue = Mathf.Clamp(currentResource * 1f / maxResource, 0, 1);
  24. }
  25.  
Title: Re: Tweening a Slider's Foreground
Post by: ArenMook on June 22, 2013, 06:21:04 AM
You can do your own tweening like so:

  1. void Update()
  2. {
  3.     progressBar.sliderValue = Mathf.Lerp(progressBar.sliderValue, targetValue, Time.deltaTime * 5f);
  4. }