unity 截图

 

 

using System.Collections;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;

public class ScreenShotTest : MonoBehaviour
{
    private string path = string.Empty;


    void Start()
    {
        //windows 不同平台各异   可参考  https://blog.csdn.net/linxinfa/article/details/51679528
        path = Application.dataPath+"/ScreenShot";
        if(!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    }


    private void OnGUI()
    {
        if(GUILayout.Button("截取全屏",GUILayout.Width(80), GUILayout.Width(200)))
        {
            if (!CanShoot) return;
            ScreenCapture.CaptureScreenshot(GetFileName());
            Refresh();
        }
        if (GUILayout.Button("截取主相机选取大小", GUILayout.Width(80), GUILayout.Width(200)))
        {
            if (!CanShoot) return;
            StartCoroutine(ShotByRect());           
        }
        GUILayout.Box(texture, GUILayout.Width(500), GUILayout.Height(500));

        if (GUILayout.Button("删除截图", GUILayout.Width(80), GUILayout.Width(200)))
        {
            DeleteShot();
        }
    }


    private bool _canShot = false;
    private float _lastShotTime = 0;
    private bool CanShoot
    {
        get {
            if (Time.time - _lastShotTime > 0.5f)
            {
                _canShot = true;
                _lastShotTime = Time.time;
            }else _canShot = false;
            return _canShot;
        }
    }


    private string lastFileName = string.Empty;
    /// <summary>
    /// 获取当前截图名
    /// </summary>
    /// <returns></returns>
    private string GetFileName()
    {
        var sTime = System.DateTime.Now;
        
        var _picName = AddEmpty(sTime.Day) + AddEmpty(sTime.Hour) + AddEmpty(sTime.Minute) + AddEmpty(sTime.Second) + AddEmpty(sTime.Millisecond, DefaultLenth.Three) + ".png";
        
        lastFileName = path + "/ScreenShot_" + _picName;
        return lastFileName;
    }

    /// <summary>
    /// 使截图名称,长度保持一致
    /// </summary>
    /// <param name="curvalue"></param>
    /// <param name="defaultLenth"></param>
    /// <returns></returns>
    private string AddEmpty(int curvalue, DefaultLenth defaultLenth = DefaultLenth.Two)
    {
        var txt = curvalue.ToString();
        int txt_length = txt.Length;
        if (defaultLenth == DefaultLenth.Two)
        {
            if (txt_length == 1) txt = "0" + txt;
        }else{
            if (txt_length == 1) txt = "00" + txt;
            else if (txt_length == 2) txt = "0" + txt;
        }
        return txt;
    }


    private void Refresh()
    {
        StartCoroutine(_Refresh());
    }


    private IEnumerator _Refresh()
    {
        yield return new WaitForSeconds(0.1f);//全屏截取存储等待,这里统一使用

        RefreshAsset();
        StartCoroutine(GetTexture());
    }

    /// <summary>
    /// 刷新截图目录
    /// </summary>
    private void RefreshAsset()
    {
#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
    }

    private Rect rect = new Rect(700, 300, 500, 500);
    /// <summary>
    /// 截取主相机设定大小
    /// </summary>
    /// <returns></returns>
    private IEnumerator ShotByRect()
    {
        yield return new WaitForEndOfFrame();

        Texture2D texture2D = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        texture2D.ReadPixels(rect, 0, 0);
        texture2D.Apply();

        File.WriteAllBytes(GetFileName(), texture2D.EncodeToPNG());
        Refresh();
    }


    private Texture texture;
    /// <summary>
    /// 读取当前截图
    /// </summary>
    /// <returns></returns>
    private IEnumerator GetTexture()
    {
        UnityWebRequest uwr = new UnityWebRequest("file://" + lastFileName);
        DownloadHandlerTexture handleTexture = new DownloadHandlerTexture(true);
        uwr.downloadHandler = handleTexture;
        yield return uwr.SendWebRequest();

        if(uwr.isDone)
        {
            texture = handleTexture.texture;
        }else{
            texture = null;
        }

        if (uwr.isNetworkError) Debug.Log("<color=red>  ==  NetworkError  ==   +</color>");
        else if (uwr.isHttpError) Debug.Log("<color=red>  ==  HttpError  ==   +</color>");
    }


    /// <summary>
    /// 删除截图
    /// </summary>
    private void DeleteShot()
    {
        if (!Directory.Exists(path)) return;

        var info = Directory.CreateDirectory(path);
        var files= info.GetFiles();

        for (int i = 0; i < files.Length; i++)
        {
            string filePath = path +"/"+ files[i].Name;
            File.Delete(filePath);
        }
        RefreshAsset();
    }


    enum DefaultLenth
    {
        Two,
        Three
    }
}
 

参考: https://blog.csdn.net/qinyuanpei/article/details/39185195 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值