- Home /
EventSystem - Call 'OnPointerClick' to EventSystem
Hello,
I'm making my own Input for 'OnPointerClick' - which is basically a series of keys pushed in sequence.
If complete, I want to then tell my EventSystem to call 'OnPointerClick'. I've looked at the ExecuteEvents, and the 'pointerClickHandler' but the documentation doesn't really give much information based on that. So I'm not really sure how to set it up.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PointerClickCustom : MonoBehaviour, IEventSystemHandler {
//tell EventSystem to trigger OnPointerClick within its EventSystem
void CallOnPointerClickEvent(){
EventSystem _current = EventSystem.current;
ExecuteEvents.pointerClickHandler();
}
}
Any advise would be greatly appreciated. Thanks
--- Update ---
I've created my own InputModule, and I've added to the EventSystem:
public class GestureInputModule : BaseInputModule
{
public GameObject m_TargetObject;
public override void Process(){
if (m_TargetObject == null)return;
ExecuteEvents.Execute (m_TargetObject, new PointerEventData (eventSystem), ExecuteEvents.pointerClickHandler);
}
}
The question now is, how do I connect this InputModule to my 'PointerClickCustom'. Thanks
Answer by Bunny83 · Nov 30, 2016 at 02:34 PM
If you want to provide your own input to the event system you should create your own Input module. Just look at the sample code. For OnPointerClick you want to pass the ExecuteEvents.pointerClickHandler instead of moveHandler. You also might need to pass an instance of PointerEventData instead of BaseEventData.
Don't pay too much attention to the example in the docs. Most examples make no sense.
So I can't just tell the EventSystem to call 'OnPointerClick', but I have to create a whole new Input $$anonymous$$odule? Thanks.
Answer by Feyyyyy · Nov 30, 2016 at 02:47 PM
There are some different ways of doing this. According to your purpose you need to select one of them.
If your purpose is just do some action on pointer event and position of the click is not important you can do something like this.
void Update() { if (Input.GetMouseButtonDown(0)) { CallOnPointerClickEvent(); } }
If you have a specific location relative to screen that you want user to click. You can do the following.
Instead of IEventSystemHandler use IPointerClickHandler Also the game object that your script attached have to be attached with a visible component from UnityEngine.UI package (ex: Image), and this gameobject have to be under a canvas
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PointerClickCustom : MonoBehaviour, IPointerClickHandler{
public void OnPointerClick(PointerEventData eventData) {
CallOnPointerClickEvent();
}
}
Hope this helps.
Hello, thanks for your reply - but this doesn't answer my question. I want to be able to 'inject' an OnPointClick event into my current EventSystem. Not to listen for one.