unity RenderTexture小知识

为啥要为临时生成的图片添加名字?

方便查看内存的时候观察有没删除干净啊

 

缩放图片

    private Texture2D ScaleTexture(Texture2D target, int width, int height)
    {
        RenderTexture rt = new RenderTexture(width, height, 24);
        Graphics.Blit(target, rt);
        var result = new Texture2D(width, height, TextureFormat.ARGB32, false);
        result.name = "ScaleTextureResult";
        RenderTexture.active = rt;
        result.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        result.Apply();
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        rt = null;
        return result;
    }

 

获取shader的结果

    private Texture2D MatTexture(Material mat, int width, int height)
    {
        RenderTexture rt = new RenderTexture(width, height, 24);
        Graphics.Blit(null, rt, mat);
        var result = new Texture2D(width, height, TextureFormat.ARGB32, false);
        result.name = "MatTextureResult";
        RenderTexture.active = rt;
        result.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        result.Apply();
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        rt = null;
        return result;
    }

 

你可能感兴趣的:(unity3d,unity,RenderTexture,缩放图片,shader,Material)