简体   繁体   中英

Unity3d EventTrigger - on pointer enter image

I am using eventTrigger to trigger the event when pointer enter an image. Basically, I have an image within the canvas . At the image I added EventTrigger component, add event type Pointer Enter , then link to a function of my script:

public void PointerEnter(){
    Debug.Log ("pointer enter");
}

This works fine. But what if I want to pass some data to the script? For example, the name of the image . In normal Unity3d raycast scenario, we can do this:

if (Physics.Raycast (ray0, out hit)) {
                Debug.Log (hit.collider.gameObject.name);//<-- get the name
            }

But how to do that if we use EventTrigger ?

Implement IPointerEnterHandler then override the OnPointerEnter() function which provides PointerEventData data as parameter. Get the name of the GameObject from that information.

Attach the simple MouseEnterScript script to your Canvas and it should detect all mouse enter on any UI.

using UnityEngine.EventSystems;

public class MouseEnterScript: MonoBehaviour, IPointerEnterHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Name: " + eventData.pointerCurrentRaycast.gameObject.name);
        Debug.Log("Tag: " + eventData.pointerCurrentRaycast.gameObject.tag);
        Debug.Log("GameObject: " + eventData.pointerCurrentRaycast.gameObject);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM