UnityEditor.CubemapPreview.RenderStaticPreview C# (CSharp) Method

RenderStaticPreview() public method

public RenderStaticPreview ( Texture t, int width, int height ) : Texture2D
t UnityEngine.Texture
width int
height int
return UnityEngine.Texture2D
        public Texture2D RenderStaticPreview(Texture t, int width, int height)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return null;
            }
            this.InitPreview();
            this.m_PreviewUtility.BeginStaticPreview(new Rect(0f, 0f, (float) width, (float) height));
            Vector2 previewDir = new Vector2(0f, 0f);
            InternalEditorUtility.SetCustomLighting(this.m_PreviewUtility.m_Light, new Color(0f, 0f, 0f, 0f));
            this.RenderCubemap(t, previewDir, 5.3f);
            InternalEditorUtility.RemoveCustomLighting();
            return this.m_PreviewUtility.EndStaticPreview();
        }

Usage Example

        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);
        }
All Usage Examples Of UnityEditor.CubemapPreview::RenderStaticPreview