Unity之将Texture保存成png
2016-04-18 10:07
585 查看
using UnityEngine; using System.Collections; using System.IO; public class SaveToPng : MonoBehaviour { public Shader outShader; public Texture inputTex; // Use this for initialization void Start () { SaveRenderTextureToPNG(inputTex,outShader,Application.dataPath+"/temp","ok.png"); } public bool SaveRenderTextureToPNG(Texture inputTex,Shader outputShader, string contents, string pngName) { RenderTexture temp = RenderTexture.GetTemporary(inputTex.width, inputTex.height, 0, RenderTextureFormat.ARGB32); Material mat = new Material(outputShader); Graphics.Blit(inputTex, temp, mat); bool ret = SaveRenderTextureToPNG(temp, contents,pngName); RenderTexture.ReleaseTemporary(temp); return ret; } //将RenderTexture保存成一张png图片 public bool SaveRenderTextureToPNG(RenderTexture rt,string contents, string pngName) { RenderTexture prev = RenderTexture.active; RenderTexture.active = rt; Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false); png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); byte[] bytes = png.EncodeToPNG(); if (!Directory.Exists(contents)) Directory.CreateDirectory(contents); FileStream file = File.Open(contents + "/" + pngName + ".png", FileMode.Create); BinaryWriter writer = new BinaryWriter(file); writer.Write(bytes); file.Close(); Texture2D.DestroyImmediate(png); png = null; RenderTexture.active = prev; return true; } }
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- Unity UGUI教程之实现滑页效果
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- [软件资讯]Unity已支持将3D游戏导出成Flash
- ShaderLab: Pass
- Unity 武器拖尾效果
- unity常见问题之20题
- 反编译ARB program to GLSL shader日记
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- uLua中遇到的问题
- Unity3d 协程、调用函数、委托
- Unity3d摄像机Camera参数详解
- Unity3d多线程