unity實(shí)現(xiàn)按住鼠標(biāo)選取區(qū)域截圖
更新時(shí)間:2020年04月16日 11:40:20 作者:LixiSchool
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)按住鼠標(biāo)選取區(qū)域截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了unity按住鼠標(biāo)選取區(qū)域截圖的具體代碼,供大家參考,具體內(nèi)容如下
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; //重新計(jì)算截取的位置 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);//需要 正確設(shè)置好圖片保存格式 t.ReadPixels(rect, 0, 0, false);//按照設(shè)定區(qū)域讀取像素;注意是以左下角為原點(diǎn)讀取 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實(shí)現(xiàn)截屏功能的代碼,供大家參考:
public class ScreenShot : MonoBehaviour { void OnScreenShotClick() { //得到當(dāng)前系統(tǒng)時(shí)間 System.DateTime now = System.DateTime.Now; string times = now.ToString(); //去掉前后空格 times = times.Trim(); //將斜杠替換成橫杠 times = times.Replace("/", "-"); string fileName = "ARScreenShot" + times + ".png"; //判斷該平臺(tái)是否為安卓平臺(tái) if (Application.platform == RuntimePlatform.Android) { //參數(shù)依次為 屏幕寬度 屏幕高度 紋理格式 是否使用映射 Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //讀取貼圖 texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); //應(yīng)用截屏 texture.Apply(); //將對(duì)象序列化 byte[] bytes = texture.EncodeToPNG(); //設(shè)定存儲(chǔ)到的手機(jī)文件夾路徑 string destination = "/sdcard/DCIM/Screenshots"; //如果不存在該文件夾 if (!Directory.Exists(destination)) { //創(chuàng)建該文件夾 Directory.CreateDirectory(destination); } string pathSave = destination + "/" + fileName; File.WriteAllBytes(pathSave, bytes); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案
這篇文章主要介紹了C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02采用C#代碼動(dòng)態(tài)設(shè)置文件權(quán)限
在開發(fā)中,我們經(jīng)常會(huì)使用IO操作,例如創(chuàng)建,刪除文件等操作。在項(xiàng)目中這樣的需求也較多,我們也會(huì)經(jīng)常對(duì)這些操作進(jìn)行編碼,但是對(duì)文件的權(quán)限進(jìn)行設(shè)置,這樣的操作可能會(huì)手動(dòng)操作,本文介紹一種采用代碼動(dòng)態(tài)對(duì)文件設(shè)置權(quán)限的操作。2016-12-12Unity游戲開發(fā)之炸彈人游戲的實(shí)現(xiàn)
大家小時(shí)候肯定玩過這款游戲,炸彈人也算是經(jīng)典中的經(jīng)典啦。本文將利用Unity模擬實(shí)現(xiàn)這一經(jīng)典游戲,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-03-03C#將Sql數(shù)據(jù)保存到Excel文件中的方法
這篇文章主要介紹了C#將Sql數(shù)據(jù)保存到Excel文件中的方法,文中的ExportExcel可起到將sql數(shù)據(jù)導(dǎo)出為Excel的作用,需要的朋友可以參考下2014-08-08