If I have many buttons in a canvas, I can simply use this:
button1.onClick.AddListener(() =>{doStuff();});
button2.onClick.AddListener(() =>{doStuff2();});
However, if I have images in the worldspace and I want to make them clickable I need to use the EventSystem, attach a physics2D raycaster to the main camera, and a Box Collider 2D to the image, and then have code like this:
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickAction : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
doStuff();
}
}
image.AddComponent<ClickAction>();
How can I do something like this
image1.AddComponent<ClickAction>(()=>{doStuff();});
image2.AddComponent<ClickAction>(()=>{doStuff2();});
That is, use the ClickAction class in a generic and reusable way? Thanks.