1.先是EventSystem在Update中调用当前输入模块InputModules的Process方法处理所有的鼠标事件,
2.并且输入模块会调用RaycastAll来得到目标信息,
3.通过子物体未挂载IEventSystemHandler,再找父物体方式找到事件实际接收者并执行点击事件
创建一个Button,那这个Button还包含了Text组件,如果text.RaycastTarget勾上
当鼠标点击的时候会调用GetEventHandler函数,
该函数的root参数其实是Text,发现text无IEventSystemHandler组件
但是会查找到它的父物体Button,发现有,然后调用Button的点击事件
核心问题:text缺少IEventSystemHandler
public class Button : Selectable, IPointerClickHandler, ISubmitHandler
public interface IPointerClickHandler : IEventSystemHandler
点击button,先响应text,再查找到button,handlerCount为0,说明无IPointerClickHandler组件
text加上EventTrigger,会响应text的点击事件,不会向上响应button
text挂脚本实现IPointerClickHandler 接口OnPointerClick
IsPointerOverGameObject是射线接触到的UI有RaycastTarget
public override bool IsPointerOverGameObject(int pointerId)
{
var lastPointer = GetLastPointerEventData(pointerId);
if (lastPointer != null)
return lastPointer.pointerEnter != null;
return false;
}
if (Input.GetMouseButtonDown(0))
{
if (Application.isMobilePlatform && Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
{
m_isClickUi = true;
break;
}
}
}
else if (EventSystem.current.IsPointerOverGameObject())
{
m_isClickUi = true;
}
}
EventSystem会在Update中调用输入模块PointerInputModule的Process方法来处理输入消息
PointerInputModule会调用EventSystem中的RaycastAll方法进行射线检测
RaycastAll又会调用BastRaycaster的Raycast方法执行具体的射线检测操作,主要是获取被选中的目标信息。
Polygon Collider 2D
不用的时候不勾选2d,3d,在update有性能消耗。默认是none
if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All)
{
if (ReflectionMethodsCache.Singleton.raycast3D != null)
{
var hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, (int)m_BlockingMask);
if (hits.Length > 0)
hitDistance = hits[0].distance;
}
}
if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All)
{
if (ReflectionMethodsCache.Singleton.raycast2D != null)
{
var hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, (int)m_BlockingMask);
if (hits.Length > 0)
hitDistance = hits[0].distance;
}
}
1.不需要点击事件的可以不勾选RaycastTarget
2.封装点击按钮带参数
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler,IPointerUpHandler
{
public delegate void VoidDelegate(GameObject go);
public VoidDelegate onClick;
public VoidDelegate onPress;
public VoidDelegate onNewGuideClick;
static public ClickListener Get(GameObject go)
{
ClickListener listener = go.GetComponent<ClickListener>();
if (listener == null) listener = go.AddComponent<ClickListener>();
return listener;
}
public void OnPointerClick(PointerEventData eventData)
{
if (onClick != null)
{
onClick(gameObject);
}
}
bool ispress;
public void OnPointerDown(PointerEventData eventData)
{
ispress = true;
}
public void OnPointerUp(PointerEventData eventData)
{
ispress = false;
}
void Update() {
if (onPress != null && ispress)
onPress(gameObject);
}
}