MacOS平台下C#获取鼠标位置:无需Cocoa/XCode且兼容Unity的方案问询
Hey there! Let's tackle your two questions one by one—first getting the mouse position in plain C# on macOS, then the Unity-compatible approach that avoids Cocoa/Xcode entirely.
You don't need Cocoa or Xcode for this—you can directly call macOS's CoreGraphics C API via P/Invoke in C#. The CGEventGetLocation function gives you the global mouse position, and it's accessible without any Objective-C bindings.
Here's a complete example:
using System; using System.Runtime.InteropServices; public static class MacMouseUtils { // Import the CoreGraphics function [DllImport("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")] private static extern CGPoint CGEventGetLocation(IntPtr eventSource); // Match the CGPoint struct from CoreGraphics [StructLayout(LayoutKind.Sequential)] private struct CGPoint { public double X; public double Y; } /// <summary> /// Gets the global mouse position in macOS's coordinate system (origin at bottom-left) /// </summary> /// <returns>X and Y coordinates as a tuple</returns> public static (double X, double Y) GetGlobalMousePosition() { // Pass IntPtr.Zero to get the current mouse position from the default event source var point = CGEventGetLocation(IntPtr.Zero); return (point.X, point.Y); } }
Quick Notes:
- macOS uses a bottom-left origin for screen coordinates, which is different from most UI frameworks that use top-left. Adjust the Y value if you need to match another coordinate system.
- This works with any .NET runtime on macOS (like .NET 6+/Core, or Mono) since P/Invoke is supported across all of them.
Unity runs on Mono (or .NET Core in newer versions) on macOS, so the same P/Invoke approach works perfectly—no need for Xcode projects or Cocoa bindings. You can drop this code directly into a Unity C# script.
This example converts the macOS coordinate system to Unity's top-left screen origin for convenience:
using UnityEngine; using System.Runtime.InteropServices; public class MacGlobalMouseTracker : MonoBehaviour { [DllImport("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")] private static extern CGPoint CGEventGetLocation(IntPtr eventSource); [StructLayout(LayoutKind.Sequential)] private struct CGPoint { public double X; public double Y; } void Update() { // Get the system-wide mouse position var systemPosition = CGEventGetLocation(IntPtr.Zero); // Convert to Unity's screen coordinate system (origin at top-left) float unityX = (float)systemPosition.X; float unityY = Screen.height - (float)systemPosition.Y; Debug.Log($"Global Mouse Position (Unity Screen): ({unityX:F2}, {unityY:F2})"); } }
How to Use:
- Create a new C# script in Unity and paste this code.
- Attach the script to any GameObject in your scene.
- Enter Play Mode—you'll see the global mouse position in the Console, adjusted to Unity's screen coordinates.
Key Advantages:
- No Xcode setup required: All code is pure C# and runs directly in Unity.
- Works with both Intel and Apple Silicon Macs: The CoreGraphics framework is universal, so P/Invoke will resolve correctly for both architectures.
- Gets the system-wide mouse position, not just the position within the Unity window (if you need window-local position, combine this with
Screenclass methods or Unity'sInput.mousePosition).
内容的提问来源于stack exchange,提问作者Max




