The sprite which create by svg can't set to VisiualElement.

I load a svg as a TextAsset,and I try to get the sprite by this code:

 public static Sprite GetSprite(string txt)
        {
            if (txt == null) return null;
            var strReader = new StringReader(txt);
            var sceneInfo = SVGParser.ImportSVG(strReader);
            TessellationOptions options = new TessellationOptions();
            options.StepDistance = 1f;
            options.MaxCordDeviation = 1f;
            options.MaxTanAngleDeviation = 1f;
            options.SamplingStepSize = 1f;
            var geoms = TessellateScene(sceneInfo.Scene, options);

            var sprite = BuildSprite(geoms, pixelsPerUnit, Alignment.Center, Vector2.zero, gradientResolution, true);
            return sprite;
        }

Then I set the sprite to a VisialElement.But I failed.
I find that the sprite.texture is null,that the UIR report NullReferenceException error.

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.

I need to load the svg from seriver,but I don’t know how to set the svg as a UI Toolkit VectorImage when loaded.

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.

Thanks.We’ll consider the temporary workaround.

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.

Let us know if you need more information.