- Home /
How can use OnPointerClick in a generic way?
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.
Answer by hexagonius · Dec 09, 2018 at 12:58 PM
the Syntax would look something like this:
image1.AddComponent<ClickAction>().SetAction(doStuff)
;
ClickAction needs a public method, receiving either a System.Action or UnityEvent you save locally and call when the real OnClick is called.
Your answer
Follow this Question
Related Questions
Event System: Check which button has been clicked? 2 Answers
generic event system with scriptable objects 0 Answers
What good is a SortedList if GetKey doesn't work? 1 Answer
Accept multiple types via implicit operator as method argument 1 Answer
iOS JIT compile error on Generic method called with basic types (int, bool, float) 1 Answer