实现的功能比较简单,在Update中进行鼠标左键的检测,然后发射射线检测是否点击到目标层级的模型。
protected virtual void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f, LayerMask.GetMask("MouseClickObj")))
{
Debug.Log("click object name is " + hit.collider.gameObject.name);
}
}
}