Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: themouse on December 19, 2012, 05:20:47 PM

Title: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 19, 2012, 05:20:47 PM
Hello,

I am trying to access the Progress Bar's foreground Color Tint Value in C#

Basically I am trying to implement my own version of BurgZerg's Vital Bar.
I wanted to try a different approach. :)

Basically I want a single script that gets put on my GameObject called "Vital Bar",
That came object contains the progressbar & a label.
WHat I am doing different is I want Public values for the Text and the Foreground Color
(so they are visible in the inspector) , it is proving to be a pia. SO annoying Unity cannot display public properties in the inspector.

Any I cannot see to access the Color Tint property on the foreground value.
So far the text on the label is fine.

Eventually I would like to come up with a new NGUI Control built up of NGUI controls.
Think UserControl in C#/VB.Net windows forms.

I would like to reuse this thing all over the place.
Health/Mana. for 1-4 players. and then as a health bar above enemies.

Can someone tell this Unity/NGUI newb where he is being dumb? :D


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class VitalBar: MonoBehaviour {
  5.         private UISlider _sliderCtrl;
  6.         private UILabel _labelCtrl;    
  7.        
  8.         private string _label;
  9.         public string Label;
  10.        
  11.         private Color _vitalColor;
  12.         public Color VitalColor;
  13.                
  14.         void Awake()
  15.         {
  16.                
  17.                 _sliderCtrl = transform.FindChild ("ProgressBar").GetComponent<UISlider>();
  18.                 _labelCtrl = transform.FindChild ("Label").GetComponent<UILabel>();
  19.                 SetControlValues();
  20.                                
  21.         }
  22.        
  23.         void Update()
  24.         {
  25.                 SetControlValues();
  26.                
  27.         }
  28.                
  29.         private void SetControlValues()
  30.         {
  31.                 if( _label != Label)
  32.                 {
  33.                         _labelCtrl.text = Label;
  34.                         _label = Label;
  35.                 }
  36.                
  37.                 if( _vitalColor != VitalColor)
  38.                 {              
  39.                         //_slider.foreground. ??? where/what/how ??? = VitalColor
  40.                         _vitalColor = VitalColor;
  41.                 }
  42.         }
  43. }
  44.  
  45.  
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: dlewis on December 19, 2012, 07:15:38 PM
slider.foreground is a Transform. To change the colour you need to modify the right value on a widget.

Step 1: Get the widget
  1. UIWidget widget = slider.foreground.GetComponent<UIWidget>();

Step 2: Set the color
  1. if (widget != null)
  2. {
  3.     widget.color = VitalColor;
  4. }
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 19, 2012, 08:04:15 PM
Oh duh! LOL.

Thanks.
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 20, 2012, 03:00:52 AM
Still not quite right. And I am confused as to why its not working.
I can see all the objects, debug.logging shows the values are set correctly.

The foreground bar is no where to be seen in the game window.

I was having alot of issues with NGUI for a bit today, I would add a NGUI Camera and it was backwards or behind it. Updated to the latest version and that stopped.

If anyone is interested in telling me what bone-headed thing I am doing wrong.

http://www.filefactory.com/file/775rc07hmzhp/n/VitalBar_unitypackage (http://www.filefactory.com/file/775rc07hmzhp/n/VitalBar_unitypackage)

Here is a package with all the assets and code. you will need to create a new project with NGUI and add this.

Was planning on sharing it anyway.

Suggestions, comments all welcome. I'm married I can handle criticism. :)

thanks,
Adam


Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 20, 2012, 03:54:55 PM
Anyone?  I have no idea why it disappears. :'(

Actually I comment the code that modifies anything and the foreground still is missing.
So it has to be something with the Progress Bar.  Perhaps how its in the Prefab or something.

Current code

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class VitalBar: MonoBehaviour {
  6.        
  7.         // pointers to the child controls
  8.         private UISlider _sliderCtrl;
  9.         private UILabel _labelCtrl;    
  10.        
  11.         // using a private variable and a public variable that can be scene
  12.         // in the inspector, and checking that in the update loop. we can simulate
  13.         // properties for Unity3D. latebound properties for the lack of a better term.
  14.        
  15.         // private value for Label, this way we can see when it changes.
  16.         private string _label;
  17.         // public value for Label, for the inspector.
  18.         public string Label;
  19.        
  20.         // private value for VitalColor, this way we can see when it changes.
  21.         private Color _vitalColor;
  22.         // public value for Label, for the inspector.
  23.         public Color VitalColor;
  24.                
  25.         // private value for Value, this way we can see when it changes.
  26.         private int _value = 10;
  27.         // public value for Value, for the inspector.
  28.         public int Value = 10;
  29.        
  30.         // private value for MaxValue, this way we can see when it changes.
  31.         private int _maxValue = 10;
  32.         // public value for MaxValue, for the inspector.
  33.         public int MaxValue = 10;
  34.        
  35.         //Max with of the foreground component of the progress bar control.
  36.         private float _maxWidth = 0.0f;
  37.        
  38.         //flag to detect the first time this is in the update loop.
  39.         private bool _firstTime = true;
  40.        
  41.         //flag to detect if all the controls have been initialized.
  42.         private bool _vitalBarInitialized;
  43.        
  44.         // private value for percentFull, this way we can see when it changes.
  45.         private float _percentFull;
  46.        
  47.         void Awake()
  48.         {
  49.                 //grab the pointers to the child object
  50.                 _sliderCtrl = transform.FindChild ("ProgressBar").GetComponent<UISlider>();
  51.                 _labelCtrl = transform.FindChild ("Label").GetComponent<UILabel>();
  52.                
  53.                 //set the flag to see if they have been referenced.
  54.                 _vitalBarInitialized = (_sliderCtrl!=null && _labelCtrl!= null);
  55.                 _firstTime = true;
  56.                
  57.                 //if the controls are there then procede with first update
  58.                 if(_vitalBarInitialized)
  59.                 {
  60.                        
  61.                         Debug.Log ("Init:" + _sliderCtrl.sliderValue);
  62.                         SetControlValues();
  63.                         _maxWidth = _sliderCtrl.foreground.localScale.x;       
  64.                 }
  65.                 else
  66.                 {
  67.                         //if we are here we failed
  68.                         Debug.Log ("Unable to initialize. Missing Sub Controls!");     
  69.                 }
  70.                
  71.         }
  72.        
  73.         void Update()
  74.         {
  75.                 SetControlValues();
  76.         }
  77.        
  78.         //this function does double duty.
  79.         //First it checks to see if there have been changes to any of the
  80.         //latebound properties, if they have been changed set them. (do nothing and do not update
  81.         //any of the gui controls if no changes have been made.
  82.         //Second it computes the changes (if any to the percent full) and adjust the width of the
  83.         //foreground.
  84.         private void SetControlValues()
  85.         {
  86.                 // if not initialized then quit method.
  87.                 if (_vitalBarInitialized)
  88.                 {
  89.                        
  90.                         if(_maxWidth == 0 && _firstTime)                                       
  91.                                 _maxWidth = _sliderCtrl.foreground.localScale.x;                       
  92.  
  93.                         if( _vitalColor != VitalColor)
  94.                         {              
  95.                                 UIWidget widget = _sliderCtrl.foreground.GetComponent<UIWidget>();
  96.                                 if (widget != null)
  97.                                 {
  98.                                     widget.color = VitalColor;
  99.                                         //set the private property
  100.                                         _vitalColor = VitalColor;
  101.                                 }
  102.                         }
  103.        
  104.                         if(_maxValue != MaxValue)
  105.                         {
  106.                                 //set the private property
  107.                                 _maxValue = MaxValue;  
  108.                         }
  109.                        
  110.                         //Check the range of Value
  111.                         if(Value < 0)
  112.                         {
  113.                                 Value = 0;
  114.                         }
  115.                         else if(Value>_maxValue)
  116.                         {
  117.                                 Value = _maxValue;
  118.                         }
  119.                        
  120.                         if( (_value != Value) || _firstTime)
  121.                         {      
  122.                                 float percentFull=0;
  123.                                 if(_maxValue>0)
  124.                                 {
  125.                                         percentFull = (float)Value / (float)_maxValue;
  126.                                 }
  127.        
  128.                                 Debug.Log("percentFull:" + percentFull);
  129.                                 Debug.Log("_maxWidth:" + _maxWidth);
  130.                                 Debug.Log("_maxWidth * percentFull:" + _maxWidth * percentFull);
  131.                                        
  132.                                 if(_percentFull!=percentFull)
  133.                                 {
  134.                                         Debug.Log ("Before:" + _sliderCtrl.sliderValue);
  135.                                         _sliderCtrl.sliderValue = percentFull;
  136.                                         //_sliderCtrl.foreground.localScale = new Vector3(_maxWidth * percentFull,_sliderCtrl.foreground.localScale.y, _sliderCtrl.foreground.localScale.z); ;
  137.                                         Debug.Log ("After:" +  _sliderCtrl.sliderValue);
  138.                                        
  139.                                         //set the private property
  140.                                         _percentFull = percentFull;
  141.                                         _value = Value;
  142.                                 }
  143.                         }
  144.                        
  145.                         //if the text has changed change the text on the label control.
  146.                         if( _label != Label)
  147.                         {
  148.                                 _labelCtrl.text = Label + " " + _percentFull * 100 + " %";
  149.                                 //set the private property.
  150.                                 _label = Label;
  151.                         }
  152.                        
  153.                         //set the first time flag
  154.                         _firstTime = false;
  155.                 }
  156.         }
  157. }
  158.  
  159.  
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: Nicki on December 20, 2012, 04:03:50 PM
Turn on gizmos, see if it's actually in the place you think it should be.

Check if it's in the correct unity layer.

Double click it with the scene view open and see where it goes in the scene (might be moved some place weird).

Are you using NGUI free or the bought one?
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 20, 2012, 05:49:49 PM
Thank you responding.

Quote
Turn on gizmos, see if it's actually in the place you think it should be.
Gizmos is on. When I select it the gizmo is where I think it would be, but there is nothing highlighted. For Example when I select the label or the background objects a blue line highlights the item. Not so for the foreground.

Quote
Check if it's in the correct unity layer.
I checked its in correct layer, the VitalBar gameobject that holds everything was not. I fixed that but there is no change.

Quote
Double click it with the scene view open and see where it goes in the scene (might be moved some place weird).
It appears to be in the correct place in the scene. But it either has no width, or is not visible. (Note the x, y, z) properties are correct. So it should be there, the color has changed, the values change if I change them. As far as I can tell it should be there.

Quote
Are you using NGUI free or the bought one?
Purchased one, updated to the latest version, and with unity3d 4.0 free version, windows 7.

I just don't get it. Everything looks ok in the inspector. Debug.Log shows me all the values are correct.  :'(

Thanks again for your help, I'll keep working on it, its probably going to be something dumb.
Adam
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: Nicki on December 20, 2012, 05:57:58 PM
Make sure the foreground isn't scaled to 0 on any axis.

Try taking some screenshots of your inspector values, we might be able to glean something from it.

If your panel is static, turn that off.
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: ArenMook on December 20, 2012, 11:22:53 PM
Most common reason for not seeing anything like this is having your scene view camera set up on the wrong axis. Switch it to 3D, select your widget, press F with the mouse hovering over the scene view to focus on it. If it's still not visible, start zooming in.
Title: Re: Accessing Progress Bar's foreground Color Tint Value in C#
Post by: themouse on December 21, 2012, 01:13:55 AM
Hello everyone,

I got it working! Thanks for all of your help.
It turned out to be some initialization logic and the alpha channel.

Couple of things I would like to change if I can. But it may be the way I am doing properties.
But you can set all the values in the inspector. You wont see them till runtime.

And the label's font.
1) it doesn't respond well to changing the prefab, too small and it gets blurry.
2) I would like to be able to modify the text alignment with an inspector dropdown from the VitalBar. Right/Left and Center. Changing the pivot moves the label to the wrong places.