美文网首页
在Unity中截图的操作

在Unity中截图的操作

作者: Moment__格调 | 来源:发表于2016-04-19 20:27 被阅读761次

这是一个脚本,模拟QQ截图

using UnityEngine;
using System.Collections;
using System.IO;

public class CapstureScreen : MonoBehaviour {

    string filePath;
    void Start () {
        filePath = Application.streamingAssetsPath + "/1.png";
    }
    Rect rect;
    Vector3 s_pos;
    Vector3 e_pos;
    bool isDraw;
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            s_pos = Input.mousePosition;
        }
        if(Input.GetMouseButton(0))
        {
            e_pos = Input.mousePosition;
            isDraw = true;
        }

        if(Input.GetMouseButtonUp(0))
        {
            isDraw = false;
            e_pos = Input.mousePosition;

            rect = new Rect(Mathf.Min(s_pos.x, e_pos.x),
                            Mathf.Min(s_pos.y, e_pos.y),
                            Mathf.Abs(s_pos.x - e_pos.x),
                            Mathf.Abs(s_pos.y - e_pos.y));
            StartCoroutine(Capsture(filePath, rect));
        }
    }


    IEnumerator Capsture(string filePath, Rect rect)
    {
        yield return new WaitForEndOfFrame();
        Texture2D tex = new Texture2D((int)rect.width, (int)rect.height);
        
        tex.ReadPixels(rect, 0, 0) ;
        tex.Apply();
        
        byte[] result = tex.EncodeToPNG();
        //文件夹
        if(!Directory.Exists(Application.streamingAssetsPath))
            Directory.CreateDirectory(Application.streamingAssetsPath);
        File.WriteAllBytes(filePath, result);
        print ("Finish");
    }

    //1.GL的回调函数
    //2.Material
    public Material lineMaterial;

    void OnPostRender() {
        if(!isDraw) return;
        print (s_pos);
        Vector3 sPos = Camera.main.ScreenToWorldPoint(s_pos + new Vector3(0, 0, 10));
        Vector3 ePos = Camera.main.ScreenToWorldPoint(e_pos + new Vector3(0, 0, 10));

        print(string.Format("GL.....{0},  {1}", sPos, ePos));
        // Set your materials Done
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        lineMaterial.SetPass(0);    //告诉GL使用该材质绘制
        // Draw your stuff
        //始终在最前面绘制
        GL.invertCulling = true;
        GL.Begin(GL.LINES);     //开始绘制

        GL.Vertex(sPos);
        GL.Vertex(ePos);
        //如果想要绘制,矩形,将下面代码启动
//      GL.Vertex(sPos);
//      GL.Vertex(new Vector3(ePos.x, sPos.y, 0));
//
//      GL.Vertex(new Vector3(ePos.x, sPos.y, 0));
//      GL.Vertex(ePos);
//
//      GL.Vertex(ePos);
//      GL.Vertex(new Vector3(sPos.x, ePos.y, 0));
//
//      GL.Vertex(new Vector3(sPos.x, ePos.y, 0));
//      GL.Vertex(sPos);
        GL.End();               //结束绘制

        GL.PopMatrix();
    }
    
    IEnumerator Capsture(string filePath)
    {
        yield return new WaitForEndOfFrame();
        Texture2D tex = new Texture2D(Screen.width, Screen.height);
        tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        tex.Apply();
        byte[] result = tex.EncodeToPNG();
        //文件夹
        if(!Directory.Exists(Application.streamingAssetsPath))
            Directory.CreateDirectory(Application.streamingAssetsPath);

        File.WriteAllBytes(filePath, result);
        print ("Finish");
    }
}

相关文章

网友评论

      本文标题:在Unity中截图的操作

      本文链接:https://www.haomeiwen.com/subject/qilzlttx.html