其他分享
首页 > 其他分享> > Unity 任意区域截图

Unity 任意区域截图

作者:互联网

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

 

标签:截图,screenShot,filePath,height,width,Unity,任意,rect,rectT
来源: https://www.cnblogs.com/dj1232090/p/14828792.html