Hi, I’m trying to change the color of the progress bar using the built-in “progress bar” control in UI Toolkit. I’m attaching a screenshot of what it looks like in UI Builder.
The progress bar itself (called “ChargeMeter” above) only lets you edit the color of text and borders, not the color of the bar itself. All of the children of ChargeMeter are greyed out and I can’t edit them at all in UI Builder.
I created a UI Document in my scene, attached this uxml as the source, and added a script to control the progress of the bar (this part works fine), but I don’t know how to properly change the color in my script.
I was looking at this post: How to change progress bar color? but I couldn’t quite follow what they were saying, I’m still new to Unity and C#. Changing the color of this seems like it should be trivial, but I’m stuck. Any help would be greatly appreciated!
Unfortunately UI Toolkit isn’t very newbie friendly. It has a reasonably high floor, but a super high ceiling with what you can do with it.
You might want to start with Unity’s uGUI/TMPro system instead, as they work within the game object/component architecture that you’ll be learning as you start your journey with Unity anyway.
I could try to answer your question, but… it probably also won’t make a lot of sense without a lot of underlying C#/Unity knowledge. Might be worth reading the docs on the USS selectors, along with how to find and manipulate visual elements via code. But it all assumes an intermediate or above knowledge level.
Ultimately this visual element is the one that is the bar itself:
You should be able to query it with the method suggested here and from there alter it’s background color style property.
Ahhh, thanks so much for the help, I was able to figure it out! I suspected that the Visual Element you underlined was the one I needed to edit, but I had no idea what its class name was or how to reference it in a query. Turns out that in the UI Builder, if you click the 3 dots beside “Hierarchy”, you can display the list of class names. See here:
So now that I see that the relevant class name is “unity-progress-bar__progress”, the other forum post I referenced makes more sense. Here’s the code in the C# script I attached to the UI Document in my scene that turns the progress bar green:
using UnityEngine;
using UnityEngine.UIElements;
public class ChargeMeter : MonoBehaviour
{
// The game object I'm getting info about charge % from
public SpearController spear;
private VisualElement root;
private void Start()
{
root = GetComponent<UIDocument>().rootVisualElement;
//Sets the progress bar color to be green
var actualBar = root.Q(className: "unity-progress-bar__progress");
actualBar.style.backgroundColor = Color.green;
}
private void Update()
{
//Updates the progress bar value to be equal to the charge %
ProgressBar chargeBar = root.Q<ProgressBar>("ChargeMeter");
chargeBar.value = spear.spearCharge;
}
}
Actually, instead of modifying the color in C#, you can create an USS file attached to your view which contains the classname of what you want to modify. In your case, it should be .unity-progress-bar__progress. It will look like that in USS :