Found some interesting conditions regarding UIProgressBar. Had to make some changes to the UIProgressBar.cs script that I would like to share here, as I think they could be valid updates to it overall.
Silent Value ChangingIn our project we have various UISliders (which are derived from UIProgressBar). The values are changed by the player, then saved. When the player returns to that dialog, the sliders need to be the same value where they were left. We ran into issues where we would change the value of the progress bar in script, and since the value changed - the OnChange event would get toggled. This led to some various ugly situations. The best answer we found was to add a method to UIProgressBar which would allow changing the value property, without toggling the OnChange event. That code is as follows. It's pretty much the same as the value property, only does not call the event.
/// <summary>
/// Changes the value without toggling the callback. Use for when changing the value in script
/// </summary>
/// <param name="newValue">New value to be saved to mValue.</param>
public void changeValueNoCallback(float newValue)
{
float val = Mathf.Clamp01(newValue);
if (mValue != val)
{
float before = this.value;
mValue = val;
if (before != this.value)
ForceUpdate();
#if UNITY_EDITOR
if (!Application.isPlaying)
NGUITools.SetDirty(this);
#endif
}
}
Misplaced Object Creation Event CallsNext, we found another issue where every time the dialog opened, regardless of us setting the value to what we wanted, it would be automatically changed to whatever we defaulted the value to in the editor. This was tracked down to the OnChange event delegate being called as soon as the dialog was opened. That can be fixed by simply removing those lines of code. This also isn't needed, because the UIProgressBar was just displayed - the value isn't changed by the user, therefore calling the EventDelagate in this case causes far more problems than it helps with. To address that, just change the Start method in UIProgressBar to the following:
protected void Start ()
{
Upgrade();
if (Application.isPlaying)
{
if (mBG != null) mBG.autoResizeBoxCollider = true;
OnStart();
// Comment out or remove the following block of code to prevent the event from running.
/*if (current == null && onChange != null)
{
current = this;
EventDelegate.Execute(onChange);
current = null;
}*/
}
ForceUpdate();
}
Hopefully those help out some people stuck with other similar oddities when working with UIProgressBar and UISlider!