public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
{
if (!ShaderUtil.hardwareSupportsRectRenderTexture)
{
return(null);
}
Texture texture = target as Texture;
if (IsCubemap())
{
return(m_CubemapPreview.RenderStaticPreview(texture, width, height));
}
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite && textureImporter.spriteImportMode == SpriteImportMode.Polygon)
{
// If the texture importer is a Sprite of primitive, use the sprite inspector for generating preview/icon.
if (subAssets.Length > 0)
{
Sprite sprite = subAssets[0] as Sprite;
if (sprite)
{
return(SpriteInspector.BuildPreviewTexture(width, height, sprite, null, true));
}
}
else
{
return(null);
}
}
PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height);
RenderTexture savedRT = RenderTexture.active;
Rect savedViewport = ShaderUtil.rawViewportRect;
RenderTexture tmp = RenderTexture.GetTemporary(
width, height,
0,
SystemInfo.GetGraphicsFormat(DefaultFormat.LDR));
Material mat = EditorGUI.GetMaterialForSpecialTexture(texture, null, QualitySettings.activeColorSpace == ColorSpace.Linear, false);
if (mat != null)
{
Graphics.Blit(texture, tmp, mat);
}
else
{
Graphics.Blit(texture, tmp);
}
RenderTexture.active = tmp;
Texture2D copy;
Texture2D tex2d = target as Texture2D;
if (tex2d != null && tex2d.alphaIsTransparency)
{
copy = new Texture2D(width, height, TextureFormat.RGBA32, false);
}
else
{
copy = new Texture2D(width, height, TextureFormat.RGB24, false);
}
copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);
copy.Apply();
RenderTexture.ReleaseTemporary(tmp);
EditorGUIUtility.SetRenderTextureNoViewport(savedRT);
ShaderUtil.rawViewportRect = savedViewport;
return(copy);
}