unity实现按住鼠标选取区域截图

所属分类: 软件编程 / C#教程 阅读数: 110
收藏 0 赞 0 分享

本文实例为大家分享了unity按住鼠标选取区域截图的具体代码,供大家参考,具体内容如下

private int capBeginX;
private int capBeginY;
private int capFinishX;
private int capFinishY;
 
public Image showImg;
 
// Use this for initialization
void Start () {
    
  }
  
  // Update is called once per frame
  void Update () {
    if (Input.GetMouseButtonDown (0)) {
      Vector3 mousePos = Input.mousePosition;
      Vector2 beginPos = new Vector2 (mousePos.x, mousePos.y);
      capBeginX = (int)mousePos.x;
      capBeginY = (int)mousePos.y;
    }
 
    if (Input.GetMouseButtonUp (0)) {
      Vector3 mousePos = Input.mousePosition;
      Vector2 finishPos = new Vector2 (mousePos.x, mousePos.y);
      capFinishX = (int)mousePos.x;
      capFinishY = (int)mousePos.y;
      //重新计算截取的位置
      int capLeftX = (capBeginX < capFinishX) ? capBeginX : capFinishX;
      int capRightX = (capBeginX < capFinishX) ? capFinishX : capBeginX;
      int capLeftY = (capBeginY < capFinishY) ? capBeginY : capFinishY;
      int capRightY = (capBeginY < capFinishY) ? capFinishY : capBeginY;
 
      Rect rect=new Rect(capLeftX,capLeftY,capRightX,capRightY);
      StartCoroutine( Captrue (rect));
    }
  }
 
  IEnumerator Captrue(Rect rect){
 
    int t_width = Mathf.Abs (capFinishX - capBeginX);
    int t_length = Mathf.Abs (capFinishY - capBeginY);
 
    yield return new WaitForEndOfFrame ();
    Texture2D t = new Texture2D(t_width , t_length,TextureFormat.RGB24, true);//需要 
     正确设置好图片保存格式 
    t.ReadPixels(rect, 0, 0, false);//按照设定区域读取像素;注意是以左下角为原点读取 
    t.Apply(); 
    byte[] byt = t.EncodeToPNG(); 
    File.WriteAllBytes(Application.dataPath + Time.time + ".png", byt); 
 
    Sprite target = Sprite.Create (t, new Rect(0, 0, t_width, t_length), Vector2.zer);
    showImg.sprite = target;
  }

小编为大家分享一段Unity实现截屏功能的代码,供大家参考:

public class ScreenShot : MonoBehaviour 
{

  void OnScreenShotClick()
  {
    //得到当前系统时间
    System.DateTime now = System.DateTime.Now;
    string times = now.ToString();
    //去掉前后空格
    times = times.Trim();
    //将斜杠替换成横杠
    times = times.Replace("/", "-");

    string fileName = "ARScreenShot" + times + ".png";
    //判断该平台是否为安卓平台
    if (Application.platform == RuntimePlatform.Android)
    {
      //参数依次为 屏幕宽度 屏幕高度 纹理格式 是否使用映射
      Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
      //读取贴图
      texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
      //应用截屏
      texture.Apply();
      //将对象序列化
      byte[] bytes = texture.EncodeToPNG();
      //设定存储到的手机文件夹路径
      string destination = "/sdcard/DCIM/Screenshots";
      //如果不存在该文件夹
      if (!Directory.Exists(destination))
      {
        //创建该文件夹
        Directory.CreateDirectory(destination);
      }
      string pathSave = destination + "/" + fileName;
      File.WriteAllBytes(pathSave, bytes);
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多