Ejemplo n.º 1
0
        /*
         * public static void HideGizmos()
         * {
         *  var Annotation = System.Type.GetType("UnityEditor.Annotation, UnityEditor");
         *  var ClassId = Annotation.GetField("classID");
         *  var ScriptClass = Annotation.GetField("scriptClass");
         *
         *  System.Type AnnotationUtility = System.Type.GetType("UnityEditor.AnnotationUtility, UnityEditor");
         *  var GetAnnotations = AnnotationUtility.GetMethod("GetAnnotations", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
         *  //var SetGizmoEnabled = AnnotationUtility.GetMethod("SetGizmoEnabled", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
         *  var SetIconEnabled = AnnotationUtility.GetMethod("SetIconEnabled", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
         *
         *  System.Array annotations = (System.Array)GetAnnotations.Invoke(null, null);
         *  foreach (var a in annotations)
         *  {
         *      int classId = (int)ClassId.GetValue(a);
         *      string scriptClass = (string)ScriptClass.GetValue(a);
         *      if(string.IsNullOrEmpty(scriptClass)) continue;
         *
         *      if(scriptClass == typeof(SVGAsset).Name ||
         *         scriptClass == typeof(SVGAtlas).Name ||
         *         scriptClass == typeof(SVGImage).Name ||
         *         scriptClass == typeof(SVGRenderer).Name ||
         *         scriptClass == typeof(SVGCollider2D).Name
         *         )
         *      {
         *          if(SetIconEnabled != null)
         *              SetIconEnabled.Invoke(null, new object[] { classId, scriptClass, 0 });
         *      }
         *  }
         * }
         */

        static void PlaymodeStateChanged()
        {
            SVGAtlas svgAtlas = Object.FindObjectOfType <SVGAtlas>();

            if (svgAtlas != null)
            {
                svgAtlas.ClearAll();
            }

            /*
             * SVGImage[] svgImages = Resources.FindObjectsOfTypeAll<SVGImage>();
             * if(svgImages != null && svgImages.Length > 0)
             * {
             *  for(int i = 0; i < svgImages.Length; i++)
             *  {
             *      if(svgImages[i] == null)
             *          continue;
             *      if(svgImages[i].vectorGraphics == null)
             *          continue;
             *      svgImages[i].vectorGraphics._editor_ClearCache();
             *  }
             * }
             */
            //visible = visible;
        }
Ejemplo n.º 2
0
 internal static void Init()
 {
     Camera[] cameras = SVGAtlas.GetAllCameras();
     if (cameras != null && cameras.Length > 0)
     {
         for (int i = 0; i < cameras.Length; i++)
         {
             SVGAtlas.AddComponent <PrivateBetaBuild>(cameras[i]);
         }
     }
 }
Ejemplo n.º 3
0
        public static void ClearAll()
        {
            if (_Instance == null)
            {
                return;
            }
            if (_Instance.ui != null)
            {
                DestroyObjectInternal(_Instance.ui);
                _Instance.ui = null;
            }
            if (_Instance.uiMask != null)
            {
                DestroyObjectInternal(_Instance.uiMask);
                _Instance.uiMask = null;
            }
            if (_Instance.opaqueSolid != null)
            {
                DestroyObjectInternal(_Instance.opaqueSolid);
                _Instance.opaqueSolid = null;
            }
            if (_Instance.transparentSolid != null)
            {
                DestroyObjectInternal(_Instance.transparentSolid);
                _Instance.transparentSolid = null;
            }
            if (_Instance.opaqueGradient != null)
            {
                DestroyObjectInternal(_Instance.opaqueGradient);
                _Instance.opaqueGradient = null;
            }
            if (_Instance.transparentGradient != null)
            {
                DestroyObjectInternal(_Instance.transparentGradient);
                _Instance.transparentGradient = null;
            }

            _Instance.ClearAllData();
            _Instance.ClearMaterials();
            _Instance.ClearAtlasTextures();

            DestroyObjectInternal(_Instance.gameObject);
            _Instance = null;
        }
Ejemplo n.º 4
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (gradientTexture == null)
            {
                gradientTexture = new Texture2D(128, 16);
            }

            SVGAtlas atlas = target as SVGAtlas;

            if (atlas.atlasData != null && atlas.atlasData.gradients != null)
            {
//				Debug.Log(atlas.atlasData);

                /*
                 * for(int i = 0; i < atlas.atlasData.gradients.Length; i++)
                 * {
                 *      //SVGAtlas.RenderGradient(gradientTexture, atlas.atlasData.gradients[i], 0, 0, gradientTexture.width, gradientTexture.height);
                 *      //GUILayout.Box(gradientTexture);
                 * }
                 */
            }
        }
Ejemplo n.º 5
0
        public static void DoRenderPreview(SVGAsset targetObject, PreviewRenderUtility previewUtility)
        {
            if (targetObject == null)
            {
                return;
            }

            bool hasGradients = targetObject.hasGradients;

            Mesh tempMesh = GetMesh(targetObject);

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

            Texture2D gradientAtlas = null;

            if (hasGradients)
            {
                gradientAtlas   = SVGAtlas.GenerateGradientAtlasTexture(targetObject.sharedGradients, SVGAtlas.defaultGradientWidth, SVGAtlas.defaultGradientHeight);
                outputMaterials = new Material[sharedMaterials.Length];
                for (int i = 0; i < sharedMaterials.Length; i++)
                {
                    if (sharedMaterials[i] == null)
                    {
                        continue;
                    }
                    outputMaterials[i] = Instantiate <Material>(sharedMaterials[i]);
                    if (outputMaterials[i].HasProperty("_Params"))
                    {
                        outputMaterials[i].SetVector("_Params", new Vector4(gradientAtlas.width, gradientAtlas.height, SVGAtlas.defaultGradientWidth, SVGAtlas.defaultGradientHeight));
                    }
                    if (outputMaterials[i].HasProperty("_GradientColor"))
                    {
                        outputMaterials[i].SetTexture("_GradientColor", gradientAtlas);
                    }
                    if (outputMaterials[i].HasProperty("_GradientShape"))
                    {
                        outputMaterials[i].SetTexture("_GradientShape", SVGAtlas.gradientShapeTexture);
                    }
                }
            }

            RenderMeshPreviewSkipCameraAndLighting(tempMesh, previewUtility, outputMaterials);

            if (hasGradients)
            {
                for (int i = 0; i < outputMaterials.Length; i++)
                {
                    if (outputMaterials[i] == null)
                    {
                        continue;
                    }
                    DestroyImmediate(outputMaterials[i]);
                }
            }

            if (gradientAtlas != null)
            {
                DestroyImmediate(gradientAtlas);
            }
        }
Ejemplo n.º 6
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;
            }