Unity 시 뮬 레이 션 마우스 자동 클릭 이벤트

2489 단어 Unity
문장http://www.jb51.net/article/54023.htm
마우스 클릭 이벤트 가 자동 으로 호출 되 어야 할 때 가 있 습 니 다.아래 는 수 요 를 만족 시 킬 수 있 습 니 다.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class ImitateMouseClick : MonoBehaviour {

    /// 
    ///     
    /// 
    ///     
    /// 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);

    /// 
    ///          
    /// 
    [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
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("    A Down");
            DoMouseClick(600, 800);
        }

        if(Input.GetMouseButtonDown(0))
        {
            Debug.Log("      Down  ");
        }

        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("      Up  ");
        }
    }

    /// 
    ///         (    x,y             )
    /// 
    ///         X   
    ///         Y   
    private static void DoMouseClick(int x, int y)
    {
        int dx = (int)((double)x / Screen.width * 65535); //        0~65535(0xffff, 16 )  
        int dy = (int)((double)y / Screen.height * 0xffff); //   double    ,    0、1
        mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //  
    }
}

주의:Mouse EventFlag.Absolute 를 잘 이용 하여 위 치 를 정 해 야 합 니 다.
mouse_event(MouseEventFlag.LeftDown| MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //  

이것 은 상대 적 으로 왼쪽 상단 의 위치 이다.
DoMouseClick(600, 800);

4.567913.이것 은 현재 마우스 의 위치 입 니 다.이것 은 자동 으로 마우스 클릭 단 추 를 호출 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기