Is there an easy way to access global pointer events in the new UI eventsystem? Basically the items shown at runtime in the inspector for the EventSystem (pointer id, position, pointerEnter, pointerPress, lastPointerPress, pointerDrag). It seems like it should be easy since they are shown in the inspector but I can't find any way to access PointerEventData in a global way (only by implementing interfaces like IPointerClickHandler, etc. on a specific gameobject). Basically I'm trying to create a context menu that destroys itself if you click anywhere outside the context menu. This seemed like the best way to do it but if there is another way I should be approaching this I'm open to suggestions.
I found a suitable function in PointerInputModule: GetLastPointerEventData which returns a PointerEventData. However, it is protected so I can't call it. Is there any way around this?
If anyone else cares, I solved this by creating my own input module that inherits from StandaloneInputModule and has a public wrapper function for GetLastPointerEventData
I am trying to do the same thing. I'm very interested in hearing more about your solution. Any chance you could go into further detail?
Yeah, in case others need it, in a script on the object you need to have destroy itself: Code (CSharp): public void Update() { if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) { PointerEventData mouse1 = EventSystem.current.gameObject.GetComponent<StandaloneInputModuleCustom>().GetLastPointerEventDataPublic(-1); //if pointerPress is null (it was either a mouse2 click or it was outside any gui), or if pointerPress doesn't have my context menu component. if (mouse1.pointerPress == null || !mouse1.pointerPress.GetComponent<ContextMenuItemUI>()) { Destroy(this.gameObject); } } } new StandaloneInputModuleCustom (make sure to attach this script to your EventSystem object in the scene, and remove the standard StandaloneInputModule): Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class StandaloneInputModuleCustom : StandaloneInputModule { public PointerEventData GetLastPointerEventDataPublic (int id) { return GetLastPointerEventData(id); } } And then on the button itself I have it destroy its context menu container when it's clicked so the context menu goes away when you select an option: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class ContextMenuItemUI : MonoBehaviour, IPointerUpHandler { public ContextMenuUI parentMenu; public void OnPointerUp (PointerEventData eventData) { StartCoroutine(DestroyOnNextFrame()); } public IEnumerator DestroyOnNextFrame() { yield return null; Destroy(parentMenu.gameObject); yield break; } }
Hello, I'm currently trying to take a left click mouse down event and disable it for a frame, and then reenable it the next (without the user having to click the mouse button again. It seems like this code may help with my cause, but I can't quite determine how I can make this happen. I tried your code, but PointerEventData mouse1 always ends up being null even though I click on another UI element. Is this to be expected?
@smootler WOW! This right here...I didn't know you could actually expose Unity protected methods like that!