private void Update()
{
// Clear some memory
m_HandlerHits.Clear();
// Raycast
Vector3 mouseScreenPosition = Input.mousePosition;
Vector3 mouseWorldPosition = m_Camera.ScreenToWorldPoint(mouseScreenPosition);
Ray ray = m_Camera.ScreenPointToRay(mouseScreenPosition);
RaycastHit[] hits = Physics.RaycastAll(ray);
// Loop through the hits
foreach (RaycastHit hit in hits.OrderBy(h => h.distance))
{
// Check if it matches any of the touch handlers
foreach (TouchHandler handler in m_Handlers)
{
if (hit.collider.gameObject == handler.Owner)
{
m_HandlerHits.Add(handler);
}
}
}
foreach (TouchRawType rawType in Enum.GetValues(typeof(TouchRawType)))
{
TouchEvent touchEvent = new TouchEvent(mouseScreenPosition, mouseWorldPosition, rawType);
// On touch down
if (Input.GetMouseButtonDown((int)rawType))
{
foreach (TouchHandler handler in m_HandlerHits)
{
handler.OnTouchDown(touchEvent);
if (handler.KeepTouches)
{
break;
}
}
}
if (Input.GetMouseButton((int)rawType))
{
// On touch drag
foreach (TouchHandler handler in m_Handlers)
{
if (handler.HasClaimedTouch(rawType))
{
handler.OnTouchDrag(touchEvent);
}
}
}
if (Input.GetMouseButtonUp((int)rawType))
{
// On touch up
foreach (TouchHandler handler in m_Handlers)
{
if (handler.HasClaimedTouch(rawType))
{
handler.OnTouchUp(touchEvent);
}
}
}
}
}