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

发布时间:2020-08-03 11:06:51 作者:小猪
来源:亿速云 阅读:246

这篇文章主要讲解了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) &#63; capBeginX : capFinishX;
      int capRightX = (capBeginX < capFinishX) &#63; capFinishX : capBeginX;
      int capLeftY = (capBeginY < capFinishY) &#63; capBeginY : capFinishY;
      int capRightY = (capBeginY < capFinishY) &#63; 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);
    }
  }
}

看完上述内容,是不是对unity实现按住鼠标选取区域截图的方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么利用canvas实现按住鼠标移动绘制出轨迹
  2. python+opencv如何初始种子自动选取的区域生长

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity un

上一篇:mysql常用函数的用法

下一篇:python查看模块是否安装成功的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》