Csharp/C#教程:unity实现按住鼠标选取区域截图分享

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

privateintcapBeginX; privateintcapBeginY; privateintcapFinishX; privateintcapFinishY; publicImageshowImg; //Usethisforinitialization voidStart(){ } //Updateiscalledonceperframe voidUpdate(){ if(Input.GetMouseButtonDown(0)){ Vector3mousePos=Input.mousePosition; Vector2beginPos=newVector2(mousePos.x,mousePos.y); capBeginX=(int)mousePos.x; capBeginY=(int)mousePos.y; } if(Input.GetMouseButtonUp(0)){ Vector3mousePos=Input.mousePosition; Vector2finishPos=newVector2(mousePos.x,mousePos.y); capFinishX=(int)mousePos.x; capFinishY=(int)mousePos.y; //重新计算截取的位置 intcapLeftX=(capBeginX<capFinishX)?capBeginX:capFinishX; intcapRightX=(capBeginX<capFinishX)?capFinishX:capBeginX; intcapLeftY=(capBeginY<capFinishY)?capBeginY:capFinishY; intcapRightY=(capBeginY<capFinishY)?capFinishY:capBeginY; Rectrect=newRect(capLeftX,capLeftY,capRightX,capRightY); StartCoroutine(Captrue(rect)); } } IEnumeratorCaptrue(Rectrect){ intt_width=Mathf.Abs(capFinishX-capBeginX); intt_length=Mathf.Abs(capFinishY-capBeginY); yieldreturnnewWaitForEndOfFrame(); Texture2Dt=newTexture2D(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); Spritetarget=Sprite.Create(t,newRect(0,0,t_width,t_length),Vector2.zer); showImg.sprite=target; }

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

publicclassScreenShot:MonoBehaviour { voidOnScreenShotClick() { //得到当前系统时间 System.DateTimenow=System.DateTime.Now; stringtimes=now.ToString(); //去掉前后空格 times=times.Trim(); //将斜杠替换成横杠 times=times.Replace("/","-"); stringfileName="ARScreenShot"+times+".png"; //判断该平台是否为安卓平台 if(Application.platform==RuntimePlatform.Android) { //参数依次为屏幕宽度屏幕高度纹理格式是否使用映射 Texture2Dtexture=newTexture2D(Screen.width,Screen.height,TextureFormat.RGB24,false); //读取贴图 texture.ReadPixels(newRect(0,0,Screen.width,Screen.height),0,0); //应用截屏 texture.Apply(); //将对象序列化 byte[]bytes=texture.EncodeToPNG(); //设定存储到的手机文件夹路径 stringdestination="/sdcard/DCIM/Screenshots"; //如果不存在该文件夹 if(!Directory.Exists(destination)) { //创建该文件夹 Directory.CreateDirectory(destination); } stringpathSave=destination+"/"+fileName; File.WriteAllBytes(pathSave,bytes); } } } 您可能感兴趣的文章:Unity实现截屏以及根据相机画面截图Unity实现截图功能Unity实现全屏截图以及QQ截图unity实现QQ截图功能Unity实现相机截图功能

标签: unity ni

详解bash中的初始化机制

详解bash中的脚本调试机制

上述就是C#学习教程:unity实现按住鼠标选取区域截图分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/903951.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐