UnityEditor.PreviewHelpers.AdjustWidthAndHeightForStaticPreview C# (CSharp) Method

AdjustWidthAndHeightForStaticPreview() static private method

static private AdjustWidthAndHeightForStaticPreview ( int textureWidth, int textureHeight, int &width, int &height ) : void
textureWidth int
textureHeight int
width int
height int
return void
        internal static void AdjustWidthAndHeightForStaticPreview(int textureWidth, int textureHeight, ref int width, ref int height)
        {
            int max = width;
            int num2 = height;
            if ((textureWidth <= width) && (textureHeight <= height))
            {
                width = textureWidth;
                height = textureHeight;
            }
            else
            {
                float b = ((float) height) / ((float) textureWidth);
                float a = ((float) width) / ((float) textureHeight);
                float num5 = Mathf.Min(a, b);
                width = Mathf.RoundToInt(textureWidth * num5);
                height = Mathf.RoundToInt(textureHeight * num5);
            }
            width = Mathf.Clamp(width, 2, max);
            height = Mathf.Clamp(height, 2, num2);
        }
    }

Usage Example

示例#1
0
        public sealed override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
        {
            TerrainLayer t = AssetDatabase.LoadMainAssetAtPath(assetPath) as TerrainLayer;

            if (t == null || t.diffuseTexture == null)
            {
                return(null);
            }

            int texwidth  = t.diffuseTexture.width;
            int texheight = t.diffuseTexture.height;

            PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texwidth, texheight, ref width, ref height);

            RenderTexture oldRT  = RenderTexture.active;
            RenderTexture tempRT = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32);

            Graphics.Blit(t.diffuseTexture, tempRT);
            Texture2D previewTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);

            RenderTexture.active = tempRT;
            previewTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            previewTexture.Apply();
            RenderTexture.ReleaseTemporary(tempRT);
            tempRT = null;
            RenderTexture.active = oldRT;
            return(previewTexture);
        }
All Usage Examples Of UnityEditor.PreviewHelpers::AdjustWidthAndHeightForStaticPreview