Unity 마우스 이동 및 클릭 제어

2485 단어 UnityC#
 #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));
        }
    }

좋은 웹페이지 즐겨찾기