Exemple #1
0
    private Texture2D DrawSVG(string svgXml,
                              uint texWidth, uint texHeight,
                              Color clearColor,
                              bool fastUpload)
    {
        SVGDocument document;
        SVGSurface  surface;
        Texture2D   texture = null;

        // create a drawing surface, with the same texture dimensions
        surface = SVGAssets.CreateSurface(texWidth, texHeight);
        if (surface == null)
        {
            return(null);
        }
        // create the SVG document
        document = SVGAssets.CreateDocument(svgXml);
        if (document == null)
        {
            surface.Dispose();
            return(null);
        }
        // draw the SVG document onto the surface
        if (surface.Draw(document, new SVGColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a), SVGRenderingQuality.Better))
        {
            // create a 2D texture compatible with the drawing surface
            texture = surface.CreateCompatibleTexture(true, false);
            if (texture != null)
            {
                texture.hideFlags = HideFlags.DontSave;
                // copy the surface content into the texture
                if (fastUpload && Application.isPlaying)
                {
                    surface.CopyAndDestroy(texture);
                }
                else
                {
                    if (surface.Copy(texture))
                    {
                        // call Apply() so it's actually uploaded to the GPU
                        texture.Apply(false, true);
                    }
                }
            }
        }

        // destroy SVG surface and document
        surface.Dispose();
        document.Dispose();
        // return the created texture
        return(texture);
    }
Exemple #2
0
    private Texture2D DrawAtlas(string svgXml1, string svgXml2, string svgXml3, string svgXml4,
                                uint texWidth, uint texHeight,
                                Color clearColor,
                                bool fastUpload)
    {
        SVGDocument document1;
        SVGDocument document2;
        SVGDocument document3;
        SVGDocument document4;
        SVGSurface  surface;
        Texture2D   texture = null;

        // create a drawing surface, with the same texture dimensions
        surface = SVGAssets.CreateSurface(texWidth, texHeight);
        if (surface == null)
        {
            return(null);
        }
        // create the SVG document
        document1 = SVGAssets.CreateDocument(svgXml1);
        if (document1 == null)
        {
            surface.Dispose();
            return(null);
        }
        document2 = SVGAssets.CreateDocument(svgXml2);
        if (document2 == null)
        {
            surface.Dispose();
            document1.Dispose();
            return(null);
        }
        document3 = SVGAssets.CreateDocument(svgXml3);
        if (document3 == null)
        {
            surface.Dispose();
            document1.Dispose();
            document2.Dispose();
            return(null);
        }
        document4 = SVGAssets.CreateDocument(svgXml4);
        if (document4 == null)
        {
            surface.Dispose();
            document1.Dispose();
            document2.Dispose();
            document3.Dispose();
            return(null);
        }

        // draw the SVG document1 onto the surface
        surface.Viewport = new SVGViewport(0.0f, 0.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document1, new SVGColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a), SVGRenderingQuality.Better);
        // draw the SVG document2 onto the surface
        surface.Viewport = new SVGViewport(texWidth / 2.0f, 0.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document2, null, SVGRenderingQuality.Better);
        // draw the SVG document3 onto the surface
        surface.Viewport = new SVGViewport(0.0f, texHeight / 2.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document3, null, SVGRenderingQuality.Better);
        // draw the SVG document4 onto the surface
        surface.Viewport = new SVGViewport(texWidth / 2.0f, texHeight / 2.0f, texWidth / 2.0f, texHeight / 2.0f);
        surface.Draw(document4, null, SVGRenderingQuality.Better);

        // create a 2D texture compatible with the drawing surface
        texture = surface.CreateCompatibleTexture(true, false);
        if (texture != null)
        {
            texture.hideFlags = HideFlags.DontSave;
            // copy the surface content into the texture
            if (fastUpload && Application.isPlaying)
            {
                surface.CopyAndDestroy(texture);
            }
            else
            {
                if (surface.Copy(texture))
                {
                    // call Apply() so it's actually uploaded to the GPU
                    texture.Apply(false, true);
                }
            }
        }
        // destroy SVG surface and document
        surface.Dispose();
        document1.Dispose();
        document2.Dispose();
        document3.Dispose();
        document4.Dispose();
        // return the created texture
        return(texture);
    }
    private static SVGPackedBin[] GenerateBins(// input
        List <SVGAssetInput> svgList,
        int maxTexturesDimension,
        int border,
        bool pow2Textures,
        float scale,
        // output
        Dictionary <int, PackedSvgAssetDocLink> processedAssets, Dictionary <uint, PackedSvgDocRef> loadedDocuments)
    {
        SVGPacker packer = SVGAssets.CreatePacker(scale, (uint)maxTexturesDimension, (uint)border, pow2Textures);

        // start the packing process
        if (packer.Begin())
        {
            foreach (SVGAssetInput svgAsset in svgList)
            {
                int assetKey = svgAsset.TxtAsset.GetInstanceID();
                // if the text asset has not been already processed, lets create an SVG document out of it
                if (!processedAssets.ContainsKey(assetKey))
                {
                    // create the SVG document
                    SVGDocument svgDoc = SVGAssets.CreateDocument(svgAsset.TxtAsset.text);
                    if (svgDoc != null)
                    {
                        PackedSvgDocRef svgDocRef = new PackedSvgDocRef(svgDoc, svgAsset.TxtAsset);
                        // add the document to the packer, and get back the actual number of packed bounding boxes
                        uint[] info = packer.Add(svgDoc, svgAsset.SeparateGroups, svgAsset.Scale);
                        // info[0] = number of collected bounding boxes
                        // info[1] = the actual number of packed bounding boxes
                        if (info != null && info[1] == info[0])
                        {
                            // increment references
                            svgDocRef.Inc(info[1]);
                            // keep track of the processed asset / created document
                            processedAssets.Add(assetKey, new PackedSvgAssetDocLink(svgAsset, svgDoc));
                            loadedDocuments.Add(svgDoc.Handle, svgDocRef);
                        }
                        else
                        {
                        #if UNITY_EDITOR
                            if (info[1] < info[0])
                            {
                                EditorUtility.DisplayDialog("Some SVG elements cannot be packed!",
                                                            "Specified maximum texture dimensions do not allow to pack all SVG elements, please increase the value",
                                                            "Ok");
                            }
                        #else
                            UnityEngine.Debug.Log("Some SVG elements cannot be packed! Specified maximum texture dimensions do not allow to pack all SVG elements, please increase the value");
                        #endif
                            // close the packing process without doing anything
                            packer.End(false);
                            // free memory allocated by all loaded SVG documents
                            foreach (PackedSvgAssetDocLink docLink in processedAssets.Values)
                            {
                                docLink.Document.Dispose();
                            }
                            // return the error
                            return(null);
                        }
                    }
                }
                else
                {
                    PackedSvgAssetDocLink existingAsset;
                    // get the (already processed svg) asset
                    if (processedAssets.TryGetValue(assetKey, out existingAsset))
                    {
                        PackedSvgDocRef svgDocRef;
                        // get the (already created) svg document
                        if (loadedDocuments.TryGetValue(existingAsset.Document.Handle, out svgDocRef))
                        {
                            // add the document to the packer, and get back the actual number of packed bounding boxes
                            uint[] info = packer.Add(svgDocRef.Document, svgAsset.SeparateGroups, svgAsset.Scale);
                            // info[0] = number of collected bounding boxes
                            // info[1] = the actual number of packed bounding boxes
                            if (info != null && info[1] == info[0])
                            {
                                // increment references
                                svgDocRef.Inc(info[1]);
                            }
                            else
                            {
                            #if UNITY_EDITOR
                                if (info[1] < info[0])
                                {
                                    EditorUtility.DisplayDialog("Some SVG elements cannot be packed!",
                                                                "Specified maximum texture dimensions do not allow to pack all SVG elements, please increase the value",
                                                                "Ok");
                                }
                            #else
                                UnityEngine.Debug.Log("Some SVG elements cannot be packed! Specified maximum texture dimensions do not allow to pack all SVG elements, please increase the value");
                            #endif
                                // close the packing process without doing anything
                                packer.End(false);
                                // free memory allocated by all loaded SVG documents
                                foreach (PackedSvgAssetDocLink docLink in processedAssets.Values)
                                {
                                    docLink.Document.Dispose();
                                }
                                // return the error
                                return(null);
                            }
                        }
                    }
                }
            }
            // return generated bins
            return(packer.End(true));
        }
        else
        {
            return(null);
        }
    }
 private void LoadSVG()
 {
     // create and load SVG document
     this.m_SvgDoc = (this.SVGFile != null) ? SVGAssets.CreateDocument(this.SVGFile.text) : null;
 }