Created
May 31, 2024 10:09
-
-
Save karljj1/9d0ca214192ae770ef3693bd3ad50955 to your computer and use it in GitHub Desktop.
Example of drawing an overlay over another UI Element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
More info here https://forum.unity.com/threads/creating-a-markable-progress-bar-timeline-in-unity-inspector-and-editor.1596972/#post-9865482