So I am trying to flesh out my event system and I was curious on what the “right” way to do this is.
Right now, I can check for mouse input, translate that to a cell location and access the tile - thats fine. But zooming back out when I am looking at click handling, key configuration, etc I was wondering how folks organize and sipatch their events?
Before I go and roll my own event dispatcher I wanted to see if there was something obvious that I was missing.
Use cases in mind:
User kitting a key or button which maps to an action (this is common behavior across scenes) that should be handled by an object possibly a few layers deep into a scene. Think right mouse button that triggers an action unique to a specific tile in a tilemap.
User hitting a key to scroll the camera
User interacting with UI elements in the same scene but not actually part of the grid
I can visualize building a simple event system and dispatching it to objects with listeners registered to listen for specific events but before I put a bunch of work into I just want to see if I am on the right track or not. I know there is stuff like IPointerClickHandler but it doesn’t seem like that works on individual tiles.
I don’t believe you need to get into low level stuff like IPointerClickHandler. Just put a Button on each grid cell and hook up the listener.
If you want an example of dynamic button generation in a grid, see the attached project. It finds X number of sprites and then creates copies of a single “example” grid / button combo in scene, hooks up delegates to the button and lets you click on them.
I appreciate the sample but I am using a grid and tilemap. From what I can see in other threads, you can’t add a component to a tile so I see some folks using the regular Tilemap.getTile() and then just calling a custom click handler on the Tile if extended. That of course allows for invoking what ou want but it still leaves the question of the larger strategy on how to manage many different tiles (with different capabilities) in a single grid with N tilemaps.
Is there a way I am not seeing to attach a button to a Tile instance? I tried playing around with creating a game object for a tile and setting it but was not able to get any clicks to register.
Clever… that’s so crazy that it might actually work!
The key to getting buttons to register I believe is a Graphic Raycaster and probably a Canvas above it all, if you’re talking about a UnityEngine.UI.Button
Might be able to make all the buttons share the same top level Canvas / Scaler / Raycaster…