Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Runtime.InteropServices;
- using System;
- public class TestMouse : MonoBehaviour {
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
- [DllImport("user32.dll")]
- public static extern bool SetCursorPos(int X, int Y);
- [DllImport("user32.dll")]
- public static extern bool GetCursorPos(out Point pos);
- [DllImport("user32.dll", SetLastError=true)]
- static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
- [DllImport("user32.dll", EntryPoint = "FindWindow")]
- public static extern IntPtr FindWindow(System.String className, System.String windowName);
- [StructLayout(LayoutKind.Sequential)]
- public struct Point
- {
- public int X;
- public int Y;
- public Point(int x, int y)
- {
- this.X = x;
- this.Y = y;
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int Left; // x position of upper-left corner
- public int Top; // y position of upper-left corner
- public int Right; // x position of lower-right corner
- public int Bottom; // y position of lower-right corner
- }
- private Point mousePos;
- private RECT windowPos;
- private string labelString;
- private bool isOutOfWindow(Point mousePosition,RECT windowPosition)
- {
- if (mousePosition.X > windowPosition.Right || mousePosition.X < windowPosition.Left ||
- mousePosition.Y > windowPosition.Bottom || mousePosition.Y < windowPosition.Top)
- {
- return true;
- }
- return false;
- }
- void OnGUI()
- {
- GUI.Label (new Rect(100,50,450,100),"Mouse Position: " + Input.mousePosition.ToString());
- if(GetWindowRect (FindWindow(null,"Your Product Name"),out windowPos))
- {
- GetCursorPos (out mousePos);
- GUI.Label (new Rect(100,100,300,100),"Mouse Global Position: " + mousePos.X + " "+mousePos.Y);
- GUI.Label (new Rect(100,150,450,100),"Window Position : Bottom:" + windowPos.Bottom + " Top:"+windowPos.Top + " Left:"+windowPos.Left+" Right:"+windowPos.Right);
- labelString = isOutOfWindow(mousePos,windowPos)== true ? "OUT" : "IN";
- GUI.Label(new Rect(Screen.width/2,Screen.height/2,200,80),labelString);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement