1

I have scene, camera, object with Grid component and childs with Tilemap+TilemapRenderer components, and then script on Grid object.

I need to catch mouse events (and then detect cell, i know how to do it) on these objects. Not Input.GetMouseButton(0) in update lifecycle, i need event-style handling for it. How can i make it?

Ive tried colliders on tilemaps/grid and MonoBeha:OnMouseDown method, tried methods from IPointerEnterHandler (and another form package), and raycasts. Nothing worked...

I really confused, i should make myown event system from Input.GetMouseButton(0)?

Johnson
  • 265
  • 3
  • 11

1 Answers1

2

OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

So if you want to use OnMouseDown you could add a BoxCollider2D to your Grid or Tilemap (You may have to check IsTrigger if it ends up pushing away other objects)

I just tried adding a collider and a small script on both a Grid and a Tilemap and it works fine

public void OnMouseDown()
{ 
    Debug.Log("Boom!");
}
Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42