2D的UI:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class BaseMouseEvent : EventTrigger
{
public override void OnPointerEnter(PointerEventData eventData)//鼠标进入
{
base.OnPointerEnter(eventData);
//print("进入了");
}
public override void OnPointerExit(PointerEventData eventData)//鼠标移出
{
base.OnPointerExit(eventData);
//print("出来了");
}
public override void OnPointerClick(PointerEventData eventData)//鼠标点击
{
base.OnPointerClick(eventData);
}
}
3D物体(必须鼠标显示并碰到物体):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class InteractObj : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
private Outline OutlineS;
private void Awake()
{
OutlineS = GetComponent<Outline>();
OutlineS.enabled = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("移入!!");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("移出!!");
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("点击!!");
}
}
都需要有EventSystem
3D检测需要给摄像机添加Physics Raycaster组件