通过RaycastAll获取hits
遍历hits时通过MeshRender的边界来确定是否有选中该物体
组件绑相机上就可以了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ChooseExactly : MonoBehaviour
{
private Ray ray;
private RaycastHit[] hits;
private Bounds bounds;
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);//定义射线,定位到鼠标的位置
hits = Physics.RaycastAll(ray, Mathf.Infinity);
for (int i = 0; i < hits.Length; i++)
{
bounds = hits[i].transform.GetComponent<MeshRenderer>().bounds;
if (bounds.IntersectRay(ray))
{
Debug.Log(i+","+hits[i].transform);
break;
}
}
}
}
}