public static Rect Intersection(ref Rect rect1, ref Rect rect2)
{
if (rect1.width == 0 || rect1.height == 0 || rect2.width == 0 || rect2.height == 0)
return new Rect(0, 0, 0, 0);
float left = rect1.xMin > rect2.xMin ? rect1.xMin : rect2.xMin;
float right = rect1.xMax < rect2.xMax ? rect1.xMax : rect2.xMax;
float top = rect1.yMin > rect2.yMin ? rect1.yMin : rect2.yMin;
float bottom = rect1.yMax < rect2.yMax ? rect1.yMax : rect2.yMax;
if (left > right || top > bottom)
return new Rect(0, 0, 0, 0);
else
return Rect.MinMaxRect(left, top, right, bottom);
}