Notes: Determine whether the UI is within the camera's field of view

 Determine whether the UI is within the camera's field of view

    /// <summary>
    /// 判断UI是否在摄像机视野内
    /// </summary>
    /// <param name="worldPos">UI坐标</param>
    /// <returns></returns>
    public bool IsInView(Camera camera, Vector3 worldPos)
    {
        Transform camTransform = camera.transform;
        Vector2 viewPos = camera.WorldToViewportPoint(worldPos);
        Vector3 dir = (worldPos - camTransform.position).normalized;
        float dot = Vector3.Dot(camTransform.forward, dir);//判断物体是否在相机前面  
        if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1) return true;
        else return false;
    }

Guess you like

Origin blog.csdn.net/weixin_43908355/article/details/124302926