目录
一、实现接口
二、内部方法
一、实现接口
使用该方法需要先完成一些准备工作。
① 在脚本中引用命名空间: using UnityEngine.EventSystems;
②在Main Camera 上加上Physics Raycaster组件
③添加EventSystem
④被点击的物体必须要带有collider
⑤在脚本中实现接口IPointerExitHandler,IPointerClickHandler等
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Test : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerClickHandler,IPointerDownHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("事件 物体被点击!!");
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("事件 鼠标按下");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("事件 鼠标进入物体 !!");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("事件 鼠标离开物体 !!");
}
}
使用该方法需要先完成一些准备工作。
① 在脚本中引用命名空间: using UnityEngine.EventSystems;
②在Main Camera 上加上Physics Raycaster组件
③添加EventSystem
④被点击的物体必须要带有collider
⑤在脚本中实现接口IPointerExitHandler,IPointerClickHandler等
准备
效果
二、内部方法
相比于上面的方法此方法更加的简单且更加适用于3D物体物体与鼠标事件的交互
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test2 : MonoBehaviour
{
private void OnMouseDown()
{
Debug.Log("鼠标按下");
}
private void OnMouseEnter()
{
Debug.Log("鼠标进入物体上");
}
private void OnMouseExit()
{
Debug.Log("鼠标离开物体");
}
private void OnMouseOver()
{
Debug.Log("鼠标放在物体上");
}
}
相比于上面的方法此方法更加的简单且更加适用于3D物体物体与鼠标事件的交互



