Ejemplo n.º 1
0
        protected void GetDefaultMaterial()
        {
            if (_lastVectorGraphics != _vectorGraphics)
            {
                if (_lastVectorGraphics != null)
                {
                    _lastVectorGraphics.RemoveReference(this);
                }
                if (_vectorGraphics != null)
                {
                    _vectorGraphics.AddReference(this);
                }
                _lastVectorGraphics = _vectorGraphics;
                Clear();
            }

            if (_vectorGraphics != null)
            {
                if (_defaultMaterial == null)
                {
                                        #if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 || UNITY_5_0 || UNITY_5_1
                    if (!this.m_IncludeForMasking)
                    {
                        _defaultMaterial = SVGAtlas.Instance.ui;
                    }
                    else
                    {
                        _defaultMaterial = SVGAtlas.Instance.uiMask;
                    }
                    #else
                    _defaultMaterial = _vectorGraphics.sharedUIMaterial;
                    #endif
                }
            }
        }
Ejemplo n.º 2
0
 protected override void Reset()
 {
     Clear();
     _vectorGraphics = null;
     UpdateMaterial();
     base.Reset();
 }
Ejemplo n.º 3
0
        // Imports my asset from the file
        static void ImportVGAsset(string asset)
        {
            string directoryName            = Path.GetDirectoryName(asset);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(asset);
            string outputFileName           = directoryName + "/" + fileNameWithoutExtension + ".asset";

            string svgText;

            using (StreamReader sr = new StreamReader(asset))
            {
                svgText = sr.ReadToEnd();
            }

            SVGAsset svgAsset = AssetDatabase.LoadAssetAtPath(outputFileName, typeof(SVGAsset)) as SVGAsset;

            if (svgAsset == null)
            {
                svgAsset = ScriptableObject.CreateInstance <SVGAsset>();
                InitDefaultValues(svgAsset);
                AssetDatabase.CreateAsset(svgAsset, outputFileName);
            }
            AssetDatabase.DeleteAsset(asset);

            FieldInfo _svgFile = typeof(SVGAsset).GetField("_svgFile", BindingFlags.NonPublic | BindingFlags.Instance);

            _svgFile.SetValue(svgAsset, svgText);

            MethodInfo _editor_ApplyChanges = typeof(SVGAsset).GetMethod("_editor_ApplyChanges", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_ApplyChanges.Invoke(svgAsset, new object[] { true });
        }
Ejemplo n.º 4
0
        // Call this function during Start or Awake only once
        public static RenderTexture RenderSVG(SVGAsset svgAsset, Rect textureSize)
        {
            Bounds bounds = svgAsset.bounds;

            // Initialize
            renderer.transform.position = camera.transform.forward * (camera.nearClipPlane + svgAsset.bounds.size.z + 1f) - svgAsset.bounds.center;
            renderer.vectorGraphics     = svgAsset;

            if (bounds.size.x > bounds.size.y)
            {
                camera.orthographicSize = Mathf.Min(bounds.size.x, bounds.size.y) * 0.5f;
            }
            else
            {
                camera.orthographicSize = Mathf.Max(bounds.size.x, bounds.size.y) * 0.5f;
            }

            RenderTexture rt = GetRenderTexture(svgAsset, textureSize);

            camera.targetTexture = rt;
            camera.Render();
            camera.targetTexture = null;
            RemoveSVGRenderer();
            RemoveCamera();

            return(rt);
        }
Ejemplo n.º 5
0
 protected static Material[] GetMaterial(SVGAsset targetObject)
 {
     try {
         PropertyInfo _editor_sharedMaterials = typeof(SVGAsset).GetProperty("_editor_sharedMaterials", BindingFlags.NonPublic | BindingFlags.Instance);
         return((Material[])_editor_sharedMaterials.GetValue(targetObject, new object[0]));
     } catch {
         return(null);
     }
 }
Ejemplo n.º 6
0
 protected static Mesh GetMesh(SVGAsset targetObject)
 {
     try {
         PropertyInfo _editor_sharedMesh = typeof(SVGAsset).GetProperty("_editor_sharedMesh", BindingFlags.NonPublic | BindingFlags.Instance);
         return((Mesh)_editor_sharedMesh.GetValue(targetObject, new object[0]));
     } catch {
         return(null);
     }
 }
Ejemplo n.º 7
0
        protected override void PrepareForRendering(SVGLayer[] layers, SVGAsset svgAsset, bool force)
        {
            if (!active)
            {
                return;
            }
            if (layers == null)
            {
                return;
            }
            int totalLayers = layers.Length;

            if (!useSelection)
            {
                for (int i = 0; i < totalLayers; i++)
                {
                    if (layers[i].shapes == null)
                    {
                        continue;
                    }
                    int shapesLength = layers[i].shapes.Length;
                    for (int j = 0; j < shapesLength; j++)
                    {
                        layers[i].shapes[j].fill.color *= color;
                    }
                }
            }
            else
            {
                if (layerSelection.layers != null)
                {
                    int selectionCount = layerSelection.layers.Count;
                    for (int i = 0; i < selectionCount; i++)
                    {
                        int layerIndex = layerSelection.layers[i];
                        if (layerIndex < 0 || layerIndex >= totalLayers)
                        {
                            continue;
                        }
                        if (layers[layerIndex].shapes == null)
                        {
                            continue;
                        }
                        int shapesLength = layers[layerIndex].shapes.Length;
                        for (int j = 0; j < shapesLength; j++)
                        {
                            int vertexCount = layers[layerIndex].shapes[j].vertexCount;
                            for (int k = 0; k < vertexCount; k++)
                            {
                                layers[layerIndex].shapes[j].fill.color *= color;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public override string GetInfoString()
        {
            SVGAsset svgAsset = vectorGraphics.objectReferenceValue as SVGAsset;

            if (svgAsset)
            {
                return(GetEditorInfo(svgAsset));
            }
            return("");
        }
Ejemplo n.º 9
0
        void OnEnable()
        {
            Instance = this;
            asset = (SVGAsset)serializedObject.targetObject;

            anchorPositionContent = new GUIContent[anchorPosition.Length];
            for(int i = 0; i < anchorPosition.Length; i++)
            {
                anchorPositionContent[i] = new GUIContent(anchorPosition[i]);
            }

            if(serializedObject.isEditingMultipleObjects)
            {
                assets = new SVGAsset[serializedObject.targetObjects.Length];
                for(int i = 0; i < serializedObject.targetObjects.Length; i++)
                {
                    assets[i] = (SVGAsset)serializedObject.targetObjects[i];
                }
            }

            filesValid = true;
            if(serializedObject.isEditingMultipleObjects)
            {
                for(int i = 0; i < assets.Length; i++)
                {
                    if(string.IsNullOrEmpty(assets[i].svgFile))
                        filesValid = false;
                }
            } else {
                if(string.IsNullOrEmpty(asset.svgFile))
                    filesValid = false;
            }

            format = serializedObject.FindProperty("_format");
            useGradients = serializedObject.FindProperty("_useGradients");
            antialiasing = serializedObject.FindProperty("_antialiasing");
            meshCompression = serializedObject.FindProperty("_meshCompression");
            scale = serializedObject.FindProperty("_scale");
            vpm = serializedObject.FindProperty("_vpm");
            depthOffset = serializedObject.FindProperty("_depthOffset");
            compressDepth = serializedObject.FindProperty("_compressDepth");
            customPivotPoint = serializedObject.FindProperty("_customPivotPoint");
            pivotPoint = serializedObject.FindProperty("_pivotPoint");
            //border = serializedObject.FindProperty("_border");
            generateCollider = serializedObject.FindProperty("_generateCollider");
            keepSVGFile = serializedObject.FindProperty("_keepSVGFile");
            useLayers = serializedObject.FindProperty("_useLayers");
            ignoreSVGCanvas = serializedObject.FindProperty("_ignoreSVGCanvas");
            optimizeMesh = serializedObject.FindProperty("_optimizeMesh");
            generateNormals = serializedObject.FindProperty("_generateNormals");
            generateTangents = serializedObject.FindProperty("_generateTangents");

            CreateSnapshot();
            unappliedChanges = false;
        }
Ejemplo n.º 10
0
        void InitMesh()
        {
            if (_vectorGraphics == null)
            {
                CleanMesh();
            }
            // Set mesh for rendering
            if (_vectorGraphics != null)
            {
                #if UNITY_EDITOR
                if (!UnityEditor.EditorApplication.isPlaying)
                {
                    if (_sharedMesh == null)
                    {
                        _sharedMesh = _vectorGraphics.mesh;
                        SetHideFlags(_sharedMesh, HideFlags.DontSave);
                    }
                }
                else
                {
                    if (_sharedMesh != _vectorGraphics.sharedMesh)
                    {
                        _sharedMesh = _vectorGraphics.sharedMesh;
                    }
                }
                #else
                if (_sharedMesh != _vectorGraphics.sharedMesh)
                {
                    _sharedMesh = _vectorGraphics.sharedMesh;
                }
                #endif

                if (_color == Color.white && _modifiers.Count == 0)
                {
                    if (meshFilter.sharedMesh != _sharedMesh)
                    {
                        meshFilter.sharedMesh = _sharedMesh;
                    }
                }
                else
                {
                    CacheDynamicMesh();
                    if (meshFilter.sharedMesh != _mesh)
                    {
                        meshFilter.sharedMesh = _mesh;
                    }
                }
            }
            else
            {
                Clear();
                _lastVectorGraphics = null;
            }
        }
Ejemplo n.º 11
0
        protected T AddObjectToAsset <T>(T obj, SVGAsset asset, HideFlags hideFlags) where T : UnityEngine.Object
        {
            if (obj == null)
            {
                return(null);
            }

            obj.hideFlags = hideFlags;
            UnityEditor.AssetDatabase.AddObjectToAsset(obj, asset);
            return(obj);
        }
Ejemplo n.º 12
0
        protected string GetEditorInfo(SVGAsset asset)
        {
            PropertyInfo _editor_Info = typeof(SVGAsset).GetProperty("_editor_Info", BindingFlags.NonPublic | BindingFlags.Instance);
            string output = (string)_editor_Info.GetValue(asset, new object[0]);

            var fileInfo = new System.IO.FileInfo(UnityEditor.AssetDatabase.GetAssetPath(asset));
            if(fileInfo != null)
            {
                output += ", FileSize: "+string.Format(new SVGImporter.Utils.FileSizeFormatProvider(), "{0:fs}", fileInfo.Length);
            }

            return output;
        }
Ejemplo n.º 13
0
        public void SelectionGUI()
        {
            SVGAsset svgAsset = GetSVGAsset();

            if (svgAsset == null)
            {
                return;
            }
            SVGLayer[] svgLayers = svgAsset.layers;
            if (svgLayers == null || svgLayers.Length == 0)
            {
                return;
            }
            int layersLength = svgLayers.Length;

            EditorGUILayout.PropertyField(useSelection);
            if (useSelection.boolValue)
            {
                GUILayout.BeginHorizontal();
                selectingLayers = GUILayout.Toggle(selectingLayers, "Select", "Button");
                if (GUILayout.Button("Clear"))
                {
                    modifier.layerSelection.Clear();
                }
                if (GUILayout.Button("Invert"))
                {
                    HashSet <int> cache = new HashSet <int>(modifier.layerSelection.cache);
                    modifier.layerSelection.Clear();
                    for (int i = 0; i < layersLength; i++)
                    {
                        if (cache.Contains(i))
                        {
                            continue;
                        }
                        modifier.layerSelection.Add(i);
                    }
                }
                GUILayout.EndHorizontal();
                EditorGUILayout.Space();

                if (selectingLayers)
                {
                    layerList.DoLayout(svgLayers, modifier.layerSelection);
                }
            }
            else
            {
                selectingLayers = false;
            }
        }
Ejemplo n.º 14
0
 public static GameObject DropFramesToSceneToCreateGO(string name, SVGAsset[] frames, Vector3 position)
 {
     if (frames.Length > 0)
     {
         SVGAsset   asset      = frames[0];
         GameObject gameObject = DropSVGAssetToSceneToCreateGO(asset, position);
         if (frames.Length > 1)
         {
             SVGFrameAnimator svgFrameAnimator = gameObject.AddComponent <SVGFrameAnimator>();
             svgFrameAnimator.frames = frames.Clone() as SVGAsset[];
         }
         return(gameObject);
     }
     return(null);
 }
Ejemplo n.º 15
0
        public static void UpdateInstances(SerializedObject serializedObject)
        {
            if(serializedObject == null)
                return;

            if(serializedObject.targetObjects != null && serializedObject.targetObjects.Length > 0)
            {
                SVGAsset[] svgAssets = new SVGAsset[serializedObject.targetObjects.Length];
                for(int i = 0; i < svgAssets.Length; i++)
                {
                    svgAssets[i] = serializedObject.targetObjects[i] as SVGAsset;
                }

                UpdateInstances(svgAssets);
            }
        }
Ejemplo n.º 16
0
        public static GameObject DropSVGAssetToSceneToCreateGO(SVGAsset asset, Vector3 position)
        {
            GameObject go = new GameObject(asset.name);

            Undo.RegisterCreatedObjectUndo(go, "Create SVG Renderer");
            //Vector3 destination = Camera.current.ScreenToWorldPoint(new Vector3(mousePosition.x, scenePosition.height - mousePosition.y, 0f));
            //destination.z = 0f;
            go.transform.position = position;
            SVGRenderer renderer = go.AddComponent <SVGRenderer>();

            renderer.vectorGraphics = asset;
            if (asset.generateCollider)
            {
                go.AddComponent <SVGCollider2D>();
            }
            Selection.activeGameObject = go;
            return(go);
        }
Ejemplo n.º 17
0
 protected void RecoverSVGFile(SVGAsset recoverAsset)
 {
     string assetPath = AssetDatabase.GetAssetPath(recoverAsset);
     string lastPath = lastSVGRecoveryPath;
     if(string.IsNullOrEmpty(lastPath))
     {
         lastPath = assetPath;
     } else {
         lastPath += "/";
     }
     string path = EditorUtility.SaveFilePanel("Recover SVG File", Path.GetDirectoryName(lastPath), Path.GetFileNameWithoutExtension(assetPath), "svg" );
     if(!string.IsNullOrEmpty(path))
     {
         lastSVGRecoveryPath = Path.GetDirectoryName(path);
         File.WriteAllText(path, recoverAsset.svgFile);
         EditorUtility.RevealInFinder(path);
     }
 }
Ejemplo n.º 18
0
        protected override void PrepareForRendering(SVGLayer[] layers, SVGAsset svgAsset, bool force)
        {
            if (layers == null)
            {
                return;
            }
            int totalLayers = layers.Length;

            if (!useSelection)
            {
                for (int i = 0; i < totalLayers; i++)
                {
                    if (layers[i].shapes == null)
                    {
                        continue;
                    }
                    int shapesLength = layers[i].shapes.Length;
                    for (int j = 0; j < shapesLength; j++)
                    {
                        layers[i].shapes[j].fill.color *= color;
                    }
                }
            }
            else
            {
                for (int i = 0; i < totalLayers; i++)
                {
                    if (layers[i].shapes == null)
                    {
                        continue;
                    }
                    if (!layerSelection.Contains(i))
                    {
                        continue;
                    }

                    int shapesLength = layers[i].shapes.Length;
                    for (int j = 0; j < shapesLength; j++)
                    {
                        layers[i].shapes[j].fill.color *= color;
                    }
                }
            }
        }
Ejemplo n.º 19
0
        protected static RenderTexture GetRenderTexture(SVGAsset svgAsset, Rect textureSize)
        {
            float aspect = 1f;

            if (svgAsset != null)
            {
                aspect = svgAsset.bounds.size.x / svgAsset.bounds.size.y;
            }
            int           _previewResolution = Mathf.CeilToInt(textureSize.width);
            RenderTexture rt = new RenderTexture(_previewResolution,
                                                 Mathf.CeilToInt(_previewResolution / aspect),
                                                 24,
                                                 RenderTextureFormat.Default,
                                                 RenderTextureReadWrite.Default);

            rt.antiAliasing = 8;
            rt.Create();
            return(rt);
        }
Ejemplo n.º 20
0
        protected void SaveMeshFile(SVGAsset meshAsset)
        {
            string assetPath = AssetDatabase.GetAssetPath(meshAsset);
            string lastPath  = lastMeshSavePath;

            if (string.IsNullOrEmpty(lastPath))
            {
                lastPath = assetPath;
            }
            else
            {
                lastPath += "/";
            }
            string path = EditorUtility.SaveFilePanelInProject("Save Mesh File", Path.GetFileNameWithoutExtension(assetPath) + "-mesh", "asset", "", Path.GetDirectoryName(lastPath));

            //string path = EditorUtility.SaveFilePanel("Save Mesh File", Path.GetDirectoryName(lastPath), Path.GetFileNameWithoutExtension(assetPath) + "-mesh.asset", "asset");
            if (!string.IsNullOrEmpty(path))
            {
                //System.Uri assetFolderPath = new Uri(Application.dataPath);
                //System.Uri outputPath = new Uri(path);
                //path = assetFolderPath.MakeRelativeUri(outputPath).ToString();

                lastSVGRecoveryPath = Path.GetDirectoryName(path);

                Mesh sharedMesh = meshAsset.sharedMesh;
                Mesh mesh       = new Mesh();
                mesh.name      = sharedMesh.name;
                mesh.vertices  = (Vector3[])sharedMesh.vertices.Clone();
                mesh.triangles = (int[])sharedMesh.triangles.Clone();
                if (sharedMesh.uv != null || sharedMesh.uv.Length > 0)
                {
                    mesh.uv = (Vector2[])sharedMesh.uv.Clone();
                }
                if (sharedMesh.colors32 != null || sharedMesh.colors32.Length > 0)
                {
                    mesh.colors32 = (Color32[])sharedMesh.colors32.Clone();
                }
                AssetDatabase.CreateAsset(mesh, path);
                EditorUtility.RevealInFinder(path);
            }
        }
Ejemplo n.º 21
0
        public static void DoRenderPreview(SVGAsset targetObject, PreviewRenderUtility previewUtility)
        {
            if (targetObject == null)
            {
                return;
            }

            Mesh tempMesh = GetMesh(targetObject);

            if (tempMesh == null)
            {
                return;
            }
            Material[] sharedMaterials = GetMaterial(targetObject);
            if (tempMesh == null || sharedMaterials == null || sharedMaterials.Length == 0)
            {
                return;
            }

            RenderMeshPreviewSkipCameraAndLighting(tempMesh, previewUtility, sharedMaterials);
        }
Ejemplo n.º 22
0
 protected virtual void PrepareForRendering(SVGLayer[] layers, SVGAsset svgAsset, bool force)
 {
     /*
      * if(sharedMesh == null) return;
      *
      * SVGPath[] shape = svgShape.shape;
      * if(shape != null && shape.Length > 0)
      * {
      *  int[][] submeshes = new int[sharedMesh.subMeshCount][];
      *  int subMeshCount = sharedMesh.subMeshCount;
      *  int i, j;
      *  for(i = 0; i < subMeshCount; i++)
      *  {
      *      submeshes[i] = sharedMesh.GetTriangles(i);
      *  }
      *
      *  Mesh[] meshes = new Mesh[shape.Length + 1];
      *
      *  for(i = 0; i < shape.Length; i++)
      *  {
      *      int pointsLength = shape[i].points.Length - 1;
      *      StrokeSegment[] segments = new StrokeSegment[pointsLength];
      *      for(j = 0; j < pointsLength; j++)
      *      {
      *          segments[j] = new StrokeSegment(shape[i].points[j], shape[i].points[j + 1]);
      *      }
      *
      *      //meshes[i] = SVGLineUtils.StrokeMesh(segments, width, color, lineJoin, lineCap, mitterLimit, dashArray, dashOffset, closeLine, roundQuality);
      *  }
      *
      *  CombineInstance[] combineInstances = new CombineInstance[meshes.Length];
      *  for(i = 0; i < meshes.Length; i++)
      *  {
      *      combineInstances[i].mesh = meshes[i];
      *  }
      *
      *  sharedMesh.CombineMeshes(combineInstances, false, false);
      * }
      */
 }
Ejemplo n.º 23
0
 protected static Mesh GetMesh(SVGAsset targetObject)
 {
     try {
         if (targetObject.useLayers)
         {
             if (_tempMesh == null)
             {
                 _tempMesh = new Mesh();
             }
             Shader[] outputShaders;
             SVGMesh.CombineMeshes(targetObject.layers, _tempMesh, out outputShaders, targetObject.useGradients, targetObject.format, targetObject.compressDepth, targetObject.antialiasing);
             return(_tempMesh);
         }
         else
         {
             FieldInfo _sharedMesh = typeof(SVGAsset).GetField("_sharedMesh", BindingFlags.NonPublic | BindingFlags.Instance);
             return(_sharedMesh.GetValue(targetObject) as Mesh);
         }
     } catch {
         return(null);
     }
 }
Ejemplo n.º 24
0
 public void Apply(SVGAsset svgAsset)
 {
     this.format = svgAsset.format;
     this.useGradients = svgAsset.useGradients;
     this.antialiasing = svgAsset.antialiasing;
     this.meshCompression = svgAsset.meshCompression;
     this.scale = svgAsset.scale;
     this.vpm = svgAsset.vpm;
     this.depthOffset = svgAsset.depthOffset;
     this.compressDepth = svgAsset.compressDepth;
     this.customPivotPoint = svgAsset.customPivotPoint;
     this.pivotPoint = svgAsset.pivotPoint;
     this.border = svgAsset.border;
     this.sliceMesh = svgAsset.sliceMesh;
     this.generateCollider = svgAsset.generateCollider;
     this.keepSVGFile = svgAsset.keepSVGFile;
     this.useLayers = svgAsset.useLayers;
     this.ignoreSVGCanvas = svgAsset.ignoreSVGCanvas;
     this.optimizeMesh = svgAsset.optimizeMesh;
     this.generateNormals = svgAsset.generateNormals;
     this.generateTangents = svgAsset.generateTangents;
 }
Ejemplo n.º 25
0
        protected void GetDefaultMaterial()
        {
            if (_lastVectorGraphics != _vectorGraphics)
            {
                if (_lastVectorGraphics != null)
                {
                    _lastVectorGraphics.RemoveReference(this);
                }
                if (_vectorGraphics != null)
                {
                    _vectorGraphics.AddReference(this);
                }
                _lastVectorGraphics = _vectorGraphics;
                Clear();
            }

            if (_vectorGraphics != null)
            {
                if (_defaultMaterial == null)
                {
                    _defaultMaterial = _vectorGraphics.sharedUIMaterial;
                }
            }
        }
Ejemplo n.º 26
0
        // This is called always when importing something
        static void OnPostprocessAllAssets
        (
            string[] importedAssets,
            string[] deletedAssets,
            string[] movedAssets,
            string[] movedFromAssetPaths
        )
        {
            if (!active)
            {
                return;
            }

            // Hotfix: Joonas Nissinen 13.6.16 find all already imported assets and
            // remove them from the imported assets array
            string[] svgs   = Array.FindAll(importedAssets, str => str.EndsWith(".svg"));
            string[] assets = Array.FindAll(importedAssets, str => str.EndsWith(".asset"));
            importedAssets = svgs.Where(str => !assets.Contains(str.Replace(".svg", ".asset"))).ToArray();

            if (importedAssets.Length == 0)
            {
                return;
            }

            int importTotalAssets   = 0;
            int reImportTotalAssets = 0;

            List <string>   importSVGAssets   = new List <string>();
            List <SVGAsset> reimportSVGAssets = new List <SVGAsset>();

            foreach (string asset in importedAssets)
            {
                if (HasExtension(asset, svgExtension))
                {
                    importSVGAssets.Add(asset);
                }
                else if (HasExtension(asset, assetExtension))
                {
                    SVGAsset svgAsset = AssetDatabase.LoadAssetAtPath(asset, typeof(SVGAsset)) as SVGAsset;
                    if (svgAsset != null)
                    {
                        reimportSVGAssets.Add(svgAsset);
                    }
                }
            }

            importTotalAssets   = importSVGAssets.Count;
            reImportTotalAssets = reimportSVGAssets.Count;

            if (importTotalAssets + reImportTotalAssets > 0)
            {
                AssetDatabase.StartAssetEditing();
            }
            else
            {
                EditorUtility.ClearProgressBar();
                importSVGAssets.Clear();
                importSVGAssets = null;
                reimportSVGAssets.Clear();
                reimportSVGAssets = null;
                return;
            }

            //Debug.Log("importedAssets: " + importedAssets.Length +"\n"+string.Join("\n", importSVGAssets.ToArray()));
//            Debug.Log("importedAssets: " + importedAssets.Length +"\n"+string.Join("\n", importedAssets));

            // Import SVG Assets
            int    importSVGAssetsLength = importSVGAssets.Count;
            string importSVGAsset;
            int    currentAssetIndex = 0;

            for (int i = 0; i < importSVGAssetsLength; i++)
            {
                importSVGAsset = importSVGAssets[i];
                float importProgress = (float)currentAssetIndex / (float)importTotalAssets;
                if (EditorUtility.DisplayCancelableProgressBar("Importing SVG Assets", "Importing SVG Asset: " + importSVGAsset + "...", importProgress))
                {
                    EditorUtility.ClearProgressBar();
                    break;
                }
                else
                {
                    ImportVGAsset(importSVGAsset);
                    currentAssetIndex++;
                }
            }

            importSVGAssets.Clear();
            importSVGAssets = null;

            /*
             * // Reimport SVG Assets
             * int currentAssetIndex = 0;
             * foreach(SVGAsset svgAsset in reimportSVGAssets)
             * {
             *  string asset = AssetDatabase.GetAssetPath(svgAsset);
             *  float importProgress = (float)currentAssetIndex / (float)importTotalAssets;
             *  if(EditorUtility.DisplayCancelableProgressBar("Reimporting SVG Assets", "Reimporting SVG Asset: "+asset+"...", importProgress))
             *  {
             *      EditorUtility.ClearProgressBar();
             *      break;
             *  } else {
             *      ReimportSVGAsset(svgAsset, asset);
             *      currentAssetIndex++;
             *  }
             * }
             */
            EditorUtility.ClearProgressBar();
            reimportSVGAssets.Clear();
            reimportSVGAssets = null;

            AssetDatabase.SaveAssets();
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
            Canvas.ForceUpdateCanvases();
            SceneView.RepaintAll();
        }
Ejemplo n.º 27
0
        protected static void InitDefaultValues(SVGAsset asset)
        {
            FieldInfo _editor_format = typeof(SVGAsset).GetField("_format", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_format.SetValue(asset, SVGImporterEditor.settings.defaultSVGFormat);

            FieldInfo _editor_useGradients = typeof(SVGAsset).GetField("_useGradients", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_useGradients.SetValue(asset, SVGImporterEditor.settings.defaultUseGradients);

            FieldInfo _editor_antialiasing = typeof(SVGAsset).GetField("_antialiasing", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_antialiasing.SetValue(asset, SVGImporterEditor.settings.defaultAntialiasing);

            FieldInfo _editor_meshCompression = typeof(SVGAsset).GetField("_meshCompression", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_meshCompression.SetValue(asset, SVGImporterEditor.settings.defaultMeshCompression);

            FieldInfo _editor_vpm = typeof(SVGAsset).GetField("_vpm", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_vpm.SetValue(asset, SVGImporterEditor.settings.defaultVerticesPerMeter);

            FieldInfo _editor_scale = typeof(SVGAsset).GetField("_scale", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_scale.SetValue(asset, SVGImporterEditor.settings.defaultScale);

            FieldInfo _editor_depthOffset = typeof(SVGAsset).GetField("_depthOffset", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_depthOffset.SetValue(asset, SVGImporterEditor.settings.defaultScale);

            FieldInfo _editor_compressDepth = typeof(SVGAsset).GetField("_compressDepth", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_compressDepth.SetValue(asset, SVGImporterEditor.settings.defaultCompressDepth);

            FieldInfo _editor_customPivotPoint = typeof(SVGAsset).GetField("_customPivotPoint", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_customPivotPoint.SetValue(asset, SVGImporterEditor.settings.defaultCustomPivotPoint);

            FieldInfo _editor_pivotPoint = typeof(SVGAsset).GetField("_pivotPoint", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_pivotPoint.SetValue(asset, SVGImporterEditor.settings.defaultPivotPoint);

            FieldInfo _editor_generateCollider = typeof(SVGAsset).GetField("_generateCollider", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_generateCollider.SetValue(asset, SVGImporterEditor.settings.defaultGenerateCollider);

            FieldInfo _editor_keepSVGFile = typeof(SVGAsset).GetField("_keepSVGFile", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_keepSVGFile.SetValue(asset, SVGImporterEditor.settings.defaultKeepSVGFile);

            FieldInfo _editor_useLayers = typeof(SVGAsset).GetField("_useLayers", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_useLayers.SetValue(asset, SVGImporterEditor.settings.defaultUseLayers);

            FieldInfo _editor_ignoreSVGCanvas = typeof(SVGAsset).GetField("_ignoreSVGCanvas", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_ignoreSVGCanvas.SetValue(asset, SVGImporterEditor.settings.defaultIgnoreSVGCanvas);

            FieldInfo _editor_optimizeMesh = typeof(SVGAsset).GetField("_optimizeMesh", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_optimizeMesh.SetValue(asset, SVGImporterEditor.settings.defaultOptimizeMesh);

            FieldInfo _editor_generateNormals = typeof(SVGAsset).GetField("_generateNormals", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_generateNormals.SetValue(asset, SVGImporterEditor.settings.defaultGenerateNormals);

            FieldInfo _editor_generateTangents = typeof(SVGAsset).GetField("_generateTangents", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_generateTangents.SetValue(asset, SVGImporterEditor.settings.defaultGenerateTangents);
        }
Ejemplo n.º 28
0
        static void ReimportSVGAsset(SVGAsset svgAsset, string asset)
        {
            MethodInfo _editor_ApplyChanges = typeof(SVGAsset).GetMethod("_editor_ApplyChanges", BindingFlags.NonPublic | BindingFlags.Instance);

            _editor_ApplyChanges.Invoke(svgAsset, new object[] { true });
        }
Ejemplo n.º 29
0
        protected string GetEditorInfo(SVGAsset asset)
        {
            PropertyInfo _editor_Info = typeof(SVGAsset).GetProperty("_editor_Info", BindingFlags.NonPublic | BindingFlags.Instance);

            return((string)_editor_Info.GetValue(asset, new object[0]));
        }
Ejemplo n.º 30
0
        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;
            }