Will mouse click detector demo translate to 2D?

Here is the code I found in a mouse click detector demo for use with 3D box colliders:

using UnityEngine;
using System.Collections;
 
public class ClickDetector : MonoBehaviour
{
    public bool HandleLeftClick = true;
    public bool HandleRightClick = true;
    public bool HandleMiddleClick = false;
    public string OnLeftClickMethodName = "OnLeftClick";
    public string OnRightClickMethodName = "OnRightClick";
    public string OnMiddleClickMethodName = "OnMiddleClick";
    public LayerMask layerMask;
 
    void Update()
    {
        GameObject clickedGmObj = null;
        bool clickedGmObjAcquired = false;
 
        // Left click
        if (HandleLeftClick && Input.GetMouseButtonDown(0))
        {
            /*if (!clickedGmObjAcquired)
            {*/
                clickedGmObj = GetClickedGameObject();
                clickedGmObjAcquired = true;
            /*}*/
 
            if (clickedGmObj != null)
                clickedGmObj.SendMessage(OnLeftClickMethodName, null, SendMessageOptions.DontRequireReceiver);
        }
 
        // Right click
        if (HandleRightClick && Input.GetMouseButtonDown(1))
        {
            if (!clickedGmObjAcquired)
            {
                clickedGmObj = GetClickedGameObject();
                clickedGmObjAcquired = true;
            }
 
            if (clickedGmObj != null)
                clickedGmObj.SendMessage(OnRightClickMethodName, null, SendMessageOptions.DontRequireReceiver);
        }
 
        // Middle click
        if (HandleMiddleClick && Input.GetMouseButtonDown(2))
        {
            if (!clickedGmObjAcquired)
            {
                clickedGmObj = GetClickedGameObject();
                clickedGmObjAcquired = true;
            }
 
            if (clickedGmObj != null)
                clickedGmObj.SendMessage(OnMiddleClickMethodName, null, SendMessageOptions.DontRequireReceiver);
        }
    }
 
    GameObject GetClickedGameObject()
    {
        // Builds a ray from camera point of view to the mouse position
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
 
        // Casts the ray and get the first game object hit
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            return hit.transform.gameObject;
        else
            return null;
    }
}

I’m trying to convert this for use with Sprites and 2D Box Colliders - without any luck. There are ideas online for different methods of detecting mouse clicks, but I’m keen to use something as close to this as possible. Does anyone have any suggestions?

Thanks very much!
Romano

I did not see anything beyond GetClickedGameObject() that had to change for 2D. It can be rewritten as:

GameObject GetClickedGameObject() {
	Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	RaycastHit2D hit = Physics2D.Raycast (pos, Vector3.zero);

	if (hit != null && hit.collider != null) 
		return hit.collider.gameObject;
	else
		return null;
}

Note this code assumes an Orthographic camera.

I’ve taken the answer below and added a lot more code to include sorting layers and sorting order. I can’t believe how complicated it is. I’ve posted the script here: Not Quite Black and White