- Home /
Question by
prgramernoob101 · May 22 at 02:05 AM ·
c#2duiuitoolkit
UI Toolkit: Is it possible to modify USS custom properties (variables) with C#?
Hello, I'm trying to create a crosshair with the new UI toolkit package but I cannot find a way to modify my crosshair variable var(--gap)
from within C#.
I can read the value just fine (prints out 550%), but when I try to change it in game nothing happens. Confirmed by the UI debugger window.
Crosshair.cs
private VisualElement _root;
private CustomStyleProperty<string> _gap = new CustomStyleProperty<string>("--gap");
private void Start()
{
var uiDocument = GameObject.Find("UIDocument");
_root = uiDocument.GetComponent<UIDocument>().rootVisualElement;
_root.RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
}
private void OnCustomStyleResolved(CustomStyleResolvedEvent e)
{
if (!e.customStyle.TryGetValue(_gap, out var gap)) return;
gap = "0%";
}
...
Crosshair.uss
.crosshair {
background-color: rgb(0, 255, 17);
position: absolute;
--length: 400%;
--thickness: 40%;
--minThickness: 1px;
--minLength: 10px;
--gap: 550%;
}
...
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# UI list item drag onto 2d sprite in world space? 1 Answer
Help check click out of UI 2 Answers