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#設(shè)計(jì)模式之Template模板方法模式實(shí)現(xiàn)ASP.NET自定義控件 密碼強(qiáng)度檢測(cè)功能
這篇文章主要介紹了C#設(shè)計(jì)模式之Template模板方法模式實(shí)現(xiàn)ASP.NET自定義控件 密碼強(qiáng)度檢測(cè)功能,簡(jiǎn)單介紹了模板方法模式的定義、原理及檢測(cè)密碼強(qiáng)度的相關(guān)使用技巧,需要的朋友可以參考下2017-09-09
C#結(jié)合JS實(shí)現(xiàn)HtmlTable動(dòng)態(tài)添加行并保存到數(shù)據(jù)庫的流程步驟
在 Web 應(yīng)用項(xiàng)目中,實(shí)現(xiàn)一對(duì)多錄入的數(shù)據(jù)管理功能是一項(xiàng)常見的應(yīng)用,因此可以實(shí)現(xiàn)一個(gè)相對(duì)輕量化的設(shè)計(jì)實(shí)現(xiàn)表格的錄入,為保證功能的可用性、界面友好性,本文給大家介紹了C#結(jié)合JS實(shí)現(xiàn)HtmlTable動(dòng)態(tài)添加行并保存到數(shù)據(jù)庫,需要的朋友可以參考下
2024-12-12 
