UGUI射线检测北京PK10平台出租与普通物体检测

UGUI物体检测与北京PK10平台出租QQ2952777280【话仙源码论坛】hxforum.com【木瓜源码论坛】papayabbs.com,普通物体检测有所不同(UGUI射线检测已经被封装好):

1.Graphic Raycaster

主要用于UI上的射线检测,挂有这个组件的物体,必须要挂上Canvas这个组件(当挂上Graphic Raycaster时Canvas也会自动挂上)。

Ignore Reversed Graphics:是否忽略反方向的图形,如果为true,则表示图形正面展示时,会接收到射线检测;反面展示时,不会接收到射线检测;否则,正反面展示都会接收到射线检测

Blocking Objects:屏蔽指定类型的(物理)对象,使它们不参与射线检测。渲染模式不为ScreenSpaceOverlay时起作用。可选值为:
None:不屏蔽任何物理对象
Two D:屏蔽2D物理对象(即具有2D碰撞体的对象)
Three D:屏蔽3D物理对象(即具有3D碰撞体的对象)

All:屏蔽所有物体对象

Blockin Mask:使屏蔽对象中的指定层不参与射线检测。渲染模式不为ScreenSpaceOverlay时,且Blocking Objects不为None时起作用。
例如,Blocking Objects为2D,Blocking Mask为UI,指2D物理对象中是UI层的东西都不会接收射线检测,而2D物理对象中的其他层还是能接收射线检测

2.Physics Raycaster & Physics2D Raycaster

主要用于物理对象上的射线检测,挂有这个组件的物体,必须要挂上Camera这个组件(当挂上组件时Camera也会自动挂上)。当物体(2D或者3D物理对象)受到射线检测时,物体上的实现了事件接口的方法会被调用
Physics Raycaster :只会对3D物体对象产生影响,Camera的Projection,尽量选为Perspective(透视)模式
Physics2D Raycaster:只会对2D物体对象产生影响,Camera的Projection,尽量选为Orthography(正交)模式
EventMask:射线作用层,可以用来做事件屏蔽

此处为主要代码:

//此处检测出UGUI物体代码
bool CheckGuiRaycastObjects()
{
    PointerEventData eventData = new PointerEventData(eventSystem);//与指针(鼠标/触摸)事件相关的事件负载。(PointerEventData)

    //指针位置
    eventData.pressPosition = Input.mousePosition;
    eventData.position = Input.mousePosition;//当前指针位置。

    List<RaycastResult> list = new List<RaycastResult>();
    graphicRaycaster.Raycast(eventData, list);//如同Physics.Raycast(ray, out hit)绑定相应的射线检测信息
    Debug.Log(list.Count);
    if (list.Count > 0)
    {
        foreach (RaycastResult result in list)
        {
            print(result.gameObject.name);//输出相应的Ui物体
        }
    }
    return list.Count > 0;
}

//此处检测全部为游戏物体
void update_()
{
    if (CheckGuiRaycastObjects()) return;
    Debug.Log(EventSystem.current.gameObject.name);

    //此处检测全部为游戏物体
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            print(hit.collider.gameObject.name);//输出游戏物体
        }
    }
}

//此处代码可以判断鼠标点击的是UI物体还是游戏物体
//EventSystem.current.IsPointerOverGameObject()该函数如果是UI我替返回true不是返回false
void jiance_wuti()
{
    if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
    {
        #if UNITY_ANDROID || UNITY_IPHONE
        if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
        #else
        if (EventSystem.current.IsPointerOverGameObject())//判断是否鼠标点击到了游戏物体
        #endif
            print("当前触摸在UI上"+ EventSystem.current.IsPointerOverGameObject());
        else
            print("当前没有触摸在UI上"+EventSystem.current.IsPointerOverGameObject());
    }
}

}

猜你喜欢

转载自blog.51cto.com/13907340/2154105
今日推荐