GetWidthAndHeight() private method

private GetWidthAndHeight ( int &width, int &height ) : void
width int
height int
return void
示例#1
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                background.Draw(r, false, false, false, false);
            }
            Texture texture = base.target as Texture;

            if (!(texture == null))
            {
                RenderTexture renderTexture = texture as RenderTexture;
                if (renderTexture != null)
                {
                    if (!SystemInfo.SupportsRenderTextureFormat(renderTexture.format))
                    {
                        return;
                    }
                    renderTexture.Create();
                }
                if (this.IsCubemap())
                {
                    this.m_CubemapPreview.OnPreviewGUI(texture, r, background);
                }
                else
                {
                    int   num  = Mathf.Max(texture.width, 1);
                    int   num2 = Mathf.Max(texture.height, 1);
                    float mipLevelForRendering = this.GetMipLevelForRendering();
                    float num3 = Mathf.Min(Mathf.Min(r.width / (float)num, r.height / (float)num2), 1f);
                    Rect  rect = new Rect(r.x, r.y, (float)num * num3, (float)num2 * num3);
                    PreviewGUI.BeginScrollView(r, this.m_Pos, rect, "PreHorizontalScrollbar", "PreHorizontalScrollbarThumb");
                    float mipMapBias = texture.mipMapBias;
                    TextureUtil.SetMipMapBiasNoDirty(texture, mipLevelForRendering - this.Log2((float)num / rect.width));
                    FilterMode filterMode = texture.filterMode;
                    TextureUtil.SetFilterModeNoDirty(texture, FilterMode.Point);
                    if (this.m_ShowAlpha)
                    {
                        EditorGUI.DrawTextureAlpha(rect, texture);
                    }
                    else
                    {
                        Texture2D texture2D = texture as Texture2D;
                        if (texture2D != null && texture2D.alphaIsTransparency)
                        {
                            EditorGUI.DrawTextureTransparent(rect, texture);
                        }
                        else
                        {
                            EditorGUI.DrawPreviewTexture(rect, texture);
                        }
                    }
                    if (rect.width > 32f && rect.height > 32f)
                    {
                        string           assetPath       = AssetDatabase.GetAssetPath(texture);
                        TextureImporter  textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
                        SpriteMetaData[] array           = (!(textureImporter != null)) ? null : textureImporter.spritesheet;
                        if (array != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple)
                        {
                            Rect rect2 = default(Rect);
                            Rect rect3 = default(Rect);
                            GUI.CalculateScaledTextureRects(rect, ScaleMode.StretchToFill, (float)texture.width / (float)texture.height, ref rect2, ref rect3);
                            int width  = texture.width;
                            int height = texture.height;
                            textureImporter.GetWidthAndHeight(ref width, ref height);
                            float num4 = (float)texture.width / (float)width;
                            HandleUtility.ApplyWireMaterial();
                            GL.PushMatrix();
                            GL.MultMatrix(Handles.matrix);
                            GL.Begin(1);
                            GL.Color(new Color(1f, 1f, 1f, 0.5f));
                            SpriteMetaData[] array2 = array;
                            for (int i = 0; i < array2.Length; i++)
                            {
                                SpriteMetaData spriteMetaData = array2[i];
                                Rect           rect4          = spriteMetaData.rect;
                                this.DrawRect(new Rect
                                {
                                    xMin = rect2.xMin + rect2.width * (rect4.xMin / (float)texture.width * num4),
                                    xMax = rect2.xMin + rect2.width * (rect4.xMax / (float)texture.width * num4),
                                    yMin = rect2.yMin + rect2.height * (1f - rect4.yMin / (float)texture.height * num4),
                                    yMax = rect2.yMin + rect2.height * (1f - rect4.yMax / (float)texture.height * num4)
                                });
                            }
                            GL.End();
                            GL.PopMatrix();
                        }
                    }
                    TextureUtil.SetMipMapBiasNoDirty(texture, mipMapBias);
                    TextureUtil.SetFilterModeNoDirty(texture, filterMode);
                    this.m_Pos = PreviewGUI.EndScrollView();
                    if (mipLevelForRendering != 0f)
                    {
                        EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20f), "Mip " + mipLevelForRendering);
                    }
                }
            }
        }
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                background.Draw(r, false, false, false, false);
            }

            // show texture
            Texture t = target as Texture;

            if (t == null) // texture might be gone by now, in case this code is used for floating texture preview
            {
                return;
            }

            // Render target must be created before we can display it (case 491797)
            RenderTexture rt = t as RenderTexture;

            if (rt != null)
            {
                if (!SystemInfo.IsFormatSupported(rt.graphicsFormat, FormatUsage.Render))
                {
                    return; // can't do this RT format
                }
                rt.Create();
            }

            if (IsCubemap())
            {
                m_CubemapPreview.OnPreviewGUI(t, r, background);
                return;
            }

            // target can report zero sizes in some cases just after a parameter change;
            // guard against that.
            int texWidth  = Mathf.Max(t.width, 1);
            int texHeight = Mathf.Max(t.height, 1);

            float mipLevel   = GetMipLevelForRendering();
            float zoomLevel  = Mathf.Min(Mathf.Min(r.width / texWidth, r.height / texHeight), 1);
            Rect  wantedRect = new Rect(r.x, r.y, texWidth * zoomLevel, texHeight * zoomLevel);

            PreviewGUI.BeginScrollView(r, m_Pos, wantedRect, "PreHorizontalScrollbar", "PreHorizontalScrollbarThumb");
            FilterMode oldFilter = t.filterMode;

            TextureUtil.SetFilterModeNoDirty(t, FilterMode.Point);
            Texture2D      t2d            = t as Texture2D;
            ColorWriteMask colorWriteMask = ColorWriteMask.All;

            switch (m_PreviewMode)
            {
            case PreviewMode.R:
                colorWriteMask = ColorWriteMask.Red | ColorWriteMask.Alpha;
                break;

            case PreviewMode.G:
                colorWriteMask = ColorWriteMask.Green | ColorWriteMask.Alpha;
                break;

            case PreviewMode.B:
                colorWriteMask = ColorWriteMask.Blue | ColorWriteMask.Alpha;
                break;
            }

            if (m_PreviewMode == PreviewMode.A)
            {
                EditorGUI.DrawTextureAlpha(wantedRect, t, ScaleMode.StretchToFill, 0, mipLevel);
            }
            else
            {
                if (t2d != null && t2d.alphaIsTransparency)
                {
                    EditorGUI.DrawTextureTransparent(wantedRect, t, ScaleMode.StretchToFill, 0, mipLevel, colorWriteMask);
                }
                else
                {
                    EditorGUI.DrawPreviewTexture(wantedRect, t, null, ScaleMode.StretchToFill, 0, mipLevel, colorWriteMask);
                }
            }

            // TODO: Less hacky way to prevent sprite rects to not appear in smaller previews like icons.
            if ((wantedRect.width > 32 && wantedRect.height > 32) && Event.current.type == EventType.Repaint)
            {
                string           path            = AssetDatabase.GetAssetPath(t);
                TextureImporter  textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                SpriteMetaData[] spritesheet     = textureImporter != null ? textureImporter.spritesheet : null;

                if (spritesheet != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple)
                {
                    Rect screenRect = new Rect();
                    Rect sourceRect = new Rect();
                    GUI.CalculateScaledTextureRects(wantedRect, ScaleMode.StretchToFill, (float)t.width / (float)t.height, ref screenRect, ref sourceRect);

                    int origWidth  = t.width;
                    int origHeight = t.height;
                    textureImporter.GetWidthAndHeight(ref origWidth, ref origHeight);
                    float definitionScale = (float)t.width / (float)origWidth;

                    HandleUtility.ApplyWireMaterial();
                    GL.PushMatrix();
                    GL.MultMatrix(Handles.matrix);
                    GL.Begin(GL.LINES);
                    GL.Color(new Color(1f, 1f, 1f, 0.5f));
                    foreach (SpriteMetaData sprite in spritesheet)
                    {
                        Rect spriteRect       = sprite.rect;
                        Rect spriteScreenRect = new Rect();
                        spriteScreenRect.xMin = screenRect.xMin + screenRect.width * (spriteRect.xMin / t.width * definitionScale);
                        spriteScreenRect.xMax = screenRect.xMin + screenRect.width * (spriteRect.xMax / t.width * definitionScale);
                        spriteScreenRect.yMin = screenRect.yMin + screenRect.height * (1f - spriteRect.yMin / t.height * definitionScale);
                        spriteScreenRect.yMax = screenRect.yMin + screenRect.height * (1f - spriteRect.yMax / t.height * definitionScale);
                        DrawRect(spriteScreenRect);
                    }
                    GL.End();
                    GL.PopMatrix();
                }
            }

            TextureUtil.SetFilterModeNoDirty(t, oldFilter);

            m_Pos = PreviewGUI.EndScrollView();
            if (mipLevel != 0)
            {
                EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20), "Mip " + mipLevel);
            }
        }
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                background.Draw(r, false, false, false, false);
            }
            Texture       target   = base.target as Texture;
            RenderTexture texture2 = target as RenderTexture;

            if (texture2 != null)
            {
                if (!SystemInfo.SupportsRenderTextureFormat(texture2.format))
                {
                    return;
                }
                texture2.Create();
            }
            if (this.IsCubemap())
            {
                this.m_CubemapPreview.OnPreviewGUI(target, r, background);
            }
            else
            {
                int   num  = Mathf.Max(target.width, 1);
                int   num2 = Mathf.Max(target.height, 1);
                float mipLevelForRendering = this.GetMipLevelForRendering();
                float num4     = Mathf.Min(Mathf.Min((float)(r.width / ((float)num)), (float)(r.height / ((float)num2))), 1f);
                Rect  viewRect = new Rect(r.x, r.y, num * num4, num2 * num4);
                PreviewGUI.BeginScrollView(r, this.m_Pos, viewRect, "PreHorizontalScrollbar", "PreHorizontalScrollbarThumb");
                float mipMapBias = target.mipMapBias;
                TextureUtil.SetMipMapBiasNoDirty(target, mipLevelForRendering - this.Log2(((float)num) / viewRect.width));
                UnityEngine.FilterMode filterMode = target.filterMode;
                TextureUtil.SetFilterModeNoDirty(target, UnityEngine.FilterMode.Point);
                if (this.m_ShowAlpha)
                {
                    EditorGUI.DrawTextureAlpha(viewRect, target);
                }
                else
                {
                    Texture2D textured = target as Texture2D;
                    if ((textured != null) && textured.alphaIsTransparency)
                    {
                        EditorGUI.DrawTextureTransparent(viewRect, target);
                    }
                    else
                    {
                        EditorGUI.DrawPreviewTexture(viewRect, target);
                    }
                }
                if ((viewRect.width > 32f) && (viewRect.height > 32f))
                {
                    TextureImporter  atPath      = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(target)) as TextureImporter;
                    SpriteMetaData[] spritesheet = atPath?.spritesheet;
                    if ((spritesheet != null) && (atPath.spriteImportMode == SpriteImportMode.Multiple))
                    {
                        Rect outScreenRect = new Rect();
                        Rect outSourceRect = new Rect();
                        GUI.CalculateScaledTextureRects(viewRect, ScaleMode.StretchToFill, ((float)target.width) / ((float)target.height), ref outScreenRect, ref outSourceRect);
                        int width  = target.width;
                        int height = target.height;
                        atPath.GetWidthAndHeight(ref width, ref height);
                        float num8 = ((float)target.width) / ((float)width);
                        HandleUtility.ApplyWireMaterial();
                        GL.PushMatrix();
                        GL.MultMatrix(Handles.matrix);
                        GL.Begin(1);
                        GL.Color(new Color(1f, 1f, 1f, 0.5f));
                        foreach (SpriteMetaData data in spritesheet)
                        {
                            Rect rect  = data.rect;
                            Rect rect5 = new Rect {
                                xMin = outScreenRect.xMin + (outScreenRect.width * ((rect.xMin / ((float)target.width)) * num8)),
                                xMax = outScreenRect.xMin + (outScreenRect.width * ((rect.xMax / ((float)target.width)) * num8)),
                                yMin = outScreenRect.yMin + (outScreenRect.height * (1f - ((rect.yMin / ((float)target.height)) * num8))),
                                yMax = outScreenRect.yMin + (outScreenRect.height * (1f - ((rect.yMax / ((float)target.height)) * num8)))
                            };
                            this.DrawRect(rect5);
                        }
                        GL.End();
                        GL.PopMatrix();
                    }
                }
                TextureUtil.SetMipMapBiasNoDirty(target, mipMapBias);
                TextureUtil.SetFilterModeNoDirty(target, filterMode);
                this.m_Pos = PreviewGUI.EndScrollView();
                if (mipLevelForRendering != 0f)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20f), "Mip " + mipLevelForRendering);
                }
            }
        }
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                background.Draw(r, false, false, false, false);
            }
            Texture       target        = this.target as Texture;
            RenderTexture renderTexture = target as RenderTexture;

            if ((UnityEngine.Object)renderTexture != (UnityEngine.Object)null)
            {
                if (!SystemInfo.SupportsRenderTextureFormat(renderTexture.format))
                {
                    return;
                }
                renderTexture.Create();
            }
            if (this.IsCubemap())
            {
                this.m_CubemapPreview.OnPreviewGUI(target, r, background);
            }
            else
            {
                int   num1 = Mathf.Max(target.width, 1);
                int   num2 = Mathf.Max(target.height, 1);
                float levelForRendering = this.GetMipLevelForRendering();
                float num3  = Mathf.Min(Mathf.Min(r.width / (float)num1, r.height / (float)num2), 1f);
                Rect  rect1 = new Rect(r.x, r.y, (float)num1 * num3, (float)num2 * num3);
                PreviewGUI.BeginScrollView(r, this.m_Pos, rect1, (GUIStyle)"PreHorizontalScrollbar", (GUIStyle)"PreHorizontalScrollbarThumb");
                float mipMapBias = target.mipMapBias;
                TextureUtil.SetMipMapBiasNoDirty(target, levelForRendering - this.Log2((float)num1 / rect1.width));
                UnityEngine.FilterMode filterMode = target.filterMode;
                TextureUtil.SetFilterModeNoDirty(target, UnityEngine.FilterMode.Point);
                if (this.m_ShowAlpha)
                {
                    EditorGUI.DrawTextureAlpha(rect1, target);
                }
                else
                {
                    Texture2D texture2D = target as Texture2D;
                    if ((UnityEngine.Object)texture2D != (UnityEngine.Object)null && texture2D.alphaIsTransparency)
                    {
                        EditorGUI.DrawTextureTransparent(rect1, target);
                    }
                    else
                    {
                        EditorGUI.DrawPreviewTexture(rect1, target);
                    }
                }
                if ((double)rect1.width > 32.0 && (double)rect1.height > 32.0)
                {
                    TextureImporter  atPath = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath((UnityEngine.Object)target)) as TextureImporter;
                    SpriteMetaData[] spriteMetaDataArray = !((UnityEngine.Object)atPath != (UnityEngine.Object)null) ? (SpriteMetaData[])null : atPath.spritesheet;
                    if (spriteMetaDataArray != null && atPath.spriteImportMode == SpriteImportMode.Multiple)
                    {
                        Rect outScreenRect = new Rect();
                        Rect outSourceRect = new Rect();
                        GUI.CalculateScaledTextureRects(rect1, ScaleMode.StretchToFill, (float)target.width / (float)target.height, ref outScreenRect, ref outSourceRect);
                        int width  = target.width;
                        int height = target.height;
                        atPath.GetWidthAndHeight(ref width, ref height);
                        float num4 = (float)target.width / (float)width;
                        HandleUtility.ApplyWireMaterial();
                        GL.PushMatrix();
                        GL.MultMatrix(Handles.matrix);
                        GL.Begin(1);
                        GL.Color(new Color(1f, 1f, 1f, 0.5f));
                        foreach (SpriteMetaData spriteMetaData in spriteMetaDataArray)
                        {
                            Rect rect2 = spriteMetaData.rect;
                            this.DrawRect(new Rect()
                            {
                                xMin = outScreenRect.xMin + outScreenRect.width * (rect2.xMin / (float)target.width * num4),
                                xMax = outScreenRect.xMin + outScreenRect.width * (rect2.xMax / (float)target.width * num4),
                                yMin = outScreenRect.yMin + outScreenRect.height * (float)(1.0 - (double)rect2.yMin / (double)target.height * (double)num4),
                                yMax = outScreenRect.yMin + outScreenRect.height * (float)(1.0 - (double)rect2.yMax / (double)target.height * (double)num4)
                            });
                        }
                        GL.End();
                        GL.PopMatrix();
                    }
                }
                TextureUtil.SetMipMapBiasNoDirty(target, mipMapBias);
                TextureUtil.SetFilterModeNoDirty(target, filterMode);
                this.m_Pos = PreviewGUI.EndScrollView();
                if ((double)levelForRendering == 0.0)
                {
                    return;
                }
                EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20f), "Mip " + (object)levelForRendering);
            }
        }