1.下面是实现代码

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine; public class SaveJPG : MonoBehaviour
{
public GameObject[] gameObjects;
//string fileName = Application.streamingAssetsPath + "/UI/photo.jpg";
public Camera camera;
public Transform fuwutiParent;
public int jsq = 0;
public bool zx = false;
GameObject obj = null; private void Start()
{
//自动保存图片
//StartCoroutine(CloneTuPian());
}
private void Update()
{
#region 半自动保存图片手动对位置
if (Input.GetKeyDown (KeyCode.Space))
{
for (int i = 0; i < fuwutiParent.childCount; i++)
{
fuwutiParent.GetChild(jsq).gameObject.SetActive(true);
} }
if (Input.GetKeyDown (KeyCode.Alpha1))
{
string str_ = fuwutiParent.GetChild(jsq).name.Split('(')[0].ToString();
CaptureScreenByRT(str_);
fuwutiParent.GetChild(jsq).gameObject.SetActive(false);
jsq++;
Debug.Log(jsq);
}
#endregion
} IEnumerator CloneTuPian()
{
while (jsq<gameObjects .Length)
{
GameObject obj = Instantiate(gameObjects[jsq]);
string str_ = obj.transform.name.Split('(')[0].ToString ();
CaptureScreenByRT(str_);
Destroy(obj);
yield return new WaitForSeconds(1f);
jsq++;
}
Debug.Log("生成结束");
}
public void CaptureScreenByRT(string fileName)
{
Rect rect = new Rect(0, 0, 600, 600);
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string path = Application.streamingAssetsPath + "/UI/"+ fileName + ".jpg";
System.IO.File.WriteAllBytes(path, bytes);
AssetDatabase.Refresh();
} }

直接使用即可

最新文章

  1. 用SignalR 2.0开发客服系统[系列2:实现聊天室]
  2. openseadragon.js与deep zoom java实现艺术品图片展示
  3. 关于高负载服务器Kernel的TCP参数优化
  4. ActiveMQ_点对点队列(二)
  5. 简单的cookie使用
  6. ACM: A Simple Problem with Integers 解题报告-线段树
  7. .使用 HTML+CSS 实现如图布局,border-widht 5px,一个格子大小是 50*50,hover时候边框变为红色(兼容IE6+,考虑语义化的结构)
  8. 在eclipse导入项目的步骤【转】
  9. App右上角数字
  10. C++学习17派生类的构造函数
  11. 使vim中Syntastic支持C++11
  12. Windows命令查看文件MD5码
  13. C++后台实践:古老的CGI与Web开发
  14. Java并发编程——BlockingQueue
  15. 【Spark篇】---Spark中广播变量和累加器
  16. 解决js的 Math取正弦值 余弦值不准确的问题
  17. ORA-01000
  18. Redis内存分析方法
  19. PyQt5--QCalendar
  20. 原创:Eclipse安装Eclipse Color Themes插件后,编辑器背景颜色被改变

热门文章

  1. Unity之GPS定位(腾讯sdk)
  2. 12月15日内容总结——ORM执行原生SQL语句、双下划线数据查询、ORM外键字段的创建、外键字段的相关操作、ORM跨表查询、基于对象的跨表查询、基于双下划线的跨表查询、进阶查询操作
  3. 9月21日内容总结——计算机基础知识、typora软件的安装与软件内的部分markdown语法
  4. Kubernetes(k8s)控制器(四):ReplicaSet
  5. QT 5 中文乱码,试试在PRO文件加入这几行代码
  6. Linux xsync命令脚本
  7. flutter系列之:使用SliverList和SliverGird
  8. Vue的指令(内容渲染、属性绑定、javaScript表达式、事件绑定、事务对象、双向绑定、逻辑&lt;if-show-for&gt;)
  9. idea+git+gitee的使用
  10. B端业务中仓库标签打印系统设计方案