public void StartProcess(SVGAsset asset) { if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) { return; } if (errors == null) { errors = new List <SVGError>(); } else { errors.Clear(); } _importingSVG = true; System.Reflection.FieldInfo _editor_runtimeMaterials = typeof(SVGAsset).GetField("_runtimeMaterials", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); _editor_runtimeMaterials.SetValue(asset, null); System.Reflection.FieldInfo _editor_runtimeMesh = typeof(SVGAsset).GetField("_runtimeMesh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); _editor_runtimeMesh.SetValue(asset, null); UnityEditor.SerializedObject svgAsset = new UnityEditor.SerializedObject(asset); UnityEditor.SerializedProperty sharedMesh = svgAsset.FindProperty("_sharedMesh"); UnityEditor.SerializedProperty sharedShaders = svgAsset.FindProperty("_sharedShaders"); Clear(); SVGParser.Init(); SVGGraphics.Init(); atlasData = new SVGAtlasData(); atlasData.Init(SVGAtlas.defaultAtlasTextureWidth * SVGAtlas.defaultAtlasTextureHeight); atlasData.AddGradient(SVGAtlasData.GetDefaultGradient()); 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); _importingSVG = false; return; } #if IGNORE_EXCEPTIONS try { #endif _rootSVGElement.Render(); Rect viewport = _rootSVGElement.paintable.viewport; viewport.x *= SVGAssetImport.meshScale; viewport.y *= SVGAssetImport.meshScale; viewport.size *= SVGAssetImport.meshScale; Vector2 offset; SVGGraphics.CorrectSVGLayers(SVGGraphics.layers, viewport, asset, out offset); // Handle gradients bool hasGradients = false; // Create actual Mesh Shader[] outputShaders; Mesh mesh = new Mesh(); SVGMesh.CombineMeshes(SVGGraphics.layers.ToArray(), mesh, out outputShaders, useGradients, format, compressDepth, asset.antialiasing); if (mesh == null) { return; } if (useGradients == SVGUseGradients.Always) { if (outputShaders != null) { for (int i = 0; i < outputShaders.Length; i++) { if (outputShaders[i] == null) { continue; } if (outputShaders[i].name == SVGShader.SolidColorOpaque.name) { outputShaders[i] = SVGShader.GradientColorOpaque; } else if (outputShaders[i].name == SVGShader.SolidColorAlphaBlended.name) { outputShaders[i] = SVGShader.GradientColorAlphaBlended; } else if (outputShaders[i].name == SVGShader.SolidColorAlphaBlendedAntialiased.name) { outputShaders[i] = SVGShader.GradientColorAlphaBlendedAntialiased; } } } hasGradients = true; } else { if (outputShaders != null) { for (int i = 0; i < outputShaders.Length; i++) { if (outputShaders[i] == null) { continue; } if (outputShaders[i].name == SVGShader.GradientColorOpaque.name || outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name || outputShaders[i].name == SVGShader.GradientColorAlphaBlendedAntialiased.name || outputShaders[i].name == SVGShader.GradientColorAlphaBlendedAntialiasedCompressed.name) { hasGradients = true; break; } } } } if (!asset.useLayers) { sharedMesh.objectReferenceValue = AddObjectToAsset <Mesh>(mesh, asset, HideFlags.HideInHierarchy); } // Material sharedMaterial; if (outputShaders != null && outputShaders.Length > 0) { sharedShaders.arraySize = outputShaders.Length; if (hasGradients) { for (int i = 0; i < outputShaders.Length; i++) { sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].name; } } else { for (int i = 0; i < outputShaders.Length; i++) { if (outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name) { outputShaders[i] = SVGShader.SolidColorAlphaBlended; } else if (outputShaders[i].name == SVGShader.GradientColorOpaque.name) { outputShaders[i] = SVGShader.SolidColorOpaque; } sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].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 * SVGAssetImport.meshScale - offset.x; points[j].y = (points[j].y * SVGAssetImport.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 }); } System.Reflection.MethodInfo _editor_SetGradients = typeof(SVGAsset).GetMethod("_editor_SetGradients", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); _editor_SetGradients.Invoke(asset, new object[] { null }); if (hasGradients) { if (atlasData.gradientCache != null && atlasData.gradientCache.Count > 0) { int gradientsCount = SVGAssetImport.atlasData.gradientCache.Count; CCGradient[] gradients = new CCGradient[gradientsCount]; int i = 0; foreach (KeyValuePair <string, CCGradient> entry in SVGAssetImport.atlasData.gradientCache) { gradients[i++] = entry.Value; } _editor_SetGradients.Invoke(asset, new object[] { gradients }); } } System.Reflection.MethodInfo _editor_SetLayers = typeof(SVGAsset).GetMethod("_editor_SetLayers", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); _editor_SetLayers.Invoke(asset, new object[] { null }); if (asset.useLayers) { if (SVGGraphics.layers != null && SVGGraphics.layers.Count > 0) { _editor_SetLayers.Invoke(asset, new object[] { SVGGraphics.layers.ToArray() }); } } #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 if (_svgDocument != null) { _svgDocument.Clear(); _svgDocument = null; } Clear(); UnityEditor.EditorUtility.SetDirty(asset); _importingSVG = false; }
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"); Clear(); SVGParser.Init(); SVGGraphics.Init(); atlasData = new SVGAtlasData(); 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); _importingSVG = false; return; } SVGGraphics.depthTree = new SVGDepthTree(_rootSVGElement.paintable.viewport); #if IGNORE_EXCEPTIONS try { #endif _rootSVGElement.Render(); // Handle gradients bool hasGradients = (useGradients == SVGUseGradients.Always); // Create actual Mesh Shader[] outputShaders; SVGLayer[] outputLayers; Mesh mesh = SVGMesh.CombineMeshes(SVGGraphics.meshes, out outputLayers, out outputShaders, useGradients, format, compressDepth); if (mesh == null) { return; } if (outputShaders != null) { for (int i = 0; i < outputShaders.Length; i++) { if (outputShaders[i] == null) { continue; } if (outputShaders[i].name == SVGShader.GradientColorOpaque.name || outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name) { hasGradients = true; break; } } } Vector3[] vertices = mesh.vertices; Vector2 offset; Bounds bounds = mesh.bounds; Rect viewport = _rootSVGElement.paintable.viewport; viewport.x *= SVGAssetImport.meshScale; viewport.y *= SVGAssetImport.meshScale; viewport.size *= SVGAssetImport.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 (outputShaders != null && outputShaders.Length > 0) { sharedShaders.arraySize = outputShaders.Length; if (hasGradients) { for (int i = 0; i < outputShaders.Length; i++) { sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].name; } } else { for (int i = 0; i < outputShaders.Length; i++) { if (outputShaders[i].name == SVGShader.GradientColorAlphaBlended.name) { outputShaders[i] = SVGShader.SolidColorAlphaBlended; } else if (outputShaders[i].name == SVGShader.GradientColorOpaque.name) { outputShaders[i] = SVGShader.SolidColorOpaque; } sharedShaders.GetArrayElementAtIndex(i).stringValue = outputShaders[i].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 * SVGAssetImport.meshScale - offset.x; points[j].y = (points[j].y * SVGAssetImport.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 (atlasData.gradients != null && atlasData.gradients.Count > 0) { _editor_SetGradients.Invoke(asset, new object[] { atlasData.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 if (_svgDocument != null) { _svgDocument.Clear(); _svgDocument = null; } Clear(); UnityEditor.EditorUtility.SetDirty(asset); _importingSVG = false; }