欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-14 15:07:58
本文实例为大家分享了unity按住鼠标选取区域截图的具体代码,供大家参考,具体内容如下private int capbeginx;private int capbeginy;private int c...

本文实例为大家分享了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);
    }
  }
}

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