Search Unity

  1. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice

Unity Vector Graphics Preview Package

Discussion in '2D Experimental Preview' started by rustum, May 4, 2018.

  1. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    There is no easy way to do this. The resulting asset won't necessarily have the same coordinates as the SVG file since some conversion may be done to fit in Unity's coordinate system.

    One way to do it is to try to test if the point is in one of the triangulated shapes. It was discussed in the past: https://forum.unity.com/threads/vector-graphics-preview-package.529845/page-13#post-4597357
     
    KTS95SL and PlanarBit like this.
  2. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    61
    What's the current state of the package? It's around 3 years and it's still in preview. Seems like development on this is a little bit dead?
    Is any sort of animation supported yet? Can I create something like this health bar with the current state of the package?
    https://codepen.io/justinryder/pen/OwzBVG
     
    PlanarBit and JonPQ like this.
  3. KTS95SL

    KTS95SL

    Joined:
    Apr 30, 2020
    Posts:
    7
    Is that worked for you?
     
  4. KTS95SL

    KTS95SL

    Joined:
    Apr 30, 2020
    Posts:
    7
    I am using shapes like flower petals. Is that possible with shapes like flower petals as they don't have indices?
     
  5. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    95
    @mcoted3d I've noticed that TessellateScene() and VectorUtils.BuildSprite() take quite a while to build a sprite... sometimes over half a second for a largish .svg. even with a pre-filtered scene file to only generate sub nodes separately it still takes long enough for a noticeable pause or frame-rate hitch ... do you know if these functions can be threaded / run in the background please?
     
  6. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    95
    the shapes are all curves in svg, but when unity imports them, it converts them to triangle meshes, based on the import quality settings, it builds more or less dense triangle meshes.
     
    KTS95SL likes this.
  7. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    The tessellation methods are all static with no side effects, so it's safe to call them from a different thread. (Ideally we would use the job system for the tessellation, that's planned for the future).

    Once the scene is tessellated, you'll have to create the Sprite on the main thread though.
     
    JonPQ likes this.
  8. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    Development on this package has slowed as we are trying to consolidate all the vector graphics needs across Unity (especially regarding the UI Toolkit framework). We will be able to get out of preview once we are done with this refactoring. I can't provide an ETA unfortunately.

    We don't support CSS transitions or Javascript-style animations. But, the vector graphics API allows you to generate shapes dynamically by C# code, which would allow you to make something like the example you provided.
     
    stychu likes this.
  9. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    95
    Thank you !
     
  10. kulnor

    kulnor

    Joined:
    May 23, 2016
    Posts:
    7
    Used the Vector Graphics 2.0.0-preview.14 in a project a little while ago and noticed it is no longer available under preview. Was planning to publish assets leveraging it, but may not be a safe thing to do. What is the current and future state of the project? This is a great package with tremendous potential!
     
  11. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    It is available, but the Package Manager window doesn't show preview packages by default anymore. You can change that in the Project Settings. See this thread for details:
    https://forum.unity.com/threads/visibility-changes-for-preview-packages-in-2020-1.910880/

    Even though the package has been stable for a while, we are not yet ready to remove the preview tag. Some changes to the package might be necessary to improve the compatibility of the vector graphics package with the other tools provided by Unity (in particular, UI Toolkit has heavy requirements regarding vector graphics).
     
    Skjalg and PlanarBit like this.
  12. kulnor

    kulnor

    Joined:
    May 23, 2016
    Posts:
    7
    Thanks for the feedback. So looks I can generally assume the project will continue to me further developer and maintained for the foreseeable future? This is a very useful package, will do what I can to contribute.
     
  13. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    Yes, our plan is to bring the package to verified status later.
     
    Noisecrime and JonPQ like this.
  14. xqtr123

    xqtr123

    Joined:
    Jun 7, 2014
    Posts:
    18
    Hi!
    I want to be able to change target resolution based on which device is used but can't find an example. How do you change the target resolution in runtime?
     
  15. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    95
    You can load the SVG file via script... then have your own quality settings exposed in inspector variables, or in a scriptable object.
    the main trick was to rename the .svg file, as .bytes instead. Then you can load it as a text/data file

    define a TextAsset variable, assign your .bytes file to that,
    then load it using something like this...

    Code (CSharp):
    1.     private SVGParser.SceneInfo loadSVG()
    2.     {
    3.         using (var reader = new StringReader(TextAssetToLoad.text))
    4.         {
    5.             return SVGParser.ImportSVG(reader);
    6.         }
    7.     }
    That gives you a sceneInfo object...

    You can use VectorUtils to setup your custom settings....
    var geometry = VectorUtils.TessellationOptions tess = new VectorUtils.TessellationOptions();

    then Tesselate the scene and build your sprite or texture...
    VectorUtils.TessellateScene(scene, tess);

    var sprite = VectorUtils.BuildSprite(geometry, .......)
     
  16. SimplyVideoJordan

    SimplyVideoJordan

    Joined:
    Mar 25, 2021
    Posts:
    1
    Thought I would share some small, probably unconventional feedback.

    Firstly, thanks for making this amazing package. Works straight out the box like magic.

    Literally my only problem with it (unfortunately) is that because it's a preview package, depending on it in any production-ready asset/plugin (such as the one my company is about to release) means transitively our asset is a preview "package," which gives the extended impression what we're delivering is something that is in alpha/beta and/or could potentially have major faults you would associate with said beta product. And all we're doing is using this one package so we can use svg icons, a super small, basic use case. As a result, we've basically had to batch convert all svgs to pngs and re-import icons across the project before we could release it.

    So that's just a very small feedback. Super appreciative of the work over the years though, brilliant stuff. But I thought I would just mention just in case it's one of the types of user outcomes at the moment you're not aware of. Just because of the preview packages in use warning, it will potentially limit the amount of users you have testing and feeding back on this (despite just how stable this package appears to be in certain use cases).

    Cheers,
     
    Last edited: Mar 25, 2021
  17. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    We completely understand. Hopefully the package will be out of preview soon, but we have some major refactor to do first, most of them related to antialiasing and UI Toolkit integration. Once this is done and we've made a few iterations with our users, we will be in good shape to get out of preview. I can't provide and ETA unfortunately.

    Thanks for the feedback!
     
    SimplyVideoJordan likes this.
  18. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    Where do I post a bug for this package? It has a hidden dependency on "com.unity.modules.unitywebrequestwww". It trips up if you have that modules disabled in your project.
     
  19. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    Hello, you can file a regular bug (Help > Report a Bug...), thank you!
     
    Schodemeiss likes this.
  20. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    No problem, have done exactly that. I don't think it's an urgent issue, but it's nice to make sure our dependency tree's are accurate - especially in what I imagine will be quite a popular package.
     
    mcoted3d likes this.
  21. JiriAtanasovsky

    JiriAtanasovsky

    Joined:
    Jan 18, 2021
    Posts:
    7
    Hello,

    first thank you for this excellent package.

    Is it possible to use imported svg as sprite with Sprite Shape Profile? When I tried, I got error...
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.U2D.SpriteShapeGenerator.TransferSprites (Unity.Collections.NativeArray`1[UnityEngine.U2D.SpriteShapeGenerator+JobSpriteInfo]& spriteInfos, UnityEngine.Sprite[] sprites, System.Int32 maxCount) (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeGenerator.cs:580)
    3. UnityEngine.U2D.SpriteShapeGenerator.PrepareSprites (UnityEngine.Sprite[] edgeSprites, UnityEngine.Sprite[] cornerSprites) (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeGenerator.cs:599)
    4. UnityEngine.U2D.SpriteShapeGenerator.Prepare (UnityEngine.U2D.SpriteShapeController controller, UnityEngine.U2D.SpriteShapeParameters shapeParams, System.Int32 maxArrayCount, Unity.Collections.NativeArray`1[T] shapePoints, Unity.Collections.NativeArray`1[T] metaData, UnityEngine.U2D.AngleRangeInfo[] angleRanges, UnityEngine.Sprite[] segmentSprites, UnityEngine.Sprite[] cornerSprites, System.Boolean UseUTess) (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeGenerator.cs:2484)
    5. UnityEngine.U2D.SpriteShapeController.ScheduleBake () (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeController.cs:709)
    6. UnityEngine.U2D.SpriteShapeController.BakeMesh () (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeController.cs:447)
    7. UnityEngine.U2D.SpriteShapeController.OnWillRenderObject () (at Library/PackageCache/com.unity.2d.spriteshape@5.1.1/Runtime/SpriteShapeController.cs:416)
    8. UnityEngine.Rendering.RenderPipelineManager:DoRenderLoop_Internal(RenderPipelineAsset, IntPtr, List`1, AtomicSafetyHandle)
     
  22. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    272
    Hi!

    Quick question:

    I have a diamond sprite that I need to scale to say 50 to cover the screen. Using a texture it looks blurry on the edges.

    Screen Shot 2021-04-05 at 5.01.16 PM.png

    I decided to try and fix this with an svg. However, I am sure I am doing something wrong. It looks even worst.

    Screen Shot 2021-04-05 at 5.01.36 PM.png

    The only thing that makes it look better is making the texture size to around 2000.

    Screen Shot 2021-04-05 at 5.01.30 PM.png

    Screen Shot 2021-04-05 at 5.01.26 PM.png

    Is setting the texture size to 2000 the way to go? I feel like this would be bad for performance? Is there a setting I am missing so that no matter the scale, the svg will look good?

    Or am I completely off base and that isn't how svgs work?

    Thanks!
     
  23. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    I'm pretty sure SpriteShapes requires texture sprites to work with. I don't think SVG assets imported as "Vector Sprite" will work, but you may have some luck if you import them as "Textured Sprites".
     
  24. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    From your import settings, I see that you imported your SVG as a "Textured Sprite", this essentially converts to the SVG to a texture, which won't solve your issue.

    Try to import your SVG as a "Vector Sprite" instead, which will give you a sprite that can scale indefinitely. If you have aliasing issues, you'll have to turn on MSAA.
     
  25. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    272
    That make sense, thanks!
     
  26. xAvatarchikx

    xAvatarchikx

    Joined:
    Aug 17, 2012
    Posts:
    62
    How can I load svg into il2cpp runtime?
    when called, it throws an error VectorUtils.BuildSprite due to reflection


    Code (CSharp):
    1.    // The Sprite.Create(Rect, Vector2, float, Texture2D) method is internal. Using reflection
    2.             // until it becomes public.
    3.             var spriteCreateMethod = typeof(Sprite).GetMethod("Create", BindingFlags.Static | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] { typeof(Rect), typeof(Vector2), typeof(float), typeof(Texture2D) }, null);
    4.             var sprite = spriteCreateMethod.Invoke(null, new object[] { rect, pivot, svgPixelsPerUnit, texture }) as Sprite;
     
  27. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    We'll have to move this method to another assembly that's whitelisted for internal access. Can you please file a bug (Help > Report a Bug...), we'll try to get that sorted promptly.
     
  28. StephenW

    StephenW

    Joined:
    Mar 29, 2016
    Posts:
    62
    I haven't had time to read all these pages yet, however, I loaded up unity 2020.3 and I can not long see the Vector Graphics preview package, even after I tick the box to display previews.
    The manual also shows it is not anywhere to be found in Unity 2020
    https://docs.unity3d.com/2020.1/Documentation/Manual/pack-preview.html

    I did have a 2019 project which I updated to 2020 and it took the package with it, but it's nowhere to be found if I start a new 2020 project.

    Any ideas?

    Thanks
     
  29. XxTorionxX

    XxTorionxX

    Joined:
    Feb 6, 2020
    Posts:
    1
    This Package Isn't Supported In Unity 2020.1.x And Above! Will We See Support In The Future (Because I Have A Game That Uses SVG File's & Once 2019.4 LTS End's, I'm Probably Gonna Have To Convert Them All To PNG's And It Won't Look Good!)
     
  30. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    651
    There were changes in the visibility of packages. This package won't show in the Package Manager Window because it is considered "experimental". It is still supported, and you can install it by clicking the "+" sign and manually adding a git package with the name "com.unity.vectorgraphics".

    The package will be visible in the Package Manager Window once we commit to a pre-release version.

    https://forum.unity.com/threads/new-package-lifecycle-taking-effect-in-2021-1.1045201/
     
    StephenW likes this.
  31. StephenW

    StephenW

    Joined:
    Mar 29, 2016
    Posts:
    62
    Thanks very much for the info, it worked great.
     
unityunity