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;
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:unity实现按住鼠标选取区域截图的方法-创新互联
网站网址:http://www.jxjierui.cn/article/ccgsis.html


咨询
建站咨询
