小功能⭐️Unity截屏功能的实现

Posted 小星河丨U3D开发支持

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小功能⭐️Unity截屏功能的实现相关的知识,希望对你有一定的参考价值。

文章目录


以下笔者整理了Unity的3种截屏功能,分享给大家:

  • 截全屏
  • 屏幕指定范围截屏
  • 指定相机截屏

🟥 截全屏

使用方法:

将下方脚本挂载到物体上,Skode_StartCapture为截图方法。action_End 委托方法为截图完后执行。

using UnityEngine;
using System.Collections;
using System.IO;
using System;
 
public class Skode_ScreenCapture_Full : MonoBehaviour

    [Tooltip("是否启用截图后将图片保存到下面路径的功能")]
    public bool saveUseable = false;
 
    [Tooltip("存储路径")]
    public string savePath = "/sdcard/Skode";
 
    public GameObject[] shouldHideObj;
 
    /// <summary>
    /// 截图后的sprite
    /// </summary>
    [HideInInspector]
    public Sprite sprite;
 
    public void Skode_StartCapture(Action action_End = null)
    
        CaptureStart();
        StartCoroutine(OnScreenShot(action_End));
    
 
    public IEnumerator OnScreenShot(Action action_End = null)
    
        //命名图片  202002261416527077
        string currentTime = string.Format("0:yyyyMMddHHmmssffff", DateTime.Now);
 
        //截屏
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        yield return new WaitForEndOfFrame();
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
 
        sprite = Sprite.Create(texture,
            new Rect(0, 0, texture.width, texture.height),
            new Vector2(0.5f, 0.5f));
 
        if (saveUseable)
        
            byte[] bytes = texture.EncodeToPNG();
 
            //若没路径 创建
            if (!Directory.Exists((savePath)))
            
                Directory.CreateDirectory(savePath);
            
            //保存路径
            string path_save = savePath + "/IMG_" + currentTime;
            //写入图片
            File.WriteAllBytes(path_save, bytes);
        
 
        CaptureOver();
        action_End?.Invoke();
    
 
    void CaptureStart()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(false);
            
        
    
 
    void CaptureOver()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(true);
            
        
    



🟧 屏幕指定范围截图

使用方法:

将下方脚本挂载到物体上,Skode_StartCapture为截图方法。action_End 委托方法为截图完后执行。

using System;
using System.Collections;
using System.IO;
using UnityEngine;
 
public class Skode_ScreenCapture_Rect : MonoBehaviour

    [Tooltip("是否启用截图后将图片保存到下面路径的功能")]
    public bool saveUseable = false;
 
    [Tooltip("存储路径")]
    public string savePath = "/sdcard/Skode";
 
    public GameObject[] shouldHideObj;
 
    /// <summary>
    /// 截图后的sprite
    /// </summary>
    [HideInInspector]
    public Sprite sprite;
 
    public void Skode_StartCapture(Rect mRect, Action action_End = null)
    
        CaptureStart();
        StartCoroutine(CaptureByRect(mRect, action_End));
    
 
    /// <summary>
    /// 根据一个Rect类型来截取指定范围的屏幕, 左下角为(0,0)
    /// 例:StartCoroutine(CaptureByRect(new Rect(100, 150, 1024, 768)));
    /// 从左下角的横坐标100,纵坐标150坐标开始截图,宽度1024,高度768
    /// </summary>
    public IEnumerator CaptureByRect(Rect mRect, Action action_End = null)
    
        //等待渲染线程结束
        yield return new WaitForEndOfFrame();
 
        string currentTime = string.Format("0:yyyyMMddHHmmssffff", DateTime.Now);
 
        //初始化Texture2D, 大小可以根据需求更改
        Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height,
                                 TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据
        mTexture.ReadPixels(mRect, 0, 0);
        //应用
        mTexture.Apply();
 
        sprite = Sprite.Create(mTexture,
    new Rect(0, 0, mTexture.width, mTexture.height),
    new Vector2(0.5f, 0.5f));
 
        if (saveUseable)
        
            byte[] bytes = mTexture.EncodeToPNG();
 
            //若没路径 创建
            if (!Directory.Exists((savePath)))
            
                Directory.CreateDirectory(savePath);
            
            //保存路径
            string path_save = savePath + "/IMG_" + currentTime;
            //写入图片
            File.WriteAllBytes(path_save, bytes);
        
 
        CaptureOver();
        action_End?.Invoke();
    
 
    void CaptureStart()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(false);
            
        
    
 
    void CaptureOver()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(true);
            
        
    



🟨 指定相机截图

使用方法:

将下方脚本挂载到物体上,Skode_StartCapture为截图方法。action_End 委托方法为截图完后执行。

using System;
using System.Collections;
using System.IO;
using UnityEngine;
 
public class Skode_ScreenCapture_Rect : MonoBehaviour

    [Tooltip("是否启用截图后将图片保存到下面路径的功能")]
    public bool saveUseable = false;
 
    [Tooltip("存储路径")]
    public string savePath = "/sdcard/Skode";
 
    public GameObject[] shouldHideObj;
 
    /// <summary>
    /// 截图后的sprite
    /// </summary>
    [HideInInspector]
    public Sprite sprite;
 
    public void Skode_StartCapture(Rect mRect, Action action_End = null)
    
        CaptureStart();
        StartCoroutine(CaptureByRect(mRect, action_End));
    
 
    /// <summary>
    /// 根据一个Rect类型来截取指定范围的屏幕, 左下角为(0,0)
    /// 例:StartCoroutine(CaptureByRect(new Rect(100, 150, 1024, 768)));
    /// 从左下角的横坐标100,纵坐标150坐标开始截图,宽度1024,高度768
    /// </summary>
    public IEnumerator CaptureByRect(Rect mRect, Action action_End = null)
    
        //等待渲染线程结束
        yield return new WaitForEndOfFrame();
 
        string currentTime = string.Format("0:yyyyMMddHHmmssffff", DateTime.Now);
 
        //初始化Texture2D, 大小可以根据需求更改
        Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height,
                                 TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据
        mTexture.ReadPixels(mRect, 0, 0);
        //应用
        mTexture.Apply();
 
        sprite = Sprite.Create(mTexture,
    new Rect(0, 0, mTexture.width, mTexture.height),
    new Vector2(0.5f, 0.5f));
 
        if (saveUseable)
        
            byte[] bytes = mTexture.EncodeToPNG();
 
            //若没路径 创建
            if (!Directory.Exists((savePath)))
            
                Directory.CreateDirectory(savePath);
            
            //保存路径
            string path_save = savePath + "/IMG_" + currentTime;
            //写入图片
            File.WriteAllBytes(path_save, bytes);
        
 
        CaptureOver();
        action_End?.Invoke();
    
 
    void CaptureStart()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(false);
            
        
    
 
    void CaptureOver()
    
        if (shouldHideObj.Length > 0)
        
            for (int i = 0; i < shouldHideObj.Length; i++)
            
                shouldHideObj[i].SetActive(true);
            
        
    





大家还有什么问题,欢迎在下方留言!



如果你有 技术的问题 项目开发

都可以加下方联系方式

和我聊一聊你的故事🧡

以上是关于小功能⭐️Unity截屏功能的实现的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D 灵巧小知识点☀️ | Unity 屏幕坐标 和 世界坐标 之间相互转换

100个 Unity实用技能☀️ | Unity InputSystem中拿到触摸屏幕的坐标,鼠标的坐标等

Unity3D 灵巧小知识点☀️ | Unity通过 射线 获取 鼠标的世界坐标 和 鼠标点击的物体信息

Unity UGUI 实现简单拖拽功能

小功能⭐️Unity单例模式

小功能⭐️Unity单例模式