You need to create your own custom Input Module to change this behavior.
Create a new C# script and name it “MyInputModule”.
Copy & paste the StandaloneInputModule source code into this new script and replace all occurrences of “StandaloneInputModule” with “MyInputModule” in the newly copied code.
To stop mouse clicks from deselecting objects, just delete or comment-out line 322 in the new script: DeselectIfSelectionChanged(currentOverGo, pointerEvent);
Or to disable pointer input completely, just delete or comment-out line 179 in the new script: ProcessMouseEvent();
Finally, remove the StandaloneInputModule script attached to the EventSystem object and replace it with your new [64244-myinputmodule.zip|64244] script to use it.
You can use a low level mouse hook like so. This is not global (do NOT use a global hook for a video game). This is only if you’re completely and utterly not going to use the mouse or touch screen in your entire game. Just create an instace of this object and do myMouse.EnableClicks() or myMouse.DisableClicks().
public class Mouse
{
#if UNITY_STANDALONE_WIN
public WindowsLowLevelHook windowsLowLevelHook = new WindowsLowLevelHook();
public void DisableClicks()
{
windowsLowLevelHook.Enable();
}
public void EnableClicks()
{
windowsLowLevelHook.Disable();
}
public class WindowsLowLevelHook
{
public bool enabled = false;
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
public HookProc hookProc;
public delegate void MouseHookCallback(MOUSEHOOKSTRUCT mouseHookStruct);
#region Events
public event MouseHookCallback LeftButtonDown;
public event MouseHookCallback LeftButtonUp;
public event MouseHookCallback RightButtonDown;
public event MouseHookCallback RightButtonUp;
public event MouseHookCallback MouseMove;
public event MouseHookCallback MouseWheel;
public event MouseHookCallback DoubleClick;
public event MouseHookCallback MiddleButtonDown;
public event MouseHookCallback MiddleButtonUp;
#endregion
public IntPtr hookID = IntPtr.Zero;
public void Enable()
{
hookProc = MouseProc;
hookID = SetHook(hookProc);
enabled = true;
}
public void Disable()
{
if (hookID == IntPtr.Zero)
return;
UnhookWindowsHookEx(hookID);
hookID = IntPtr.Zero;
enabled = false;
}
~WindowsLowLevelHook()
{
Enable();
}
private IntPtr SetHook(HookProc hookProc)
{
using (ProcessModule module = Process.GetCurrentProcess().MainModule)
return SetWindowsHookEx(WH_MOUSE, hookProc, GetModuleHandle(module.ModuleName), GetCurrentThreadId());
}
private IntPtr MouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MOUSEHOOKSTRUCT lParamStruct = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));
if (nCode >= 0)
{
if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
return (IntPtr)1;
}
}
return CallNextHookEx(hookID, nCode, wParam, lParam);
}
#region WinAPI
private const int WH_MOUSE = 7;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_LBUTTONDBLCLK = 0x0203,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208
}
private Int32 GetWindowThreadProcessID(IntPtr hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEHOOKSTRUCT
{
public POINT pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, Int32 dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll")]
private static extern Int32 GetCurrentThreadId();
#endregion
}
#endif
}