Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Vector Graphics Preview Package

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

  1. LukasH

    LukasH

    Joined:
    May 16, 2014
    Posts:
    12
    Like all my project I like to automate importsettings for sprites depending in which folder they are. This saves me some manual repeating labor. It seems like I cannot hook into the SVG's AssetPostProcessor like I used to do:
    Code (CSharp):
    1.     public class SVGPostProcessor : AssetPostprocessor
    2.     {
    3.         private void OnPreprocessTexture()
    4.         {
    5.             if(assetPath.EndsWith(".svg"))
    6.             {
    7.                 SVGImporter svgImporter = (SVGImporter)assetImporter;
    8.                 svgImporter.SvgPixelsPerUnit = 100f;
    9.                 svgImporter.TargetResolution = 480; //480p in the editor
    10.             }
    11.         }
    12.     }
    Is this unsupported or am I doing it wrong?
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    Since the SVG asset is not a texture, you have to use the OnPostprocessAllAssets method instead:
    https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html
     
  3. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    560
    [EDIT] I figured out what the issue was. I was using Screen.dpi in my stroke size calculations and that apparently doesn't work in WebGL. There should probably be some sort of sanity checking on the HalfThickness value. [EDIT]

    I'm having no luck using runtime generated vector graphics in a WebGL export (works fine in editor and native builds). My generated content isn't being shown at all, but it sounds like this is supposed to work? I'm getting a couple different errors in the javascript console:

    This one (partial) I see even for the working examples others have posted. Based on a quick search it sounds like a known WebKit thing:
    Code (csharp):
    1. WebGL: INVALID_OPERATION: texImage2D: type HALF_FLOAT_OES but ArrayBufferView is not NULL
    But this one (full output) I only see if I do a development build. I appears to be having an issue with the tessellation.
    Code (csharp):
    1. [Error] exception thrown: Error: Out of bounds Trunc operation (evaluating 'Module["asm"]["dynCall_viiiii"].apply(null, arguments)'),<?>.wasm-function[_VectorUtils_TessellatePath_mBAF8BBAEA49F06530A5CEF55CD8BE8E843D94F5E]@[wasm code]
    2. <?>.wasm-function[_VectorUtils_TessellateShape_mC1582E5ADD69D0BE44F2F07E1D70CFF2EE630CA7]@[wasm code]
    3. <?>.wasm-function[dynCall_viiiii]@[wasm code]
    4. wasm-stub@[wasm code]
    5. dynCall_viiiii@[native code]
    6. blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:26353:46
    7. invoke_viiiii@blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:18512:27
    8. wasm-stub@[wasm code]
    9. <?>.wasm-function[_VectorUtils_TessellateNodeHierarchyRecursive_mBCAEBCB5F854E8984DF4E0957D84C4E1CA6DB75D]@[wasm code]
    10. <?>.wasm-function[_VectorUtils_TessellateScene_m77EB7AA09E71D5C0E460E666B94D12CE197CC788]@[wasm code]
    11. <?>.wasm-function[_PolyShape_GenerateGeometry_mF8F7817EBECE60EA4D123D6C16C2491DCF1D73DE]@[wasm code]
    12. <?>.wasm-function[__ZN18VirtActionInvoker06InvokeEjP12Il2CppObject]@[wasm code]
    13. <?>.wasm-function[_VectorShape_GenerateMesh_mBD6965C191D679BCE811983896B6B6A5434E8C60]@[wasm code]
    14. <?>.wasm-function[_VectorShape_get_ShapeMesh_m029B0E16146CEA1632A300197AE46B5B4D64CED1]@[wasm code]
    15. <?>.wasm-function[_PlayfieldPanel_OnRenderObject_mACCA26D0EE1B1E94053F6118508CD5A51B89E4DA]@[wasm code]
    16. <?>.wasm-function[__Z65RuntimeInvoker_TrueVoid_t22962CB4C05B1D89B55A6E1139F0E87A90987017PFvvEPK10MethodInfoPvPS4_]@[wasm code]
    17. <?>.wasm-function[dynCall_iiiii]@[wasm code]
    18. wasm-stub@[wasm code]
    19. dynCall_iiiii@[native code]
    20. blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:25683:45
    21. invoke_iiiii@blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:17440:33
    22. wasm-stub@[wasm code]
    23. <?>.wasm-function[__ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException]@[wasm code]
    24. <?>.wasm-function[_il2cpp_runtime_invoke]@[wasm code]
    25. <?>.wasm-function[__Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb]@[wasm code]
    26. <?>.wasm-function[__ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb]@[wasm code]
    27. <?>.wasm-function[__ZN13MonoBehaviour20InvokeOnRenderObjectEv]@[wasm code]
    28. <?>.wasm-function[__ZN13RenderManager29InvokeOnRenderObjectCallbacksEv]@[wasm code]
    29. <?>.wasm-function[__ZN6Camera18DoRenderPostLayersEPK11CullResultsR17ShaderPassContextNS_10RenderFlagEPFvvE]@[wasm code]
    30. <?>.wasm-function[__ZN6Camera12CustomRenderER11CullResultsR17ShaderPassContextPK21CameraRenderingParamsNS_10RenderFlagEPNS_21PerformRenderFunctionEPFvvE]@[wasm code]
    31. <?>.wasm-function[__ZN13RenderManager13RenderCamerasEiPFvvE]@[wasm code]
    32. <?>.wasm-function[__Z12PlayerRenderb]@[wasm code]
    33. <?>.wasm-function[__ZZ23InitPlayerLoopCallbacksvEN45PostLateUpdateFinishFrameRenderingRegistrator7ForwardEv]@[wasm code]
    34. <?>.wasm-function[__Z17ExecutePlayerLoopP22NativePlayerLoopSystem]@[wasm code]
    35. <?>.wasm-function[__Z17ExecutePlayerLoopP22NativePlayerLoopSystem]@[wasm code]
    36. <?>.wasm-function[__Z10PlayerLoopv]@[wasm code]
    37. <?>.wasm-function[__ZL8MainLoopv]@[wasm code]
    38. <?>.wasm-function[dynCall_v]@[wasm code]
    39. wasm-stub@[wasm code]
    40. dynCall_v@[native code]
    41. blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:26023:41
    42. browserIterationFunc@blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:6826:23
    43. runIter@blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:6928:9
    44. Browser_mainLoop_runner@blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:6864:27
    45.     printErr (UnityLoader.js:1044)
    46.     runIter (blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:6933)
    47.     Browser_mainLoop_runner (blob:http://localhost:63217/830ac37a-b83b-4af9-a3f9-82a2faa24d8c:6864)
     
    Last edited: Jan 6, 2019
    mcoted3d likes this.
  4. LukasH

    LukasH

    Joined:
    May 16, 2014
    Posts:
    12
  5. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    36
    I get a lot of bugs trying to build to UWP for 2018.3.0f2 now:

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(59,22): error CS1061: 'XmlReaderSettings' does not contain a definition for 'ProhibitDtd' and no accessible extension method 'ProhibitDtd' accepting a first argument of type 'XmlReaderSettings' could be found (are you missing a using directive or an assembly reference?)

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(61,22): error CS1061: 'XmlReaderSettings' does not contain a definition for 'ValidationFlags' and no accessible extension method 'ValidationFlags' accepting a first argument of type 'XmlReaderSettings' could be found (are you missing a using directive or an assembly reference?)

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(62,22): error CS1061: 'XmlReaderSettings' does not contain a definition for 'ValidationType' and no accessible extension method 'ValidationType' accepting a first argument of type 'XmlReaderSettings' could be found (are you missing a using directive or an assembly reference?)

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(63,22): error CS1061: 'XmlReaderSettings' does not contain a definition for 'XmlResolver' and no accessible extension method 'XmlResolver' accepting a first argument of type 'XmlReaderSettings' could be found (are you missing a using directive or an assembly reference?)

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(420,53): error CS0117: 'Thread' does not contain a definition for 'Sleep'

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\VectorSprite.cs(113,124): error CS0117: 'Type' does not contain a definition for 'DefaultBinder'

    Library\PackageCache\com.unity.vectorgraphics@1.0.0-preview.22\Runtime\SVGParser.cs(2085,25): error CS1061: 'SVGDocument.ElemHandler' does not contain a definition for 'Method' and no accessible extension method 'Method' accepting a first argument of type 'SVGDocument.ElemHandler' could be found (are you missing a using directive or an assembly reference?)

     
  6. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    Version 1.0.0-preview.23 now supports gradients defined later in the file.
     
    Pawl likes this.
  7. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    It seems that we're using APIs that aren't available in UWP .NET. This will have to be fixed on our side.
     
  8. FeynmanRen100

    FeynmanRen100

    Joined:
    Sep 6, 2013
    Posts:
    20
    @mcoted3d

    I found a SVG Asset bug on Android , when import an svg file into unity, unity auto convert it with an unlit/vectorgradient shader, and build it to the Asset bundles ,
    when load the bundle and spawn the game object, the material says : Material does not have a _MainTex texture property. it is required for SpriteRenderer.

    SVG Package version1.0.0. preview.23,

    Notice: the material is fine when drag the svg object into scene, when load the svg game object from asset bundle on Android , this will occur, and this bug only occur on Android Asset bundle. it's ok with iOS ABs.
    if you need , I can give you the AB.

    Can you help to fix this ?
     

    Attached Files:

  9. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    36
    I really, really, really, really, really, really, really, really, really, really, really, really, really need this functionality, and I need it now.

    What can I do to make it work? Do you have somewhere I can get access to the code to try to fix it myself? Is all of it in the repository?
     
  10. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    I'll contact you privately to give you some options while we resolve this.

    [EDIT]
    @Quakeulf Please note that the .Net scripting backend for UWP is deprecated since Unity 2018.2 (the full removal should happen in 2019.2). Is there any chance you can target the IL2CPP backend instead? This would give you the same .Net APIs as any other platforms. See here:
    https://forum.unity.com/threads/dep...sed-by-the-universal-windows-platform.539685/
     
    Last edited: Jan 8, 2019
  11. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    Depending on which version of Unity you are using, I think there may be some issues with package assets when they are bundled in players or external packages. I think your best bet right now would be to make your own material asset instead of using the default one provided by the SVG importer. Your material can reference the same shader ("Unlit/Vector" or "Unlit/VectorGradient").
     
  12. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    36
    Thank you for this. We are using the HoloLens so we need the .NET-option so we can debug in Visual Studio after building for it, however we can also build for IL2CPP if this works. I can do some tests when I have the time, but unfortunately for now I will have to wait with this until next week.
     
  13. ArtDept3

    ArtDept3

    Joined:
    Jan 9, 2019
    Posts:
    2
    Hello All, I'm very new to unity but I'm having a problem using svg files. I have the latest download (2018.3) but I do not see the Vector Graphics package in the Package Manager (w/ All packages tab). I can drag svgs to Assets, but it won't let me do anything with them. Am I missing something? TIA
    Screen Shot 2019-01-09 at 8.25.22 AM.png
     
  14. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    76
    Did you click Advanced -> Show Preview Packages?
     
  15. ArtDept3

    ArtDept3

    Joined:
    Jan 9, 2019
    Posts:
    2
    Thank You!!! I guess I missed that part.
     
  16. MartinV

    MartinV

    Joined:
    Nov 12, 2013
    Posts:
    3
    As discussed before, I'd like to split a svg file info multiple sprites, but do it in Editor (building these runtime is slow).

    I tried simple code as a proof of concept


    VectorUtils.TessellationOptions tessOptions = new VectorUtils.TessellationOptions()
    {
    StepDistance = 100,
    MaxCordDeviation = float.MaxValue,
    MaxTanAngleDeviation = Mathf.PI / 2.0f,
    SamplingStepSize = 10
    };

    string filename = "Assets/!/live trace test/martin/straw.svg";
    var sceneInfo = SVGParser.ImportSVG(new StreamReader(filename));
    var geom = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
    Sprite sprite = VectorUtils.BuildSprite(geom, 10.0f, VectorUtils.Alignment.SVGOrigin, Vector2.zero, 128, true);


    when I run this (triggered by a button in EditorWindow), VectorUtils.BuildSprite returns


    Not allowed to override geometry on sprite ''
    UnityEngine.Sprite:OverrideGeometry(Sprite, Vector2[], UInt16[])
    Unity.VectorGraphics.VectorUtils:BuildSprite(List`1, Rect, Single, Alignment, Vector2, UInt16, Boolean) (at /Users/mvaligursky/Library/Unity/cache/packages/packages.unity.com/com.unity.vectorgraphics@1.0.0-preview.23/Runtime/VectorSprite.cs:116)
    Unity.VectorGraphics.VectorUtils:BuildSprite(List`1, Single, Alignment, Vector2, UInt16, Boolean) (at /Users/mvaligursky/Library/Unity/cache/packages/packages.unity.com/com.unity.vectorgraphics@1.0.0-preview.23/Runtime/VectorSprite.cs:69)


    Can I generate and save sprites in editor this way?
     
  17. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    This is probably the source of the problem. Sprites only allow overriding the geometry at either runtime or import time. What you could do instead is to do the work in an asset postprocessor, probably by overriding OnPostprocessAllAssets:
    https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html

    Postprocessors are triggered during import-time and will allow you to override the sprite geometry.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class SVGPostProcess : AssetPostprocessor
    8. {
    9.     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    10.     {
    11.         foreach (string str in importedAssets)
    12.         {
    13.             if (Path.GetExtension(str) == ".svg")
    14.             {
    15.                 Debug.Log("Overriding geometry on: " + str);
    16.                 var prefab = AssetDatabase.LoadMainAssetAtPath(str) as GameObject;
    17.                 var sprite = prefab.GetComponent<SpriteRenderer>().sprite;
    18.                 sprite.OverrideGeometry(
    19.                     new Vector2[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1) },
    20.                     new ushort[] { 0, 1, 2 });
    21.             }
    22.         }
    23.     }
    24. }
     
    Last edited: Jan 10, 2019
  18. Seb-1814

    Seb-1814

    Joined:
    Nov 23, 2013
    Posts:
    11
    Hi, tanks for the feature, it seems very promising.
    A problem for me is the generation of the collider. The physic shape doesn't line up at all with the actual geometry. I tried to adjust the tolerance in the sprite editor but can't get a good result.

    How is the shape generated? What I need is for the collision shape to be generated from the svg geometry (in my case it can be exactly the same edges, but I'd except separate tesselation settings for the collider in the svg importer).
    I tried to modify the code of the importer, but couldn't figure out how to get a shape rather than a triangulated mesh out of the tessellation api).

    Any hints? Thanks!
     

    Attached Files:

  19. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    The physics outline is generated by first rendering the SVG asset to a texture, and then using the same "outline detection" algorithm as regular textured sprites. This works ok most of the time, but may not give great results, especially for non-trivial shapes.

    You will probably have to edit the shape manually to get the quality you need. The physics outline editor documentation is valid for SVG sprites:
    https://docs.unity3d.com/2017.4/Documentation/Manual/SpritePhysicsShapeEditor.html

    Otherwise, you can override the physics shape using the Sprite.OverridePhysicsShape method. You could parse the SVG file (SVGParser.ImportSVG) which will return the vector scene with all the shapes. Then you could construct the physics outline from those shapes.
     
  20. Seb-1814

    Seb-1814

    Joined:
    Nov 23, 2013
    Posts:
    11
    That was my approach, but I'm stuck at the "construct the physics outline from those shapes" part. I'm trying to use the api of the package to do this. I'll dig a little deeper and come back to you. Thanks!
     
  21. ahmadreza_hadidi

    ahmadreza_hadidi

    Joined:
    Sep 21, 2018
    Posts:
    1
    At first tanks you for this great package.

    Can vector graphics import each Layer in SVG file (or group) into separate game object in unity.
    I need something like importing FBX files in to unity (Each group into separate game object)
     
  22. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    It is not (yet) possible to split your SVG files when using the SVG importer. This feature will come later, although it's still not clear how it will work (are we going to do a grid-split like the sprites system, or split by SVG group names, etc.).

    That said, it should be possible for you to implement your own SVG splitting system using the vector graphics APIs. Basically, you can use the SVGParser.ImportSVG method to import an SVG file into a vector representation. You can analyze and split the vector scene as you wish, then tessellate + convert them to separate sprites, all using our APIs. The SVGRuntimeLoad.cs sample might help you to get you started if you want to try this:
    https://github.com/Unity-Technologi...b/master/Assets/RuntimeDemo/SVGRuntimeLoad.cs
     
  23. Seb-1814

    Seb-1814

    Joined:
    Nov 23, 2013
    Posts:
    11
    Ok, I got it to work. I added this code in SVGImporter.cs at the end of
    OnImportAsset.
    Code (CSharp):
    1. if (m_BetterGeneratePhysicsShape)
    2. {
    3.     var physicsShapes = VectorUtils.TraceNodeHierarchyShapes(sceneInfo.Scene.Root, tessOptions);
    4.  
    5.     foreach (var vertices in physicsShapes)
    6.     {  
    7.         if (rect == Rect.zero)
    8.         {
    9.             rect = VectorUtils.Bounds(vertices);
    10.             VectorUtils.RealignVerticesInBounds(vertices, rect, flip: true);
    11.         }
    12.         else
    13.         {
    14.             VectorUtils.FlipVerticesInBounds(vertices, rect);
    15.  
    16.             // The provided rect should normally contain the whole geometry, but since VectorUtils.SceneNodeBounds doesn't
    17.             // take the strokes into account, some triangles may appear outside the rect. We clamp the vertices as a workaround for now.
    18.             VectorUtils.ClampVerticesInBounds(vertices, rect);
    19.         }
    20.     }
    21.    
    22.     sprite.OverridePhysicsShape(physicsShapes);
    23. }
    Here is a comparaison of the colliders generated with the build edge detection vs this solution
    Capture d’écran 2019-01-12 à 18.47.03.png

    It would be great if you could support this kind of collider generation in the package.
     
    mcoted3d likes this.
  24. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    This is looking great.

    Question: how will it behave if you have more complex overlapping shapes (think tiger.svg)? I assume that this will generate many overlapping physics shapes, is that correct?
     
  25. Seb-1814

    Seb-1814

    Joined:
    Nov 23, 2013
    Posts:
    11
    I didn't try, but indeed it wouldn't work. One solution might be to fuse all the shapes together using a boolean algorithm.
     
  26. mspetkat

    mspetkat

    Joined:
    Feb 16, 2018
    Posts:
    1
    Hello,

    I'm quite new to Unity. I'm the artist for my team so I'm learning some of the things that will affect the design of our game. This package helps tremendously with my work and how I design our worlds.

    I've recently been learning how to tile and I love but I can not figure out how to use my SVGs with this system.
    Is this not supported or am I possibly not porting them correctly for tiling? I understand that I can not cut up SVGs like a regular sprite sheet but instead load them in individually. I have done this for the paper-doll animations for our characters. Works awesomely!
     
    Last edited: Jan 14, 2019 at 9:30 PM
  27. FeynmanRen100

    FeynmanRen100

    Joined:
    Sep 6, 2013
    Posts:
    20
    @mcoted3d
    when import the svg files into unity, sometimes it will generate the a sprite and atlas , or sometimes it only just a sprite, I found it's related to svg has gradient color or not,
    when the svg has gradient color, it will generate the sprite, the sprite has a size ,it shows the scale,
    my questions :
    1. how can i get the sprite size at runtime ,see the image below.
    2. the artist make the svg image size for example 1024*1024, how can get the scale ratio to make size to fit. for example if the sprite size is 100*100, the scale is 1024/100=10.24, how can i get the sprite size for pure colo svg image which they have no sprite or atlas .

    I have no idea how unity calculate the sprite size, and how to get it at runtime.

    Can you help me out?
     

    Attached Files:

  28. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    As far as I can tell, the tile system was designed to work with textured sprites, so SVG sprites probably won't work properly at this time.

    You may have some luck with SVG sprites that don't have any gradients and/or embedded textures, since they are very simple and will probably tile properly. SVG sprites with gradients, however, will probably confuse the tile system since they have an associated texture which is an atlas of packed gradients (the sprite UVs can't be mapped directly on them).

    Another solution would be to import your SVG assets as "textured sprites" instead of "vector sprites". These assets should work fine with any 2D system, at the cost of the "infinite" resolution of the SVG assets and more heavy in terms of memory.
     
  29. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    Basically, the resulting sprite size depends on three things:
    1. The actual coordinates of the shapes in the SVG files
    2. The "Pixels per Unit" value from the SVG importer
    3. Whether or not the SVG has a "viewBox" property at its root element

    In the simple case without any viewBox, the SVG file is imported and the coordinates are divided by the "Pixels per Unit" value. The bounding box of the resulting shapes will be the size of the sprite.

    When there's a viewBox, things are a bit more complicated. The point of the viewBox is to tell how to automatically scale the SVG content to fit a given "window". That makes sense in a web browser, but not so much in Unity. I think we provide a "virtual window" of size 100x100 for the root element in this situation, so the size of the resulting sprite should be around 1 unit if your "Pixels per Unit" is 100, but could be different depending on how the SVG file is constructed.

    If you can, try to avoid generating a "viewBox" when exporting your SVG files. I think all major vector editing tools has an option to do that (in Illustrator, there's a "responsive" checkbox if my memory serves).
     
  30. FeynmanRen100

    FeynmanRen100

    Joined:
    Sep 6, 2013
    Posts:
    20
    As you mentioned the sprite size also depending on how the svg file is constructed. so the sprite size may change.

    Does unity provide the runtime api to get the sprite size? no matter what the setting is, what all I want is get the imported sprite size, and get the unit, then scale it to my size.
    If there isn't, how can i make sure every time when import the svg file, the sprite size is 100*100( the pixels per unit is 100, preserve viewport is checked.)

    I have a png sprite which size is 1024*1024, the svg sprite should be exactly the same size as the png sprite, and overlay on it.
     
  31. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    168
    I think you're looking for the Sprite.bounds API:
    https://docs.unity3d.com/ScriptReference/Sprite-bounds.html

    This will give you the position and size (in world units) of the sprite.
     
  32. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    83
    Would you mind sharing how you were able to accomplish this? I'm trying to do the same thing, but am a bit lost. Thanks!