Ejemplo n.º 1
0
        internal static Texture2D BuildPreviewTexture(UnityEngine.Object obj, int width, int height)
        {
            Texture2D previewTex = null;

            var sprite = SVGImporter.GetImportedSprite(obj);

            if (sprite != null)
            {
                var mat = SVGImporter.CreateSVGSpriteMaterial(sprite);
                previewTex = VectorUtils.RenderSpriteToTexture2D(sprite, width, height, mat, 4);
                Material.DestroyImmediate(mat);
            }
#if UNITY_2019_3_OR_NEWER
            else
            {
                Vector2[] vertices = null;
                UInt16[]  indices  = null;
                Vector2[] uvs      = null;
                Color[]   colors   = null;
                Vector2[] settings = null;
                Texture2D atlas    = null;
                Vector2   size     = Vector2.zero;
                if (InternalBridge.GetDataFromVectorImage(obj, ref vertices, ref indices, ref uvs, ref colors, ref settings, ref atlas, ref size))
                {
                    var mat = SVGImporter.CreateSVGSpriteMaterial(atlas != null);
                    previewTex = InternalBridge.RenderVectorImageToTexture2D(obj, width, height, mat, 4);
                    Material.DestroyImmediate(mat);
                }
            }
#endif
            return(previewTex);
        }
Ejemplo n.º 2
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            background.Draw(r, false, false, false, false);

            var sprite = SVGImporter.GetImportedSprite(assetTarget);

            if (sprite == null)
            {
                if (assetTarget is Texture2D)
                {
                    EditorGUI.DrawTextureTransparent(r, (Texture2D)assetTarget, ScaleMode.ScaleToFit, 0.0f, 0);
                }
                return;
            }

            float zoomLevel  = Mathf.Min(r.width / sprite.rect.width, r.height / sprite.rect.height);
            Rect  wantedRect = new Rect(r.x, r.y, sprite.rect.width * zoomLevel, sprite.rect.height * zoomLevel);

            wantedRect.center = r.center;

            var previewTex = BuildPreviewTexture(sprite, (int)wantedRect.width, (int)wantedRect.height);

            if (previewTex != null)
            {
                EditorGUI.DrawTextureTransparent(r, previewTex, ScaleMode.ScaleToFit);
                UnityEngine.Object.DestroyImmediate(previewTex);
            }
        }
Ejemplo n.º 3
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            background.Draw(r, false, false, false, false);

            Texture2D previewTex = null;
            var       sourceSize = Vector2.zero;

            #if UNITY_2019_3_OR_NEWER
            UnityEngine.Object vectorImage      = null;
            Texture2D          vectorImageAtlas = null;
            #endif

            var sprite = SVGImporter.GetImportedSprite(assetTarget);
            if (sprite == null)
            {
                if (assetTarget is Texture2D)
                {
                    EditorGUI.DrawTextureTransparent(r, (Texture2D)assetTarget, ScaleMode.ScaleToFit, 0.0f, 0);
                    return;
                }
                #if UNITY_2019_3_OR_NEWER
                Vector2[] vertices       = null;
                UInt16[]  indices        = null;
                Vector2[] uvs            = null;
                Color[]   colors         = null;
                Vector2[] settingIndices = null;
                Texture2D atlas          = null;
                Vector2   size           = Vector2.zero;
                InternalBridge.GradientSettingsBridge[] settings = null;
                if (InternalBridge.GetDataFromVectorImage(assetTarget, ref vertices, ref indices, ref uvs, ref colors, ref settingIndices, ref settings, ref atlas, ref size))
                {
                    vectorImage      = assetTarget;
                    vectorImageAtlas = atlas;
                    sourceSize       = size;
                }
                #endif
            }
            else
            {
                sourceSize = sprite.rect.size;
            }

            float zoomLevel  = Mathf.Min(r.width / sourceSize.x, r.height / sourceSize.y);
            Rect  wantedRect = new Rect(r.x, r.y, sourceSize.x * zoomLevel, sourceSize.y * zoomLevel);
            wantedRect.center = r.center;

            previewTex = BuildPreviewTexture(assetTarget, (int)wantedRect.width, (int)wantedRect.height);
            if (previewTex != null)
            {
                EditorGUI.DrawTextureTransparent(r, previewTex, ScaleMode.ScaleToFit);
                Texture2D.DestroyImmediate(previewTex);
            }
        }
Ejemplo n.º 4
0
        public Sprite GetSprite()
        {
            var sprite = GetImporter().GetImportingSprite();

            if (sprite == null)
            {
                sprite = SVGImporter.GetImportedSprite(GetImporter().assetPath);
            }
            return(sprite);
        }
Ejemplo n.º 5
0
        public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
        {
            var sprite = SVGImporter.GetImportedSprite(assetTarget);

            if (sprite == null)
            {
                return(null);
            }

            return(BuildPreviewTexture(sprite, width, height));
        }
Ejemplo n.º 6
0
        public override string GetInfoString()
        {
            var sprite = SVGImporter.GetImportedSprite(assetTarget);

            if (sprite == null)
            {
                var tex = assetTarget as Texture2D;
                if (tex == null)
                {
                    return("");
                }
                return(InternalEditorBridge.GetTextureInfoString(tex));
            }

            int vertexCount = sprite.vertices.Length;
            int indexCount  = sprite.triangles.Length;

            var stats = "" + vertexCount + " Vertices (Pos";

            int vertexSize = sizeof(float) * 2;

            if (sprite.HasVertexAttribute(VertexAttribute.Color))
            {
                stats      += ", Col";
                vertexSize += 4;
            }
            if (sprite.HasVertexAttribute(VertexAttribute.TexCoord0))
            {
                stats      += ", TexCoord0";
                vertexSize += sizeof(float) * 2;
            }
            if (sprite.HasVertexAttribute(VertexAttribute.TexCoord1))
            {
                stats      += ", TexCoord1";
                vertexSize += sizeof(float) * 2;
            }
            if (sprite.HasVertexAttribute(VertexAttribute.TexCoord2))
            {
                stats      += ", TexCoord2";
                vertexSize += sizeof(float) * 2;
            }

            stats += ") " + HumanReadableSize(vertexSize * vertexCount + indexCount * 2);

            return(stats);
        }
Ejemplo n.º 7
0
        public void Load(SerializedObject so)
        {
            var importer = so.targetObject as SVGImporter;
            var sprite   = SVGImporter.GetImportedSprite(importer.assetPath);

            if (sprite == null)
            {
                return;
            }

            SpriteRect.name = sprite.name;

            int targetWidth;
            int targetHeight;

            importer.TextureSizeForSpriteEditor(sprite, out targetWidth, out targetHeight);
            SpriteRect.rect = new Rect(0, 0, targetWidth, targetHeight);
            var textureSize = new Vector2(targetWidth, targetHeight);

            var baseSP       = so.FindProperty("m_SpriteData");
            var spriteRectSP = baseSP.FindPropertyRelative("SpriteRect");

            SpriteRect.border = spriteRectSP.FindPropertyRelative("m_Border").vector4Value;
            SpriteRect.pivot  = sprite.pivot / textureSize;

            var guidSP = spriteRectSP.FindPropertyRelative("m_SpriteID");

            SpriteRect.spriteID = new GUID(guidSP.stringValue);

            SpriteRect.alignment = SpriteAlignment.Center;
            if (Enum.IsDefined(typeof(SpriteAlignment), (int)importer.Alignment))
            {
                SpriteRect.alignment = (SpriteAlignment)importer.Alignment;
            }
            else if (importer.Alignment == VectorUtils.Alignment.SVGOrigin)
            {
                SpriteRect.alignment = SpriteAlignment.Custom;
            }
            m_PrevAlignment = SpriteRect.alignment;
            m_PrevPivot     = SpriteRect.pivot;
        }