Unity3D通过鼠标点击拖动获取屏幕范围

 作为一个新人,我也来发表一篇文章,大早上的通过unity3d来给大家分享一下我自己做的小demo;


首先获取鼠标在屏幕上点击的那个点的坐标,在这里要用到input类中的MouseButtonDown()这个方法来获得鼠标的左键,右键已经滚轮的点击反应。

今天我们就用左键吧,在MouseButtonDown()这个方法里,使用参数0来表示按下左键,;使用MouseButtonUp(0)方法来表示释放左键,因为在屏幕

中是要拖动的,所以需要持续按下鼠标的函数GetMouseButton(0);这三个函数我们都放在Update里面执行;




在这里我们使用unity自带的画图类库来对鼠标点击并且拖动形成的范围进行画图,我们使用类GL中的一些方法,如PushMatrix()、LoadPixelMatrix(),以及开始画图的点和结束画图的点的函数,在这里就不一一介绍啦,待会直接上传代码就好了。


当鼠标点击拖动,形成的区域肯定是矩形,所以这边涉及上下左右边框的计算,我们使用一个材质来表示,


最后,当鼠标左键施放之后,所形成的区域就在屏幕中释放,我们使用GL中的PopMatrix()函数来表示结束;


下面是我这个demo的源代码,给大家分享一下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GetRange : MonoBehaviour {


    private Vector2 mouseStartPos, mouseEndPos;
    private bool mBDrawMouseRect;


    public Material rectMat;//画线的材质


    void Start()
    {
        mBDrawMouseRect = false;
    }


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        //按下鼠标左键  
        {
            Vector3 mousePosition = Input.mousePosition;
            mouseStartPos = new Vector2(mousePosition.x, mousePosition.y);
        }


        if (Input.GetMouseButton(0))
        //持续按下鼠标左键  
        {
            mBDrawMouseRect = true;
            Vector3 mousePosition = Input.mousePosition;
            mouseEndPos = new Vector2(mousePosition.x, mousePosition.y);
        }


        if (Input.GetMouseButtonUp(0))
        {
            mBDrawMouseRect = false;
        }
    }


    void OnGUI()
    {
        if (mBDrawMouseRect)
            Draw(mouseStartPos, mouseEndPos);
        GUILayout.Label("X:"+Input.mousePosition.x);
        GUILayout.Label("Y:" + Input.mousePosition.y);
    }


    //渲染2D框  
    void Draw(Vector2 start, Vector2 end)
    {
        rectMat.SetPass(0);
        


        GL.PushMatrix();//保存摄像机变换矩阵  


        Color clr = Color.green;
        clr.a = 0.1f;


        GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
                             //透明框  
        GL.Begin(GL.QUADS);
        GL.Color(clr);
        GL.Vertex3(start.x, start.y, 0);
        GL.Vertex3(end.x, start.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.Vertex3(start.x, end.y, 0);
        GL.End();


        //线  
        //上  
        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        GL.Vertex3(start.x, start.y, 0);
        GL.Vertex3(end.x, start.y, 0);
        GL.End();


        //下  
        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        GL.Vertex3(start.x, end.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.End();


        //左  
        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        GL.Vertex3(start.x, start.y, 0);
        GL.Vertex3(start.x, end.y, 0);
        GL.End();


        //右  
        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        GL.Vertex3(end.x, start.y, 0);
        GL.Vertex3(end.x, end.y, 0);
        GL.End();


        GL.PopMatrix();//还原  
    }


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KathyKarol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值