Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'UI Toolkit' started by YinZuoyu, Oct 14, 2020.

  1. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    I load a svg as a TextAsset,and I try to get the sprite by this code:
    Code (CSharp):
    1.  public static Sprite GetSprite(string txt)
    2.         {
    3.             if (txt == null) return null;
    4.             var strReader = new StringReader(txt);
    5.             var sceneInfo = SVGParser.ImportSVG(strReader);
    6.             TessellationOptions options = new TessellationOptions();
    7.             options.StepDistance = 1f;
    8.             options.MaxCordDeviation = 1f;
    9.             options.MaxTanAngleDeviation = 1f;
    10.             options.SamplingStepSize = 1f;
    11.             var geoms = TessellateScene(sceneInfo.Scene, options);
    12.  
    13.             var sprite = BuildSprite(geoms, pixelsPerUnit, Alignment.Center, Vector2.zero, gradientResolution, true);
    14.             return sprite;
    15.         }
    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.
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    969
    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.
     
  3. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    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.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    969
    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.

    Code (CSharp):
    1. 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.
     
  5. YinZuoyu

    YinZuoyu

    Joined:
    Aug 25, 2015
    Posts:
    44
    Thanks.We'll consider the temporary workaround.
     
  6. The-Wand3rer

    The-Wand3rer

    Joined:
    May 14, 2019
    Posts:
    31
    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?
     
  7. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    969
    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.