微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐