USS Variable Multiplication Support or Recommended Workaround

Dear UI Toolkit Team,

First of all, great work on UI Toolkit, especially looking forward to the upcoming 6.7 features such as gradients.

I wanted to ask whether there are any plans to support basic multiplication with USS variables, or if there is a recommended workaround.

A common use case is localization. Different language scripts often require font sizes to be scaled relative to a base token:

--base-font-size: 16px;
--language-scale: 1.2;

/* desired */
font-size: calc(var(--base-font-size) * var(--language-scale));

Even a limited form of variable multiplication would greatly simplify localization workflows. If there’s already a cleaner UI Toolkit solution, I’d love to hear about it

Thanks

calc is something we currently have in prototype, but it still needs further testing to determine whether our implementation is suitable. Its very complex to implement.

In the meantime, I’d recommend trying the binding system. A custom binding may also work well for this use case. We use a similar approach for localization, as described here:

Thank you for the quick reply.

I’m trying to avoid binding because all of my UI is generated at runtime and relies heavily on pooling, so adding bindings throughout would be difficult to maintain.

Is there another approach you would recommend in 6.6 or 6.7? For example, if stylesheets can be updated at runtime, I could handle this programmatically instead of manually updating individual values.

Yes, having a different style sheet for your variables will also work. You can apply them via code
Unity - Manual: Apply styles in C# scripts

Having multiple style sheets would be one way to do this. You can break the variant parts into separate style sheets, it doesnt need to all be in 1 if only part will change.

Sorry, what I meant was the ability to change the value of a style defined in a stylesheet at runtime, so that any element using that style would automatically receive the updated value.

For example, changing a custom USS variable or style token at runtime and having all affected elements update accordingly. I thought I had read somewhere that something similar was coming soon like creating or editing stylesheets at runtime.

No this is not supported at the moment.
I think your options are:

  • Loading custom style sheets via code
  • Bindings. Either custom or binding against a variant version of the data.