コード例 #1
0
        //
        private void PerformRaycast(UpdateEvent currentEvent)
        {
            if (currentEvent != updateEvent)
            {
                return;
            }

            if (cam == null)
            {
                return;
            }

            if (onHover == null)
            {
                return;
            }

            RaycastHit hitInfo;

            // Create a ray from mouse coords
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);


            // Targeting raycast
            if (Physics.Raycast(ray, out hitInfo, rayLength >= 0f ? rayLength : Mathf.Infinity, layerMask.value))
            {
                onHover.Invoke(hitInfo);
                //划出射线,只有在scene视图中才能看到
                Debug.DrawLine(ray.origin, hitInfo.point);
            }
        }
コード例 #2
0
 void Update()
 {
     if (!Cursor.visible)
     {
         if (m_Camera == null)
         {
             return;
         }
         if (OnHover == null)
         {
             return;
         }
         // 创建射线
         RaycastHit m_HitInfo;
         Ray        ray = m_Camera.ScreenPointToRay(Input.mousePosition);
         // 检测碰撞
         if (Physics.Raycast(ray, out m_HitInfo, m_RayLength >= 0f ? m_RayLength : Mathf.Infinity, m_LayerMask.value))
         {
             m_HitTarget = m_HitInfo.collider.gameObject;
             OnHover.Invoke(m_HitInfo);
             //划出射线,只有在scene视图中才能看到
             Debug.DrawLine(ray.origin, m_HitInfo.point);
         }
         else
         {
             m_HitTarget = null;
             //划出射线,只有在scene视图中才能看到
             Debug.DrawLine(ray.origin, ray.origin + ray.direction * m_RayLength);
         }
     }
 }