I have a very fundamental problem of enabling mouse cursor as a pointer. I have tried to write pointer enter events through a simple code, the pointer enter exit event work. But when I use Event Trigger's PointerEnter or PointerExit events, the actions are not fired. Do I have to enable something to activate my mouse as a Unity 3d Pointer?
1 Answers
PointerEnter and PointerExit for Unity UI:
Include
using UnityEngine.EventSystems;
at the top.Implement the
PointerEnter
andPointerExit
interface.Attach the Script to the Canvas of the UI.
PointerEnter and PointerExit for 3D Object/Model:
Include
using UnityEngine.EventSystems;
at the top.Implement the
PointerEnter
andPointerExit
interface.Attach the Script to the 3D Object and make sure that a
2D Collider
is attached to that 3D Object/Model.Select the
Camera
and attachPhysics Raycaster
to the camera.
PointerEnter and PointerExit for 2D Object/Sprite:
Include
using UnityEngine.EventSystems;
at the top.Implement the
PointerEnter
andPointerExit
interface.Attach the Script to the 2D Object and make sure that a
2D Collider
is attached to that 2D Object/Sprite.Select the
Camera
and attachPhysics 2D Raycaster
to the camera.
The code is the-same for all of these scenarios.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class ClickTester : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
}

- 2,218
- 22
- 32

- 121,791
- 22
- 236
- 328
-
I tested with 3d object with 2d collider is not working. But 3d object with 3d collider...it indeed works. thanks a lot – user6160538 May 26 '16 at 15:22
-
@user6160538 This I can tell you is the easiest solution out here. Create a script called ClickTester. Copy the code in my answer to the script then attach it to the Sprite since this is a 2D object. Attach `Physics 2D Raycaster` to the camera. Click play and click once in the middle of the GameView then you can now move your mouse over the Sprite and it should work. If that doesn't work then your project is not 2D but 3D. Create a new project and choose 2D this time. – Programmer May 26 '16 at 15:35
-
2Wow, this is the first thing I have seen that explains what you _actually have to do_. I have found it astoundingly difficult to understand the eventsystem so far but with this post it all makes sense. Thank you so much! :D – Clonkex Jan 05 '19 at 12:52