unity 框选自由截屏与相机截屏

一、框选自由截屏

using UnityEngine;
using System.Collections;
using System.IO;
public class draw : MonoBehaviour
{
    public Color rColor = Color.green;
    private Vector3 start = Vector3.zero;
    private Vector3 end = Vector3.zero;
    private Material rMat = null;
    private bool drawFlag = false;
    private Rect rect;
    private Texture2D cutImage;
    // Use this for initialization  
    void Start()
    {
        rMat = new Material("Shader \"Lines/Colored Blended\" {" +
        "SubShader { Pass { " +
        "    Blend SrcAlpha OneMinusSrcAlpha " +
        "    ZWrite Off Cull Off Fog { Mode Off } " +
        "    BindChannels {" +
        "      Bind \"vertex\", vertex Bind \"color\", color }" +
        "} } }");//生成画线的材质  
        rMat.hideFlags = HideFlags.HideAndDontSave;
        rMat.shader.hideFlags = HideFlags.HideAndDontSave;
    }

    // Update is called once per frame  
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            drawFlag = true;//如果鼠标左键按下 设置开始画线标志  
            start = Input.mousePosition;//记录按下位置  
            end = Input.mousePosition;//鼠标当前位置  
        }
        else if (Input.GetMouseButtonUp(0))
        {
            drawFlag = false;//如果鼠标左键放开 结束画线  

        }
        if (Input.GetKeyDown(KeyCode.P))
        {
            StartCoroutine(CutImage());
        }
    }
    //绘制框选  
    void OnPostRender()
    {//画线这种操作推荐在OnPostRender()里进行 而不是直接放在Update,所以需要标志来开启  
        if (drawFlag)
        {
            end = Input.mousePosition;//鼠标当前位置  
        }
        GL.PushMatrix();//保存摄像机变换矩阵  

        if (!rMat)
            return;

        rMat.SetPass(0);
        GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
        GL.Begin(GL.QUADS);
        GL.Color(new Color(rColor.r, rColor.g, rColor.b, 0.1f));//设置颜色和透明度,方框内部透明  
        GL.Vertex3(start.x, start.y, 0);
        GL.Vertex3(end.x, start.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.Vertex3(start.x, end.y, 0);
        GL.End();
        GL.Begin(GL.LINES);
        GL.Color(rColor);//设置方框的边框颜色 边框不透明  
        GL.Vertex3(start.x, start.y, 0);
        GL.Vertex3(end.x, start.y, 0);
        GL.Vertex3(end.x, start.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.Vertex3(start.x, end.y, 0);
        GL.Vertex3(start.x, end.y, 0);
        GL.Vertex3(start.x, start.y, 0);
        GL.End();
        GL.PopMatrix();//恢复摄像机投影矩阵  

    }

    IEnumerator CutImage()
    {
        string date = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        //图片大小    
        cutImage = new Texture2D((int)(end.x - start.x), (int)(start.y - end.y), TextureFormat.RGB24, true);
        //坐标左下角为0    
        rect = new Rect((int)start.x, Screen.height - (int)(Screen.height - end.y), (int)(end.x - start.x), (int)(start.y - end.y));

        yield return new WaitForEndOfFrame();
        cutImage.ReadPixels(rect, 0, 0, true);

        cutImage.Apply();
        yield return cutImage;
        byte[] byt = cutImage.EncodeToPNG();
        //保存截图    
        File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage" + date + ".png", byt);
    }
}

需要说明一下的是,框选的时候填充的有颜色,这样截图的时候,填充的颜色也带进去了。可以把

GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
GL.Begin(GL.QUADS);
GL.Color(new Color(rColor.r, rColor.g, rColor.b, 0.1f));//设置颜色和透明度,方框内部透明  
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();

这段去掉,或者

GL.Color( new Color(rColor.r, rColor.g, rColor.b, 0.1f) );  

二、相机截屏

    /// <summary>
    /// 相机截图
    /// </summary>
    /// <param name="camera">截屏相机</param>
    /// <param name="rect">截屏区域</param>
    /// <returns></returns>
    public Texture2D CaptureCamera(Camera camera,Rect rect)
    {
        RenderTexture rt = new RenderTexture((int)rect.width,(int)rect.height,0);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width,(int)rect.height);
        screenShot.ReadPixels(rect,0,0);
        screenShot.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        return screenShot;
    }
    /// <summary>
    /// 设置截屏UI所需材质的值
    /// </summary>
    /// <param name="texture"></param>
    public void SetMaterialValue(Texture texture)
    {
        ri.material.SetTexture("_MainTex",texture);
        ri.color = new Color(ri.color.r, ri.color.g, ri.color.b, 1);
        ri.gameObject.SetActive(true);
        blurValue = 0;
    }

    //使用
    SetMaterialValue(CaptureCamera(Camera.main,new Rect(0,0,800,600)));
    

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值