[Unity基础]RenderTexture
参考链接:
https://www.cnblogs.com/Jimm/p/5951362.html
一.相关API
1.Texture2D.ReadPixels
从RenderTexture.active中复制像素,以左下角为原点。
2.MonoBehaviour.OnPostRender
当相机渲染完所有物体就会调用该方法,并且只有当这个脚本挂在相机时才会调用。
二.测试
新建一个场景,新建一个camera和一个go,把go设置为一个单独的层,让camera单独照这个层,主camera不要照这个层。调整camera的深度和targetTexture。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO; public class TestRenderTexture : MonoBehaviour { public Camera camera;
public RawImage rawImage;
bool isScreenShot = false; void Update()
{
//将镜头的画面保存到本地
if (Input.GetKeyDown(KeyCode.Q))
{
Texture2D tex = RenderTexture2Texture2D(camera.targetTexture); //展示
rawImage.texture = tex; //保存到本地
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/RenderTexture/Test.png", bytes);
UnityEditor.AssetDatabase.Refresh();
} //屏幕截图(方法1)
if (Input.GetKeyDown(KeyCode.W))
{
StartCoroutine(ScreenShotIEnumerator());
} //屏幕截图(方法2)
if (Input.GetKeyDown(KeyCode.E))
{
isScreenShot = true;
}
} public IEnumerator ScreenShotIEnumerator()
{
yield return new WaitForEndOfFrame();
rawImage.texture = ScreenShot();
} private void OnPostRender()
{
//Debug.Log("OnPostRender");
if (isScreenShot)
{
isScreenShot = false;
rawImage.texture = ScreenShot();
}
} //屏幕截图
public Texture2D ScreenShot()
{
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
tex.ReadPixels(new Rect(, , Screen.width, Screen.height), , );
tex.Apply();
return tex;
} //RenderTexture转Texture2D
public Texture2D RenderTexture2Texture2D(RenderTexture rt)
{
RenderTexture preRT = RenderTexture.active;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
tex.ReadPixels(new Rect(, , rt.width, rt.height), , );
tex.Apply();
RenderTexture.active = preRT;
return tex;
}
}
[Unity基础]RenderTexture的更多相关文章
- Unity基础6 Shadow Map 阴影实现
这篇实现来的有点墨迹,前前后后折腾零碎的时间折腾了半个月才才实现一个基本的shadow map流程,只能说是对原理理解更深刻一些,但离实际应用估计还需要做很多优化.这篇文章大致分析下shadow ma ...
- unity 基础之InputManager
unity 基础之InputManager 说一下unity中的InputManager,先截个图 其中Axes指的是有几个轴向!Size指的是有几个轴,改变Size可以添加或者减少轴! Name指 ...
- unity 基础学习 transform
unity 基础学习 transform 1.unity采用的是右手坐标系,X轴右手为+,Y轴向上为+,Z轴朝里为+; 但是我们从3D MAX中导入模型之后,发现轴向并没有遵从这个原理, 其实是 ...
- Unity 基础
Unity 基础是unity入门的关键.他将讲解Unity的界面, 菜单项,使用资源,创设场景,并发布版本. 当你读完这段,你将理解unity是怎么工作的,如何有效地使用它,并且完成一个基本的游戏. ...
- NET笔记——IOC详解和Unity基础使用介绍
说起IOC,可能很多初学者不知道是用来做什么的,今天正好有点时间,就来扫扫盲,顺便巩固下自己. IOC全称是Inversion Of Control,意为控制反转(这些自然百度也有),可什么是控制反转 ...
- IOC详解和Unity基础使用介绍
说起IOC,可能很多初学者不知道是用来做什么的,今天正好有点时间,就来扫扫盲,顺便巩固下自己. IOC全称是Inversion Of Control,意为控制反转(这些自然百度也有),可什么是控制反转 ...
- unity基础命令
获取所挂脚本元素的组件: rd = GetComponent<Rigidbody>(); 获取其他元素的组件: rd = GameObject.Find("Player" ...
- 【Unity Shader学习笔记】Unity基础纹理-法线贴图
1 高度纹理 使用一张纹理改变物体表面法线,为模型提供更多细节. 有两种主要方法: 1.高度映射:使用一张高度纹理(height map)来模拟表面位移(displacement).得到一个修改后的法 ...
- Unity基础知识学习笔记二
1,object Instantiate(object original,Vector3 position,Quaternion rotation) 克隆原始物体,并返回克隆物体. ...
随机推荐
- IDEA中使用springBoot+gradle构建多模块项目
https://blog.csdn.net/forMelo/article/details/78995875
- KC705开发板关于MIG的配置
KC705开发板关于MIG的配置
- NAND FLASH控制器
一.nand flash访问原理 地址空间概念 nand的编址 nand命令 命令,地址,数据 使用S3C2440的nand flash控制器访问nand flash 前几个编译出来的文件都小于4k ...
- linux 查看系统磁盘、内存大小
1.磁盘 df -h cat /proc/partitions 2.内存 cat /proc/meminfo cat /proc/meminfo
- Winfrom控件使用
1.Lablelable添加图片,解决图片和字体重叠? Text属性添加足够空格即可,显示效果如下所示: 2.根据窗体名称获取窗体并显示到指定panel? Label item = sender as ...
- ALGO-123_蓝桥杯_算法训练_A+B problem
问题描述 Given two integers A and B, your task is to output their sum, A+B. 输入格式 The input contains of o ...
- LeetCode——3. Longest Substring Without Repeating Characters
一.题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters 二.题目大意: 给定一个字符串, ...
- HTTP/HLS/RTMP超级负载测试工具
这个负载测试工具是网游分享的工具,可以在http://blog.csdn.net/win_lin/article/details/11835011 或者https://github.com/winli ...
- QT中实现应用程序的单例化
一介绍 通过编写一个QSingleApplication类,来实现Qt程序的单例化,原文的作者是在Windows Vista + Qt4.4 下实现的,不过应用在其他平台上是没问题的.(本文是我在ht ...
- 51nod 1132 覆盖数字的数量 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1132 题意是给定a,b,l,r求[l,r]内有几个整数可以表示成ax+b ...