Untiy截图更新,及截取透明图片签字

一、截图

之前写了一版截图的细节,当时用的是unity5.x的版本,当前用的是unity2018了,如有疑问,及时评论,看到回复。
之前的版本的链接:
Unity3d截图方法合集

现在做了一些更新:

1.1 方法不需要写到onPostRender里

  写成协同,直接调用即可
 代码如下:




/// 
    /// 2.区域截屏
    /// 
    IEnumerator CaptureAreaScreenshot(Rect rect)
    {
        yield return new WaitForEndOfFrame();

        // 先创建一个的空纹理,大小可根据实现需要来设置
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);

        // 读取屏幕像素信息并存储为纹理数据,
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();

        // 然后将这些纹理数据,成一个png图片文件
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/ScreenshotArea" + System.DateTime.Now.Minute.ToString() + ".png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("截屏了一张图片: {0}", filename));

        // 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
        //return screenShot;
    }

    /// 
    /// 3.对相机截图。 
    /// 
    IEnumerator CaptureCamera(Camera camera, Rect rect)
    {
        yield return new WaitForEndOfFrame();

        // 创建一个RenderTexture对象
        RenderTexture rt = new RenderTexture((int)Screen.width, (int)Screen.height, 0);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
        camera.targetTexture = rt;
        camera.Render();

        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();

        // 重置相关参数,以使用camera继续在屏幕上显示
        camera.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors
        GameObject.Destroy(rt);

        // 最后将这些纹理数据,成一个png图片文件
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/ScreenshotCam" + System.DateTime.Now.Minute.ToString() + ".png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("截屏了一张照片: {0}", filename));

        //及时垃圾清理
        System.GC.Collect();
        //return screenShot;
    }

1.2 unity自带的方法有了变动

差别如下:

/// 
    /// 1.unity自带的截全屏
    /// 
    void CaptureFullScreen()
    {
        //旧版的用法
        //Application.CaptureScreenshot("ScreenShot" + System.DateTime.Now.Minute + ".png", 2);

        //更新后的用法
        ScreenCapture.CaptureScreenshot(Application.dataPath + "/ScreenshotFull" + System.DateTime.Now.Minute + ".png", 2);

    }

1.3整合

图像如图:
Untiy截图更新,及截取透明图片签字_第1张图片

三种截图方法对应的结果如图:
全屏:
Untiy截图更新,及截取透明图片签字_第2张图片
区域截屏:
Untiy截图更新,及截取透明图片签字_第3张图片
相机截屏:
Untiy截图更新,及截取透明图片签字_第4张图片

二、写字截屏,截取字体,贴到模拟的合同上

2.1 断笔写字

这个断笔写字,之前有写过一篇博客,可以参考:

Unity使用LineRender断笔写字

2.2截取透明图

在之前截取相机的基础上,设置相机的透明度,截图添加透明通道RGBA
代码如下:

#region 截取透明图
    /// 
    /// 3.对相机截图。 
    /// 
    IEnumerator CaptureAlphaCamera(Camera camera, Rect rect)
    {
        yield return new WaitForEndOfFrame();

        // 创建一个RenderTexture对象
        RenderTexture rt = new RenderTexture((int)Screen.width, (int)Screen.height, 0);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
        camera.targetTexture = rt;
        camera.Render();

        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();

        // 重置相关参数,以使用camera继续在屏幕上显示
        camera.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors
        GameObject.Destroy(rt);

        // 最后将这些纹理数据,成一个png图片文件
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/Screenshot.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("截屏了一张照片: {0}", filename));

        //及时垃圾清理
        System.GC.Collect();
        //return screenShot;
    }
    #endregion

最后,加载透明贴图:
加载方式如下:

 IEnumerator Pic_Download()
    {
        WWW www = new WWW(Application.dataPath + "/Screenshot.png");
        yield return www;
        var tex2d = new Texture2D(Screen.width, Screen.height);
        www.LoadImageIntoTexture(tex2d);
        var spr = Sprite.Create(tex2d, new Rect(0, 0,Screen.width , Screen.height), Vector2.zero);

        sign.sprite = spr;


    }

演示看下动画:
Untiy截图更新,及截取透明图片签字_第5张图片

可以看到单独写字的部分被截图,并贴到了网上搜的一个合同的甲方上。

看些整个代码结构:
Untiy截图更新,及截取透明图片签字_第6张图片
整个工程包下载

你可能感兴趣的:(Unity3d)