Exemplo 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);
        }
Exemplo 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);

            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);
            }
        }
Exemplo n.º 3
0
        public override string GetInfoString()
        {
            var sprite = SVGImporter.GetImportedSprite(assetTarget);

            if (sprite == null)
            {
                var tex = assetTarget as Texture2D;
                if (tex == null)
                {
                    return("");
                }
                return(InternalBridge.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);
        }
Exemplo n.º 4
0
        private void SetPhysicsShape(Sprite sprite)
        {
            var physicsDataProvider = (this as ISpriteEditorDataProvider).GetDataProvider <ISpritePhysicsOutlineDataProvider>();
            var outlines            = physicsDataProvider.GetOutlines(m_SpriteData.SpriteRect.spriteID);

            if (outlines.Count == 0)
            {
                if (!GeneratePhysicsShape)
                {
                    return;
                }

                var textureDataProvider = (this as ISpriteEditorDataProvider).GetDataProvider <ITextureDataProvider>();
                var tex = textureDataProvider.GetReadableTexture2D();

                outlines = InternalBridge.GenerateOutline(tex, new Rect(0, 0, tex.width, tex.height), kDefaultPhysicsTessellationDetail, kDefaultSpritePhysicsAlphaTolerance, false);
                if (outlines == null || outlines.Count == 0)
                {
                    return;
                }
            }

            int width;
            int height;

            TextureSizeForSpriteEditor(sprite, out width, out height);

            // Offset the outline inside the sprite
            foreach (var outline in outlines)
            {
                for (int i = 0; i < outline.Length; ++i)
                {
                    var v = outline[i];
                    v.x       += width / 2.0f;
                    v.y       += height / 2.0f;
                    outline[i] = v;
                }
            }

            sprite.OverridePhysicsShape(outlines.Where(o => o.Length > 2).ToArray());
        }
Exemplo n.º 5
0
        public override void OnInspectorGUI()
        {
            PropertyField(m_PixelsPerUnit, m_PixelsPerUnitText);
            PropertyField(m_GradientResolution, m_GradientResolutionText);
            IntPopup(m_Alignment, m_AlignmentText, m_AlignmentOptions);

            if (!m_Alignment.hasMultipleDifferentValues && m_Alignment.intValue == (int)VectorUtils.Alignment.Custom)
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.PropertyField(m_CustomPivot, m_CustomPivotText);
                GUILayout.EndHorizontal();
            }

            using (new EditorGUI.DisabledScope(m_SVGType.hasMultipleDifferentValues || m_SVGType.intValue == (int)SVGType.Texture2D))
                BoolToggle(m_GeneratePhysicsShape, m_GeneratePhysicsShapeText);

            BoolToggle(m_PreserveViewport, m_PreserveViewportText);

            EditorGUILayout.Space();

            IntPopup(m_AdvancedMode, m_SettingsText, m_SettingOptions);

            ++EditorGUI.indentLevel;

            if (!m_AdvancedMode.hasMultipleDifferentValues)
            {
                if (m_AdvancedMode.boolValue)
                {
                    PropertyField(m_StepDistance, m_StepDistanceText);
                    PropertyField(m_SamplingStepDistance, m_SamplingStepDistanceText);

                    BoolToggle(m_MaxCordDeviationEnabled, m_MaxCordDeviationEnabledText);
                    if (!m_MaxCordDeviationEnabled.hasMultipleDifferentValues)
                    {
                        using (new EditorGUI.DisabledScope(!m_MaxCordDeviationEnabled.boolValue))
                            PropertyField(m_MaxCordDeviation, m_MaxCordDeviationText);
                    }

                    BoolToggle(m_MaxTangentAngleEnabled, m_MaxTangentAngleEnabledText);
                    if (!m_MaxTangentAngleEnabled.hasMultipleDifferentValues)
                    {
                        using (new EditorGUI.DisabledScope(!m_MaxTangentAngleEnabled.boolValue))
                            PropertyField(m_MaxTangentAngle, m_MaxTangentAngleText);
                    }
                }
                else
                {
                    EditorGUI.BeginChangeCheck();
                    EditorGUI.showMixedValue = m_PredefinedResolutionIndex.hasMultipleDifferentValues;
                    int resolutionIndex = EditorGUILayout.Popup(m_TargetResolutionText, m_PredefinedResolutionIndex.intValue, m_TargetResolutionOptions);
                    EditorGUI.showMixedValue = false;
                    if (EditorGUI.EndChangeCheck())
                    {
                        m_PredefinedResolutionIndex.intValue = resolutionIndex;
                        if (m_PredefinedResolutionIndex.intValue != (int)SVGImporter.PredefinedResolution.Custom)
                        {
                            m_TargetResolution.intValue = TargetResolutionFromPredefinedValue((SVGImporter.PredefinedResolution)m_PredefinedResolutionIndex.intValue);
                        }
                    }

                    if (!m_PredefinedResolutionIndex.hasMultipleDifferentValues && m_PredefinedResolutionIndex.intValue == (int)SVGImporter.PredefinedResolution.Custom)
                    {
                        PropertyField(m_TargetResolution, m_CustomTargetResolutionText);
                    }

                    PropertyField(m_ResolutionMultiplier, m_ResolutionMultiplierText);
                }
            }

            --EditorGUI.indentLevel;

            EditorGUILayout.Space();

            IntPopup(m_SVGType, m_SVGTypeText, svgTypeOptions, svgTypeValues);

            if (!m_SVGType.hasMultipleDifferentValues && (m_SVGType.intValue == (int)SVGType.TexturedSprite || m_SVGType.intValue == (int)SVGType.Texture2D))
            {
                ++EditorGUI.indentLevel;

                if (m_SVGType.intValue == (int)SVGType.TexturedSprite)
                {
                    IntPopup(m_TexturedSpriteMeshType, m_TexturedSpriteMeshTypeText, texturedSpriteMeshTypeOptions, texturedSpriteMeshTypeValues);
                }

                PropertyField(m_KeepTextureAspectRatio, m_KeepTextureAspectRatioText);
                if (!m_KeepTextureAspectRatio.hasMultipleDifferentValues && m_KeepTextureAspectRatio.boolValue)
                {
                    PropertyField(m_TextureSize, m_TextureSizeText);
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel(m_TextureSizeText);
                    IntField(m_TextureWidth, GUIContent.none, GUILayout.MinWidth(40));
                    GUILayout.Label("x");
                    IntField(m_TextureHeight, GUIContent.none, GUILayout.MinWidth(40));
                    GUILayout.EndHorizontal();
                }

                IntPopup(m_WrapMode, m_WrapModeText, m_WrapModeContents, m_WrapModeValues);
                IntPopup(m_FilterMode, m_FilterModeText, m_FilterModeContents, m_FilterModeValues);
                IntPopup(m_SampleCount, m_SampleCountText, m_SampleCountContents, m_SampleCountValues);

                --EditorGUI.indentLevel;

                EditorGUILayout.Space();
            }

            if (!m_SVGType.hasMultipleDifferentValues && m_SVGType.intValue == (int)SVGType.UISVGImage)
            {
                BoolToggle(m_PreserveSVGImageAspect, m_PreserveSVGImageAspectText);
            }

            if (!m_SVGType.hasMultipleDifferentValues &&
                (m_SVGType.intValue == (int)SVGType.VectorSprite ||
                 m_SVGType.intValue == (int)SVGType.TexturedSprite ||
                 m_SVGType.intValue == (int)SVGType.UISVGImage))
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Sprite Editor"))
                {
                    InternalBridge.ShowSpriteEditorWindow();
                    // var spriteWindow = Type.GetType("UnityEditor.SpriteEditorWindow, UnityEditor", true);
                    // var getWindowMethod = spriteWindow.GetMethod("GetWindow", BindingFlags.Public | BindingFlags.Static);
                    // getWindowMethod.Invoke(null, null);
                }
                GUILayout.EndHorizontal();
            }

            ApplyRevertGUI();
        }