UI被穿透的解决方法

亲测有效

public class DialogCtr:MonoBehaviour
{
//默认为true,可发射射线
private bool shoot=true;
Transform target;

void GetTarget()
{
 if ( Input.GetMouseButtonDown(0))
			{
                //点击到UI或者点击到UI以外的范围,但UI处于显示状态,不执行射线
                if (EventSystem.current.IsPointerOverGameObject()||GameObject.Find("2_Tip").transform.Find("2_Tip_1").gameObject.activeInHierarchy)
                {
                    //检测到UI,不执行射线
                    shoot = false;
                }
                else
                {
                    //没有检测到UI,执行射线
                    shoot = true;
                }

                //是否发射射线
                if (shoot)
                {
                    RaycastHit hitInfo;
                    //发射射线
                    if (Physics.Raycast(myCamera.ScreenPointToRay(Input.mousePosition), out hitInfo))
                    {
                        //物体可高亮,移动,掉落
                        if (hitInfo.transform.GetComponent<Highlighter>() != null && hitInfo.transform.GetComponent<Rigidbody>() != null)
                        {
                            target = hitInfo.transform;
                            //不受重力影响
                            target.GetComponent<Rigidbody>().isKinematic = true;
                        }
                    }
                    else
                    {
                        target = null;
                    }
                }      
            }
  }
  }

猜你喜欢

转载自blog.csdn.net/qq_41211080/article/details/84402884