Unity3D随意截图并保存

效果


代码

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


public class CropPicture : MonoBehaviour
{

    string localPath = "http://192.168.1.100:8080/picture/15.jpg";
    Texture2D image;
    Texture2D cutImage;
    WWW www;
    Rect rect;
    float time;
    Vector2 pos1;
    Vector2 pos2;
    // Use this for initialization
    void Start()
    {

        StartCoroutine(LoadImage());
    }

    // Update is called once per frame
    void Update()
    {
        //点击鼠标左键,记录第一个位置
        if (Input.GetMouseButtonDown(0))
        {
            pos1 = Input.mousePosition;
            time = Time.time;
            if (time > 1f)
            {

                Debug.Log(pos1);
            }
        }
        //放开左键记录第二个位置
        if (Input.GetMouseButtonUp(0))
        {
            pos2 = Input.mousePosition;
            Debug.Log(pos2);
            StartCoroutine(CutImage());
            time = 0;
        }
    }

    void OnGUI()
    {
        //当下载完成
        if (www.isDone)
        {
            GUI.DrawTexture(new Rect(0, 0, 600, 904), image);
        }

        GUI.Button(new Rect(0, 0, 100, 50), "W" + Screen.width + "H" + Screen.height);

        if (pos1 != null)
        {
            GUI.Button(new Rect(0, 50, 150, 50), pos1.ToString());
        }

        if (pos2 != null)
        {
            GUI.Button(new Rect(0, 100, 150, 50), pos2.ToString());
        }

        if (cutImage != null)
        {
            GUI.Button(new Rect(0, 150, 150, 50), "image W" + cutImage.width + "H" + cutImage.height);
        }

        if (rect != null)
        {
            GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());
        }
    }

    //下载图片
    IEnumerator LoadImage()
    {
        www = new WWW(localPath);

        yield return www;
        image = www.texture;
        if (www.error != null)
        {
            Debug.Log(www.error);
        }
    }

    //截图
    IEnumerator CutImage()
    {


        //图片大小
        cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);


        //坐标左下角为0
        rect = new Rect((int)pos1.x, Screen.height - (int)(Screen.height - pos2.y), (int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y));

        yield return new WaitForEndOfFrame();
        cutImage.ReadPixels(rect, 0, 0, true); 
        
        cutImage.Apply();
        yield return cutImage;
        byte[] byt = cutImage.EncodeToPNG();
        //保存截图Project面板下要创建StreamingAssets文件夹,保存文件后要刷新Project面板图片才会显示出来
        File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage.png", byt);

    }
}


 

2016.5.7.17.34修改后的代码

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


public class CropPicture : MonoBehaviour
{

    string localPath = "http://192.168.1.100:8080/picture/15.jpg";
    Texture2D image;
    Texture2D cutImage;
    WWW www;
    Rect rect;
    float time;
    Vector2 pos1;
    Vector2 pos2;
    // Use this for initialization
    void Start()
    {

        StartCoroutine(LoadImage());
    }

    // Update is called once per frame
    void Update()
    {
        //点击鼠标左键,记录第一个位置
        if (Input.GetMouseButtonDown(0))
        {
            pos1 = Input.mousePosition;
            time = Time.time;
            if (time > 1f)
            {

                Debug.Log(pos1);
            }
        }
        //放开左键记录第二个位置
        if (Input.GetMouseButtonUp(0))
        {
            pos2 = Input.mousePosition;
            Debug.Log(pos2);
            StartCoroutine(CutImage());
            time = 0;
        }
    }

    void OnGUI()
    {
        //当下载完成
        if (www.isDone)
        {
            GUI.DrawTexture(new Rect(0, 0, 600, 904), image);
        }

        GUI.Button(new Rect(0, 0, 100, 50), "W" + Screen.width + "H" + Screen.height);

        if (pos1 != null)
        {
            GUI.Button(new Rect(0, 50, 150, 50), pos1.ToString());
        }

        if (pos2 != null)
        {
            GUI.Button(new Rect(0, 100, 150, 50), pos2.ToString());
        }

        if (cutImage != null)
        {
            GUI.Button(new Rect(0, 150, 150, 50), "image W" + cutImage.width + "H" + cutImage.height);
        }

        if (rect != null)
        {
            GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());
        }
    }

    //下载图片
    IEnumerator LoadImage()
    {
        www = new WWW(localPath);

        yield return www;
        image = www.texture;
        if (www.error != null)
        {
            Debug.Log(www.error);
        }
    }

    //截图
    IEnumerator CutImage()
    {


        //图片大小
        cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);


        //坐标左下角为0
        rect = new Rect((int)pos1.x, Screen.height - (int)(Screen.height - pos2.y), (int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y));

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

        cutImage.Apply();
        yield return cutImage;
        byte[] byt = cutImage.EncodeToPNG();
        //保存截图
        File.WriteAllBytes(Application.dataPath+ "/"+DateTime.Now.ToFileTime().ToString()+".png", byt);

    }
}




  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地狱为王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值