본문 바로가기

프로그램/유니티 스크립트 소스

[유니티 스크립트 소스] 마우스 이벤트 보내기

728x90
반응형

1. 네임스페이스

네임스페이스
using System.Runtime.InteropServices;
using System.Drawing;

 

2. 소스

using System.Collections;
using UnityEngine;
using System.Runtime.InteropServices;
using System.Drawing;

public class MouseEvent : MonoBehaviour
{
    [DllImport("user32.dll")] 
    static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);
    [DllImport("user32.dll")] 
    static extern bool GetCursorPos(ref Point lpPoint);

    [DllImport("user32.dll")] 
    static extern int SetCursorPos(int x, int y);

    private const uint MOUSEEVENTF_MOVE = 0x0001;
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;
    private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
    private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    private const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
    private const uint MOUSEEVENTF_XDOWN = 0x0080;
    private const uint MOUSEEVENTF_XUP = 0x0100;
    private const uint MOUSEEVENTF_WHEEL = 0x0800;
    private const uint MOUSEEVENTF_HWHEEL = 0x01000;
    private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
    
    void Start()
    {
        StartCoroutine(StartMouseEvnet());
    }
        
    void Update()
    {
        GetMouseEvent();
    }

    IEnumerator StartMouseEvnet()
    {
        yield return new WaitForSeconds(1.0f);

        SetCursorPos(500, 500);

        Point point = new Point();
        GetCursorPos(ref point);
        Debug.LogFormat($"x : {point.X} y : {point.Y}");

        yield return new WaitForSeconds(1.0f);

        mouse_event(MOUSEEVENTF_LEFTDOWN, 10, 10, 0, 0);
    }

    void GetMouseEvent()
    {
        if (Input.GetMouseButtonDown(0))
            Debug.Log("Pressed left click.");

        if (Input.GetMouseButtonDown(1))
            Debug.Log("Pressed right click.");

        if (Input.GetMouseButtonDown(2))
            Debug.Log("Pressed middle click.");
    }
}
728x90
반응형