unity判断点击区域是否在限定的矩形内

using UnityEngine;

namespace Gravity.Modules
{
    public static class PolygonUtility
    {
        public static float Cross(this Vector2 a, Vector2 b)
        {
            return a.x * b.y - b.x * a.y;
        }

        public static bool IsPointInRectangle(Vector2 P, Vector3[] rectCorners)
        {
            return IsPointInRectangle(P, rectCorners[0], rectCorners[1], rectCorners[2], rectCorners[3]);
        }

       
        public static bool IsPointInRectangle(Vector2 P, Vector2 A, Vector2 B, Vector2 C, Vector2 D)
        {
            Vector2 AB = A - B;
            Vector2 AP = A - P;
            Vector2 CD = C - D;
            Vector2 CP = C - P;

            Vector2 DA = D - A;
            Vector2 DP = D - P;
            Vector2 BC = B - C;
            Vector2 BP = B - P;

            bool isBetweenAB_CD = AB.Cross(AP) * CD.Cross(CP) > 0;
            bool isBetweenDA_BC = DA.Cross(DP) * BC.Cross(BP) > 0;
            return isBetweenAB_CD && isBetweenDA_BC;
        }

    }

 

你可能感兴趣的:(unity笔记)