Is mouse down and on?

Is there an easy way to detect if the mouse is both holding down (like with void update) and touching a sprite? I can’t find a way to mix them.

Can you assign those sprites to a layer, then combine a layer detection with a mousebuttondown?

I do something like this for my inventory sprites on screen (excuse the formating, did a sloppy job of copying and pasting). This is also using the old input system I believe. I assume this could be made to work with getkeydown/getbuttondown.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;

void Update()
{

if (Input.GetKeyDown(KeyCode.Mouse0))  //This is just for a single press
{

//Set up the new Pointer Event
m_PointerEventData = new PointerEventData(m_EventSystem);

//Set the Pointer Event Position to that of the mouse position
m_PointerEventData.position = Input.mousePosition;

//Create a list of Raycast Results
List<RaycastResult> results = new List<RaycastResult>();

//Raycast using the Graphics Raycaster and mouse click position
m_Raycaster.Raycast(m_PointerEventData, results);

 //For every result returned, output the name of the GameObject on the Canvas hit by the Ray
     foreach (RaycastResult result in results)
     {
               if (result.gameObject.layer == 25)
               {
                    //This is where you'll put your logic code
                }
          }
}
}

Ah, guess zulo3d’s approach may be simpler haha…

OnMouseDown

3 Likes