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

 更新时间:2020年04月16日 11:40:20   作者:LixiSchool  
这篇文章主要为大家详细介绍了unity实现按住鼠标选取区域截图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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# 实现语音合成的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • C#如何连接使用Zookeeper

    C#如何连接使用Zookeeper

    Zookeeper作为分布式的服务框架,虽然是java写的,但是强大的C#也可以连接使用。而现在主要有两个插件可供使用,分别是ZooKeeperNetEx和Zookeeper.Net,个人推荐使用ZooKeeperNetEx做开发,本文也已介绍ZooKeeperNetEx为主
    2021-06-06
  • C#开源的AOP框架--KingAOP基础

    C#开源的AOP框架--KingAOP基础

    这篇文章主要介绍了一款C#开源的AOP框架--KingAOP框架的基础知识,对于想学习AOP的小伙伴来说,非常不错,希望大家能够喜欢。
    2015-12-12
  • Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)

    Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)

    这篇文章主要介绍了Unity通用泛型单例设计模式,分为普通型和继承MonoBehaviour,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • c#基于Win32Api实现返回Windows桌面功能

    c#基于Win32Api实现返回Windows桌面功能

    本文分享下回到桌面功能的实现方法,效果与快捷键(Win+D)相同。有此需求的朋友可以参考下
    2021-05-05
  • Unity3D实现批量下载图片功能

    Unity3D实现批量下载图片功能

    这篇文章主要为大家详细介绍了Unity3D实现批量下载图片功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • C#递归算法寻找数组中第K大的数

    C#递归算法寻找数组中第K大的数

    首先将向量V从中间位置分开,分成左和右,分好后,中间值的索引如果恰恰等于K,就找到了,否则如果中间元素索引大于K,则在左子表中继续查找,忽略右子表,如果中间值索引小于K,则在右子表中继续查找,如此循环往复。
    2016-06-06
  • C#中Request.Cookies 和 Response.Cookies 的区别分析

    C#中Request.Cookies 和 Response.Cookies 的区别分析

    本文通过实例代码向我们展示了C#中Request.Cookies 和 Response.Cookies 的区别,文章浅显易懂,这里推荐给大家。
    2014-11-11
  • 使用C#调用系统API实现内存注入的代码

    使用C#调用系统API实现内存注入的代码

    使用C#调用系统API实现内存注入的代码,学习c#的朋友可以参考下。
    2011-06-06
  • C#连接SQL Sever数据库详细图文教程

    C#连接SQL Sever数据库详细图文教程

    C#是Microsoft公司为.NET Framework推出的重量级语言,和它搭配最完美的数据库无疑就是Microsoft SQL Server了,下面这篇文章主要给大家介绍了关于C#连接SQL Sever数据库的详细图文教程,需要的朋友可以参考下
    2023-06-06

最新评论