Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created May 31, 2024 10:09
Show Gist options
  • Save karljj1/9d0ca214192ae770ef3693bd3ad50955 to your computer and use it in GitHub Desktop.
Save karljj1/9d0ca214192ae770ef3693bd3ad50955 to your computer and use it in GitHub Desktop.
Example of drawing an overlay over another UI Element.
using UnityEngine;
using UnityEngine.UIElements;
public class Example : MonoBehaviour
{
public UIDocument document;
void Start()
{
var document = GetComponent<UIDocument>();
var progressBar = document.rootVisualElement.Q<ProgressBar>();
var progressBarOverlay = new VisualElement { style = { flexDirection = FlexDirection.Row } };
document.rootVisualElement.Add(progressBarOverlay);
progressBar.RegisterCallback<GeometryChangedEvent>(evt =>
{
progressBarOverlay.style.position = Position.Absolute;
progressBarOverlay.style.top = evt.newRect.y;
progressBarOverlay.style.left = evt.newRect.x;
progressBarOverlay.style.width = evt.newRect.width;
progressBarOverlay.style.height = evt.newRect.height;
progressBarOverlay.BringToFront();
});
// Add some markers
for (int i = 0; i < 10; i++)
{
var marker = new VisualElement
{
style =
{
backgroundColor = new StyleColor(Color.green),
width = 15,
height = Length.Percent(100),
left = Length.Percent(10 * i),
}
};
progressBarOverlay.Add(marker);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment