Unity控制鼠标移动及点击

 #region 鼠标点击

    ///   
    /// 鼠标事件  
    ///   
    /// 事件类型  
    /// x坐标值(0~65535)  
    /// y坐标值(0~65535)  
    /// 滚动值(120一个单位)  
    /// 不支持  
    [DllImport("user32.dll")]
    static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

    [DllImport("user32.dll")] //强制设置鼠标坐标
    public static extern int SetCursorPos(int x, int y);

    ///   
    /// 鼠标操作标志位集合  
    ///   
    [Flags]
    enum MouseEventFlag : uint
    {
        Move = 0x0001,
        LeftDown = 0x0002,
        LeftUp = 0x0004,
        RightDown = 0x0008,
        RightUp = 0x0010,
        MiddleDown = 0x0020,
        MiddleUp = 0x0040,
        XDown = 0x0080,
        XUp = 0x0100,
        Wheel = 0x0800,
        VirtualDesk = 0x4000,
        ///   
        /// 设置鼠标坐标为绝对位置(dx,dy),否则为距离最后一次事件触发的相对位置  
        ///   
        Absolute = 0x8000
    }

    ///   
    /// 调用鼠标点击事件(传的参数x,y要在应用内才会调用鼠标事件)  
    ///   
    /// 相对屏幕左上角的X轴坐标  
    /// 相对屏幕左上角的Y轴坐标  
    private static void DoMouseClick(Vector3 pos)
    {
        //int dx = (int)((double)x / Screen.width * 65535); //屏幕分辨率映射到0~65535(0xffff,即16位)之间  
        //int dy = (int)((double)y / Screen.height * 0xffff); //转换为double类型运算,否则值为0、1  

        Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);
        mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击  
    }

    private static void DoMouseDown(Vector3 pos)
    {
        Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);

        mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.Absolute, (int)pos.x, (int)pos.y, 0, new UIntPtr(0)); //点击  
    }

    private static void DoMouseUp(Vector3 pos)
    {
        Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);

        mouse_event(MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击  
    }

    #endregion
使用:
 private void Update()
    {
        if (_listenerWebSocket.ProjectionDatas.Count > 0)
        {
            SetCursorPos((int)_x, (int)_y);
            DoMouseClick(new Vector3(_x, _y));
        }
    }
 
 

你可能感兴趣的:(Unity,C#)