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

所属分类: 软件编程 / C#教程 阅读数: 93
收藏 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#实现写系统日志的方法

这篇文章主要介绍了C#实现写系统日志的方法,涉及C#针对系统日志的创建、写入及删除等技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#编程实现自定义热键的方法

这篇文章主要介绍了C#编程实现自定义热键的方法,涉及C#键盘按键设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现读取被进程占用的文件实现方法

这篇文章主要介绍了C#实现读取被进程占用的文件实现方法,涉及C#进程操作及文件读取的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#获取任务栏显示进程的方法

这篇文章主要介绍了C#获取任务栏显示进程的方法,涉及C#针对进程操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#调用dos窗口获取相关信息的方法

这篇文章主要介绍了C#调用dos窗口获取相关信息的方法,涉及C#调用dos窗口及进程操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#通过Semaphore类控制线程队列的方法

这篇文章主要介绍了C#通过Semaphore类控制线程队列的方法,涉及Semaphore类操作线程队列的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用Dispose模式实现手动对资源的释放

这篇文章主要介绍了C#使用Dispose模式实现手动对资源的释放,涉及C#采用Dispose模式操作资源的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#动态创建button的方法

这篇文章主要介绍了C#动态创建button的方法,涉及C#按钮属性动态设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#编程获取资源文件中图片的方法

这篇文章主要介绍了C#编程获取资源文件中图片的方法,涉及C#针对项目中资源文件操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

WinForm实现窗体最大化并遮盖任务栏的方法

这篇文章主要介绍了WinForm实现窗体最大化并遮盖任务栏的方法,涉及C#实现WinForm窗体全屏显示的实现及调用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多