用的unity版本为2021.1.11 URP,鼠标点击模型的OnMouseEnter失效
1、创建脚本,绑定在模型对应相机上
public class LiveCameraController : MonoBehaviour
{
Camera cam;
private IMouseEvent current;
void Start()
{
cam = GetComponent<Camera>();
}
void Update()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.GetComponent<IMouseEvent>() != null)
{
if (current == null)
{
current = hit.transform.GetComponent<IMouseEvent>();
current.OnMouseHover(true);
}
else
{
if (hit.transform.GetComponent<IMouseEvent>() != current)
{
current.OnMouseHover(false);
current = hit.transform.GetComponent<IMouseEvent>();
current.OnMouseHover(true);
}
}
}
else
{
if (current != null)
{
current.OnMouseHover(false);
current = null;
}
}
}
else
{
if (current != null)
{
current.OnMouseHover(false);
current = null;
}
}
}
}
2、需要鼠标事件的物体脚本上继承接口IMouseEvent即可
|