- Home /
IPointer Events don't seem to be working
Hello, I've tried all of the different events and the don't seem to be working. I've been inheriting from IPointerClick Hander and implementing the interface but nothing seems to be working. Any ideas?
The code for reference:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System;
public class Slot : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked");
}
}
Answer by tanoshimi · Dec 11, 2016 at 12:39 PM
Is there an eventsystem in your scene? Does your camera have a physics raycaster on it? (Or does the canvas in which this element is contained have a graphic raycaster on it?)
Yeah, I have an EventSystem in the scene and the Canvas has a Graphics raycaster on it.
Answer by Pengocat · Dec 11, 2016 at 12:51 PM
Perhaps something is blocking the thing you are trying to click. When in play mode highlight the EventSystem and look at it in the inspector. If the name of the thing you want to click is not showing up then it is never called.
Answer by Bunny83 · Dec 11, 2016 at 01:32 PM
The GameObject where your script is attached to has to have another component attached that is derived from UI.Graphic. Like one of the built-in components: Image, RawImage or Text. Only GameObjects which have a Graphic attached can actually be interacted with since the GraphicsRaycaster, as the name suggests, can only detect Graphics.
Graphic is an abstract class. However it adds itself to a global Graphic registry which is used by the raycaster. So only GameObjects which are registrated in that manager can actually be "hit".
You can create a pure event target class like this:
//EventTarget.cs
using UnityEngine.UI;
public class EventTarget : Graphic
{
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
}
}
Just attach it to the same GameObject and you will receive your events. This is basically an empty Graphic as it does not generate any triangles / vertices. So this is a bit more efficient than using an Image with alpha set to 0 as it would still generate the quad and it would still be rendered.
Even though it's not necessary, usually you would derive UI components from UIBehaviour ins$$anonymous$$d of $$anonymous$$onoBehaviour. It doesn't really implement any functionality, but implements some virtual methods which can be overridden. If you use VisualStudio, just type "override" and a space inside your class body and it should list all methods that can be overridden.
That doesn't work but adding graphics raycaster to the ui does. For some reason my ui object has a "canvas" component attached to it and when I remove it my object is no longer visible. I didn't add it.
Um, any UI component has to be under a canvas object. Is the object you have your script attached to a child somewhere below a canvas object?
$$anonymous$$y test scene currently has this layout:
-Canvas
|
\-- $$anonymous$$yTestObject
-EventSystem
Where the Canvas object has the components:
RectTransform
Canvas
CanvasScaler
GraphicRaycaster
The EventSystem has those:
Transform
EventSystem
StandalongInput$$anonymous$$odule
And finally $$anonymous$$yTestObject has:
RectTransform
Your "Slot" script
$$anonymous$$y EventTarget script
CanvasRenderer (has been added automatically)
For me it works just fine. The canvas as well as the EventSystem has been generated by Unity automatically. If you remove the canvas and the eventsystem and just do Create->UI->Text or something similar it should create a canvas, but an object with a Text component as child and add the eventsystem to the scene.
I just removed the Text gameobject and created a new empty gameobject inside the canvas. I attached both scripts to it and positioned it properly on the screen so i can actually click on it. When i test the scene it works just fine.
I'm a bit confused. You said the object is no longer visible. "What" actually is visible? What other UI components do you use and how are they arranged? $$anonymous$$eep in $$anonymous$$d that the "IPointerClickHandler" and similar interfaces are only ment for 2d UI stuff. Can you post a screenshot of your sceneview / project panel so we can see your arrangement?