一、目的
1、想知道:Unity判断是否点击到3D物体
二、参考
1、Unity判断是否点击到UI、3D物体
https://blog.csdn.net/aiyan1111111/article/details/52815691
①总结:good:很好,能使用;but:移动端判断时候要写成#if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)负责会报错
三、操作:完成:检测Unity判断是否点击到UI、3D物体
1、
①下面是我修改代码全部在一个函数中,移动端检测能点击判断物体
/// <summary>
/// 功能:判断是否点击到蝴蝶
/// 参考:https://blog.csdn.net/aiyan1111111/article/details/52815691
/// </summary>
private void JudgeIsClickButterfly()
{
if (Input.GetMouseButtonDown(0))
{
#if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
//移动端判断如下
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
//PC端判断如下
if (EventSystem.current.IsPointerOverGameObject())
#endif
{
Debug.Log("当前点击在UI上:" + EventSystem.current.currentSelectedGameObject);
}
else
{
//从摄像机发出一条射线,到点击的坐标
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rayCastHit;
if (Physics.Raycast(ray,out rayCastHit, 100))
{
if (rayCastHit.collider.gameObject!=null)
{
Debug.Log("ClickObjict:" + rayCastHit.collider.gameObject);
string str = "ClickObjict:" + rayCastHit.collider.gameObject;
MyDebug.Add(str,"");
}
}
//显示一条射线,只有在scene视图中才能看到
Debug.DrawLine(ray.origin, rayCastHit.point, Color.red, 2);
}
}
}