如何防止UGUI上的UI被射穿

    bool CheckGuiRaycastObjects()//测试UI射线

    {

        PointerEventData eventData = new PointerEventData(eventSystem);

        eventData.pressPosition = Input.mousePosition;

        eventData.position = Input.mousePosition;

        List<RaycastResult> list = new List<RaycastResult>();

        RaycastInCanvas.Raycast(eventData, list);

        //Debug.Log(list.Count);

        return list.Count > 0;

}

上面的方法通过在Update内判断Bool值,为真则直接返回。上面的方法可能导致寻路走到一半卡住。


下面的方法可以通过修改逻辑来控制。

需要引用UnityEngine.EventSystem

void Update ()
{
    if (EventSystem.current.IsPointerOverGameObject ())
        Debug.Log ("当前触摸在UI上");
    else
        Debug.Log ("当前没有触摸在UI上");
}   

另外:Graphics Raycaster的Raycast是个虚函数,可以写个Graphics Raycaster的派生类,在默认的Raycast操作执行完以后,用自定义的layer进行筛选,把不需要响应的gameobject去掉。

posted @ 2017-08-08 21:42  萌新小L  阅读(223)  评论(0编辑  收藏  举报