UI Toolkit only supports textured sprites at this time. If you want to use SVG with UI Toolkit, you should import the SVG as a UI Toolkit VectorImage instead. You can then assign the VectorImage to the VisualElement’s background.
The API for building VectorImage from a VectorScene isn’t exposed yet. I’ll have to see if we can make it public.
In the meantime, a way out for you would be to add an additional step to convert your SVG sprite to a Texture2D, which you can assign to the VisualElement.
var tex = VectorUtils.RenderSpriteToTexture2D(sprite, width, height, material, samplesCount);
The shader used with the material for the above call should be either “Unlit/Vector” or “Unlit/VectorGradient”.
Of course, this is a temporary workaround, as the texture generation can be expensive, and aren’t infinite resolution compared to VectorImages.
I have tried to use this approach. It imports SVG fine, but the result is very “pixellated” (and not antialiased but MSAA is enabled in the pipeline settings as well as in the camera’s). By changing the import settings I can only manage to make it uglier. If set the resolution to a very high one it doesn’t improve. Is this its current state or am I doing something wrong?
When calling RenderSpriteToTexture2D, set the sampleCount to 4 or 8. This will make the generated texture antialiased. Also try to provide a texture size that matches the size it will appear on screen, as this method won’t generate mipmaps for you.