首页 > 其他分享 >Unity 保存截图功能

Unity 保存截图功能

时间:2022-11-21 17:46:00浏览次数:39  
标签:截图 fuwutiParent 保存 Unity jsq GetChild using public

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();
    }


}

 

直接使用即可

 

标签:截图,fuwutiParent,保存,Unity,jsq,GetChild,using,public
From: https://www.cnblogs.com/qq2351194611/p/16912109.html

相关文章

  • python爬取网页的多种方式以及保存方法
    爬取网页信息并保存bs4和lxml都是用来将接收的数据解析html1.bs4+excel(openpyxl):importrequestsfrombs4importBeautifulSoupfromopenpyxlimportWorkbookwb......
  • docker保存、导入、导出和加载tar及其tar.gz
    一、操作tar包1、save和load命令save命令dockersave[options]images[images...]示例:dockersave-onginx.tarnginx:latest或dockersave>nginx.tarnginx:la......
  • Compose 状态保存:rememberSaveable 原理分析
    ​前言我曾经在一篇​​介绍ComposeNavigation的文章​​中提到了Navigation的状态保存实际是由​​rememberSaveable​​实现的,有同学反馈希望单独介绍一下rememb......
  • DataTable保存CSV
    也是转载的,不知道哪个是原作者,原作者若看到请联系本人注明usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;u......
  • Unity Shader 4 ShaderLab
    通用基础的ShaderUnlit不发光的纹理VertexLit顶点光照Diffuse漫反射Normalmapped法线贴图,增加一个或更多纹理和几个着色器结构Specular镜面高光,模拟......
  • Unity性能优化汇总
    1.UI方面,UGUI的优化方式参考:Unity优化之UGUI(2017年版【一】) 2.C#方面的优化 3.网络同步方面的优化 4.CPU方面的优化降低DrawCall 5.GPU方面的优化不要......
  • php开发中实现客户端扫描文档并保存到服务器端
    项目中需要控制扫描仪器扫描文稿并作为OA系统的附件保存到服务器端,在网页中控制扫描仪使用的是ScanOnWeb控件,实现了客户端通过javascript控制扫描仪扫描文稿,多页文稿全部扫......
  • Unity Shader 2 编程语言
    Shader包括了HLSL基于DirectX的HightLevelShadingLanguageGLSL基于OpenGL的OpenGLShadingLanguageCGNVIDIA的CForGraphic简称CGUnity中提供了surfac......
  • Unity Shader 1 什么是Shader
    UnityShader1什么是Shader什么是Shader什么是渲染管线Shader、材质、贴图的关系1什么是Shader建一栋房子,结构、框架都有了,如何装饰装修呢,Shader就是一道道工序,......
  • Linux 命令行批处理图片,批量去除截图的无用部分
    我希望产生一批相同区域但不同内容的截图,如果用截图工具截取矩形区域,那么每次截取的位置和大小都没法控制,不能做到区域一样。如果希望每次的截图都一样,那么就只能是保持窗口......