How do you get the position of the mouse on screen with C# in MacOS?

570 views Asked by At

Without using Cocoa/XCode, is there a way I can get the position of the mouse cursor in C# (MacOS)? It needs to be Unity-Compatible.

1

There are 1 answers

1
Chipset31 On

Taken from https://docs.unity3d.com/ScriptReference/Input-mousePosition.html

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public GameObject particle;
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray))
                Instantiate(particle, transform.position, transform.rotation);
        }
    }
}

or taken from https://stackoverflow.com/a/46999239/12808204

//the object to move
public Transform objectToMove;

void Update()
{
    Vector3 mouse = Input.mousePosition;
    Ray castPoint = Camera.main.ScreenPointToRay(mouse);
    RaycastHit hit;
    if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
    {
        objectToMove.transform.position = hit.point;
    }
}

Or if you're looking for more specifically an OS API implementation, not necessarily for unity, check Getting "global" mouse position in Mac OS X Not to be that guy, but at least try to google it before making a question thread. These where all top google results.