コード例 #1
0
 public static void Clear()
 {
     if (atlasData != null)
     {
         atlasData.Clear();
         atlasData = null;
     }
     SVGParser.Clear();
     SVGGraphics.Clear();
 }
コード例 #2
0
        public void StartProcess(SVGAsset asset)
        {
            if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            if (errors == null)
            {
                errors = new List <SVGError>();
            }
            else
            {
                errors.Clear();
            }
            _importingSVG = true;

            UnityEditor.SerializedObject   svgAsset      = new UnityEditor.SerializedObject(asset);
            UnityEditor.SerializedProperty sharedMesh    = svgAsset.FindProperty("_sharedMesh");
            UnityEditor.SerializedProperty sharedShaders = svgAsset.FindProperty("_sharedShaders");

            // clean up
            SVGParser.Clear();
            SVGParser.Init();
            SVGGraphics.Clear();
            SVGGraphics.Init();

            SVGElement _rootSVGElement = null;

#if IGNORE_EXCEPTIONS
            try {
#else
            Debug.LogWarning("Exceptions are turned on!");
#endif
                // Create new Asset
                CreateEmptySVGDocument();
                _rootSVGElement = this._svgDocument.rootElement;
#if IGNORE_EXCEPTIONS
            } catch (System.Exception exception) {
                _rootSVGElement = null;
                errors.Add(SVGError.Syntax);
                Debug.LogError("SVG Document Exception: " + exception.Message, asset);
            }
#endif

                if (_rootSVGElement == null)
                {
                    Debug.LogError("SVG Document is corrupted! " + UnityEditor.AssetDatabase.GetAssetPath(asset), asset);
                    return;
                }

                SVGGraphics.depthTree = new SVGDepthTree(_rootSVGElement.paintable.viewport);

#if IGNORE_EXCEPTIONS
                try {
#endif
                _rootSVGElement.Render();

                // Handle gradients
                bool hasGradients = (useGradients == SVGUseGradients.Always);
                SVGAtlas atlas    = SVGAtlas.Instance;
                if (useGradients != SVGUseGradients.Never)
                {
                    atlas.hideFlags = HideFlags.DontSave;
                    if (SVGAtlas.Instance.gradients != null && atlas.gradients.Count > 1)
                    {
                        int gradientCount  = atlas.gradients.Count;
                        int gradientWidth  = atlas.gradientWidth;
                        int gradientHeight = atlas.gradientHeight;
                        atlas.atlasTextureWidth  = gradientWidth * 2;
                        atlas.atlasTextureHeight = Mathf.CeilToInt((gradientCount * gradientWidth) / atlas.atlasTextureWidth) * gradientHeight + gradientHeight;
                        atlas.RebuildAtlas();

                        hasGradients = true;
                    }
                }

                // Create actual Mesh
                Material[] outputMaterials;
                Mesh mesh = SVGMesh.CreateMesh(out outputMaterials);
                if (mesh == null)
                {
                    return;
                }

                // Delete gradients if needed
                if (!hasGradients)
                {
                    mesh.uv  = new Vector2[0];
                    mesh.uv2 = new Vector2[0];
                }

                Vector3[] vertices = mesh.vertices;
                Vector2 offset;
                Bounds bounds = mesh.bounds;
                Rect viewport = _rootSVGElement.paintable.viewport;
                viewport.x    *= SVGMesh.meshScale;
                viewport.y    *= SVGMesh.meshScale;
                viewport.size *= SVGMesh.meshScale;

                if (asset.ignoreSVGCanvas)
                {
                    offset = new Vector2(bounds.min.x + bounds.size.x * asset.pivotPoint.x,
                                         bounds.min.y + bounds.size.y * asset.pivotPoint.y);
                }
                else
                {
                    offset = new Vector2(viewport.min.x + viewport.size.x * asset.pivotPoint.x,
                                         viewport.min.y + viewport.size.y * asset.pivotPoint.y);
                }

                // Apply pivot point and Flip Y Axis
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i].x = vertices[i].x - offset.x;
                    vertices[i].y = (vertices[i].y - offset.y) * -1f;
                }

                mesh.vertices = vertices;
                mesh.RecalculateBounds();
                sharedMesh.objectReferenceValue = AddObjectToAsset <Mesh>(mesh, asset, HideFlags.HideInHierarchy);

//                Material sharedMaterial;
                if (outputMaterials != null && outputMaterials.Length > 0)
                {
                    sharedShaders.arraySize = outputMaterials.Length;
                    if (hasGradients)
                    {
                        for (int i = 0; i < outputMaterials.Length; i++)
                        {
                            sharedShaders.GetArrayElementAtIndex(i).stringValue = outputMaterials[i].shader.name;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < outputMaterials.Length; i++)
                        {
                            if (outputMaterials[i].shader.name == SVGShader.GradientColorAlphaBlended.name)
                            {
                                outputMaterials[i].shader = SVGShader.SolidColorAlphaBlended;
                            }
                            else if (outputMaterials[i].shader.name == SVGShader.GradientColorOpaque.name)
                            {
                                outputMaterials[i].shader = SVGShader.SolidColorOpaque;
                            }
                            sharedShaders.GetArrayElementAtIndex(i).stringValue = outputMaterials[i].shader.name;
                        }
                    }
                }

                // Serialize the Asset
                svgAsset.ApplyModifiedProperties();

                // Handle Canvas Rectangle
                System.Reflection.MethodInfo _editor_SetCanvasRectangle = typeof(SVGAsset).GetMethod("_editor_SetCanvasRectangle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                _editor_SetCanvasRectangle.Invoke(asset, new object[] { new Rect(viewport.x, viewport.y, viewport.size.x, viewport.size.y) });

                if (asset.generateCollider)
                {
                    // Create polygon contour
                    if (SVGGraphics.paths != null && SVGGraphics.paths.Count > 0)
                    {
                        List <List <Vector2> > polygons = new List <List <Vector2> >();
                        for (int i = 0; i < SVGGraphics.paths.Count; i++)
                        {
                            Vector2[] points = SVGGraphics.paths[i].points;
                            for (int j = 0; j < points.Length; j++)
                            {
                                points[j].x = points[j].x * SVGMesh.meshScale - offset.x;
                                points[j].y = (points[j].y * SVGMesh.meshScale - offset.y) * -1f;
                            }

                            polygons.Add(new List <Vector2>(points));
                        }

                        polygons = SVGGeom.MergePolygon(polygons);

                        SVGPath[] paths = new SVGPath[polygons.Count];
                        for (int i = 0; i < polygons.Count; i++)
                        {
                            paths[i] = new SVGPath(polygons[i].ToArray());
                        }

                        System.Reflection.MethodInfo _editor_SetColliderShape = typeof(SVGAsset).GetMethod("_editor_SetColliderShape", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        if (paths != null && paths.Length > 0)
                        {
                            _editor_SetColliderShape.Invoke(asset, new object[] { paths });
                        }
                        else
                        {
                            _editor_SetColliderShape.Invoke(asset, new object[] { null });
                        }
                    }
                }
                else
                {
                    System.Reflection.MethodInfo _editor_SetColliderShape = typeof(SVGAsset).GetMethod("_editor_SetColliderShape", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    _editor_SetColliderShape.Invoke(asset, new object[] { null });
                }

                if (hasGradients)
                {
                    System.Reflection.MethodInfo _editor_SetGradients = typeof(SVGAsset).GetMethod("_editor_SetGradients", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    if (atlas.gradients != null && atlas.gradients.Count > 0)
                    {
                        _editor_SetGradients.Invoke(asset, new object[] { atlas.gradients.ToArray() });
                    }
                    else
                    {
                        _editor_SetGradients.Invoke(asset, new object[] { null });
                    }
                }
#if IGNORE_EXCEPTIONS
            } catch (System.Exception exception) {
                Debug.LogWarning("Asset: " + UnityEditor.AssetDatabase.GetAssetPath(asset) + " Failed to import\n" + exception.Message, asset);
                errors.Add(SVGError.CorruptedFile);
            }
#endif

                SVGAtlas.ClearAll();
                SVGGraphics.Clear();
                if (_svgDocument != null)
                {
                    _svgDocument.Clear();
                }

                UnityEditor.EditorUtility.SetDirty(asset);
                _importingSVG = false;
            }