码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 任意区域截图

时间:2021-06-02 17:50:21      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:write   etop   tar   unit   png   screens   direct   hmm   local   

IEnumerator getScreenTexture(RectTransform rectT)
    {
        yield return new WaitForEndOfFrame();
        Texture2D screenShot = new Texture2D((int)rectT.rect.width, (int)rectT.rect.height, TextureFormat.RGB24, true);
        float x = rectT.localPosition.x + (Screen.width - rectT.rect.width) / 2;
        float y = rectT.localPosition.y + (Screen.height - rectT.rect.height) / 2;
        Rect position = new Rect(x, y, rectT.rect.width, rectT.rect.height);
        screenShot.ReadPixels(position, 0, 0, true);//按照设定区域读取像素;注意是以左下角为原点读取
        screenShot.Apply();

        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
        string filePath = "";
        filePath = Application.persistentDataPath + "/HeadFold";
        string scrPathName = filePath + "/" + fileName;
        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
        }

        //二进制转换
        byte[] byt = screenShot.EncodeToPNG();
        File.WriteAllBytes(scrPathName, byt);
        System.Diagnostics.Process.Start(scrPathName);
    }

 

Unity 任意区域截图

标签:write   etop   tar   unit   png   screens   direct   hmm   local   

原文地址:https://www.cnblogs.com/dj1232090/p/14828792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!