轩辕kuku
论坛版主
论坛版主
  • UID14
  • 粉丝6
  • 关注1
  • 发帖数78
  • 社区居民
  • 原创写手
阅读:1658回复:0

Unity3d基础——拍照打水印方式一

楼主#
更多 发布于:2016-07-09 11:12


        Unity3d开发中拍照的方式有好几种——截屏,渲染Camera等,打水印的方式也是,有的可以控制UI的消隐来处理。下面介绍另外个人觉得比较好的方式。解决方式来自本站作者:kingdong020543。
只需将要渲染的Camera和水印挂上去即可。

public Camera arcamera;//需要渲染的摄像头
 
    public Texture2D _logo;//水印
 
    public void ScreenPicture()
    {
        Capture(arcamera, new Rect(0, 0, Screen.width, Screen.height));
    }
 
    Texture2D Capture(Camera camera, Rect rect)
    {
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 3);
        camera.targetTexture = rt;
        camera.Render();
 
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
 
        Texture2D _tex = _logo;
 
        screenShot.ReadPixels(rect, 0, 0);
 
        //合成水印和Camera渲染的图片
        for (int i = 0; i < _tex.width; i++)
        {
            for (int j = 0; j < _tex.height; j++)
            {
                if (_tex.GetPixel(i, j).a > 0.5f)
                {
                    screenShot.SetPixel((int)(i+rect.width-_tex.width), j, _tex.GetPixel(i, j));
                }
            }
        }
        screenShot.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.persistentDataPath + "/Screenshot.png";
        print(filename);
        System.IO.File.WriteAllBytes(filename, bytes);
 
        return screenShot;
    }

图片:68_14_a728a0831681809.jpg

欢迎分享

游客

返回顶部