Rect.Intersect(Rect, Rect) is a .Net function, so it should work, right? Is there a file I need to include like I do when I use lists? It says Rect.Intersect is in the System.Windows namespace, but doesn't say what class.
UnityScript: Code (csharp): import System.Drawing; function IntersectRect(){ var r1 = new Rectangle(make, your, rectangle, here); var r2 = new Rectangle(make, another, rectangle, here); var intersected = Rectangle.Intersect(r1, r2); //bob's your uncle! } C#: Code (csharp): using System.Drawing; void IntersectRect(){ Rectangle r1 = new Rectangle(make, your, rectangle, here); Rectangle r2 = new Rectangle(make, another, rectangle, here); Rectangle intersected = Rectangle.Intersect(r1, r2); //bob's your uncle! } Standard typed-in-browser disclaimer.
Yeah, there isn't any System.Drawing in Unity. It would be pretty straightforward to make your own function that does the same thing though. Unity uses Mono rather than .NET, and a subset of Mono at that. Also the Rect struct in Unity is a custom struct specific to Unity, not the one from .NET. (Among other things, it uses floats rather than doubles.) --Eric
I'm really curious why the Rect class in Unity is so under-featured. Intersection testing, and computation of union/intersection, are basic functions of any rectangle class. Sure, it doesn't take long for us each to write our own, but multiply that by the (roughly) bajillion Unity devs out there, and it adds up to some serious wasted time.
Code (csharp): using UnityEngine; public static class UnityFix { public static bool Intersect(this Rect rectA, Rect rectB) { return ( Mathf.Abs(rectA.x - rectB.x) < (Mathf.Abs(rectA.width + rectB.width) / 2)) (Mathf.Abs(rectA.y - rectB.y) < (Mathf.Abs(rectA.height + rectB.height) / 2)); } }
I tried the above code, but it didn't seem to work. This did: Code (csharp): public static bool Intersect( Rect a, Rect b ) { bool c1 = a.x < b.xMax; bool c2 = a.xMax > b.x; bool c3 = a.y < b.yMax; bool c4 = a.yMax > b.y; return c1 c2 c3 c4; } Note that the above code doesn't work if your Rects have a negative width. If you want one that accepts any rects, you can use this: Code (csharp): public static bool Intersect( Rect a, Rect b ) { FlipNegative( ref a ); FlipNegative( ref b ); bool c1 = a.xMin < b.xMax; bool c2 = a.xMax > b.xMin; bool c3 = a.yMin < b.yMax; bool c4 = a.yMax > b.yMin; return c1 c2 c3 c4; } public static void FlipNegative(ref Rect r) { if( r.width < 0 ) r.x -= ( r.width *= -1 ); if( r.height < 0 ) r.y -= ( r.height *= -1 ); }
if you just need a bool https://docs.unity3d.com/Documentation/ScriptReference/Rect.Contains.html https://docs.unity3d.com/Documentation/ScriptReference/Rect.Overlaps.html
If we're requesting "functions that should exist in the Rect class but don't", can we also get a Rect.Lerp?
Code for Union of 2 Rects: Code (CSharp): public static void RectUnion(ref Rect RA, ref Rect RB, out Rect RUnion) { RUnion = new Rect(); RUnion.min = Vector2.Min(RA.min, RB.min); RUnion.max = Vector2.Max(RA.max, RB.max); }