Unity C# 获取 并 导出 天空盒(six-sided)的六张图片

var FrontTexture = (Texture2D)SkyBox.GetTexture("_FrontTex");
exportTexture(FrontTexture, Path.Combine(path,"front.png"));

var BackTexture = (Texture2D)SkyBox.GetTexture("_BackTex");
exportTexture(BackTexture, Path.Combine(path, "back.png"));
            
var LeftTexture = (Texture2D)SkyBox.GetTexture("_LeftTex");           
exportTexture(LeftTexture, Path.Combine(path, "left.png"));
            
var RightTexture = (Texture2D)SkyBox.GetTexture("_RightTex");            
exportTexture(RightTexture, Path.Combine(path, "right.png"));
            
var UpTexture = (Texture2D)SkyBox.GetTexture("_UpTex");            
exportTexture(UpTexture, Path.Combine(path, "up.png"));
            
var DownTexture = (Texture2D)SkyBox.GetTexture("_DownTex");            
exportTexture(DownTexture, Path.Combine(path, "down.png"));
private static void exportTexture(Texture2D texture, string outputPath)
    {
        //这些操作主要是避免readable的问题
        var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);

        Graphics.Blit(texture, destRenderTexture);

        var exportTexture = new Texture2D(texture.width, texture.height);
        exportTexture.ReadPixels(new Rect(0, 0, destRenderTexture.width, destRenderTexture.height), 0, 0);
        exportTexture.Apply();

        File.WriteAllBytes(outputPath, exportTexture.EncodeToPNG());

        RenderTexture.ReleaseTemporary(destRenderTexture);
    }

右键获取 Six Sided 的 shader 属性  

 获得 _FrontTex _BackTex _LeftTex _RightTex _UpTex _DownTex

 关于 SkyBoxGetTexture 函数.

 RenderTexture.GetTemporary 分配临时渲染纹理

 RenderTextureFormat --> 颜色渲染纹理

 RenderTextureReadWrite  --> 渲染纹理包括颜色数据 实现线性到颜色的转换

 Graphics.Blit

using UnityEngine;

public class Example : MonoBehaviour
{ // 复制 aTexture 到 rTex and displays it in all cameras.

    Texture aTexture;
    RenderTexture rTex;

    void Start()
    {
        if (!aTexture || !rTex)
        {
            Debug.LogError("A texture or a render texture are missing, assign them.");
        }
    }

    void Update()
    {
        Graphics.Blit(aTexture, rTex);
    }

 Texture2D.ReadPixels

// Attach this script to a Camera
//Also attach a GameObject that has a Renderer (e.g. a cube) in the Display field
//Press the space key in Play mode to capture

using UnityEngine;

public class Example : MonoBehaviour
{
    // Grab the camera's view when this variable is true.
    bool grab;

    // The "m_Display" is the GameObject whose Texture will be set to the captured image.
    public Renderer m_Display;

    private void Update()
    {
        //Press space to start the screen grab
        if (Input.GetKeyDown(KeyCode.Space))
            grab = true;
    }

    private void OnPostRender()
    {
        if (grab)
        {
            //Create a new texture with the width and height of the screen
            Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
            //Read the pixels in the Rect starting at 0,0 and ending at the screen's width and height
            texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
            texture.Apply();
            //Check that the display field has been assigned in the Inspector
            if (m_Display != null)
                //Give your GameObject with the renderer this texture
                m_Display.material.mainTexture = texture;
            //Reset the grab state
            grab = false;
        }
    }
}

 Texture2D.Apply 

File.WriteAllBytes

 

 

仅供自己学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值