/// <summary> /// 判断是否在立方体内 /// </summary> /// <param name="position"></param> /// <param name="size"></param> /// <param name="targetPosition"></param> /// <param name="distanceType"></param> /// <returns></returns> public static bool CubeDistance(Vector3 position, Vector3 size, Vector3 targetPosition, DistanceType distanceType, out float distanceValue) { Distance(position, targetPosition, distanceType, out distanceValue); switch (distanceType) { case DistanceType.D3D: float d3d_xMin = position.x - size.x / 2; float d3d_xMax = position.x + size.x / 2; float d3d_yMin = position.y - size.y / 2; float d3d_yMax = position.y + size.y / 2; float d3d_zMin = position.z - size.z / 2f; float d3d_zMax = position.z + size.z / 2f; return(targetPosition.x.FloatContains(d3d_xMin, d3d_xMax) && targetPosition.y.FloatContains(d3d_yMin, d3d_yMax) && targetPosition.z.FloatContains(d3d_zMin, d3d_zMax)); case DistanceType.D2D: float xMin = position.x - size.x / 2; float xMax = position.x + size.x / 2; float yMin = position.y - size.y / 2; float yMax = position.y + size.y / 2; return(targetPosition.x.FloatContains(xMin, xMax) && targetPosition.y.FloatContains(yMin, yMax)); case DistanceType.DScreen: //计算两个坐标的屏幕坐标 Vector3 screen1 = MUtility.MainCamera.WorldToScreenPoint(position); Vector3 screen2 = MUtility.MainCamera.WorldToScreenPoint(targetPosition); //在计算范围 float sizeScreenX = Mathf.Abs(MUtility.MainCamera.WorldToScreenPoint(new Vector3(position.x + size.x / 2, position.y, position.z)).x - MUtility.MainCamera.WorldToScreenPoint(new Vector3(position.x - size.x / 2, position.y, position.z)).x); float sizeScreenY = Mathf.Abs(MUtility.MainCamera.WorldToScreenPoint(new Vector3(position.x, position.y, position.z)).x - MUtility.MainCamera.WorldToScreenPoint(new Vector3(position.x - size.x / 2, position.y, position.z)).x); return(MUtility.ScreenPointContains(screen1, new Vector2(sizeScreenX, sizeScreenY), screen2)); default: return(false); } }