0
\$\begingroup\$

I need to work with a Rect that has Left, Top, Bottom, Right.

Unity's Rect has x, y, width and height instead.

Also, Unity's Rect doesn't accept Rect.Compare(otherRect).

System.Windows.Rect however seems a good choice for my needs.

I would like to ask if it's ok to use System.Windows.Rect in my Unity script and if it will be cross-platform.

\$\endgroup\$
  • \$\begingroup\$ What have you tried? Where did your testing suggest that this could pose a problem? \$\endgroup\$ – DMGregory Feb 29 at 0:10
  • \$\begingroup\$ I took a look at the docs: docs.microsoft.com/de-de/dotnet/api/…. It showed "WindowsBase.dll", and I wondered what happens if my app runs on Android. I have not tried anything yet, \$\endgroup\$ – tmighty Feb 29 at 0:35
  • \$\begingroup\$ Test it on your target hardware, then post here if you determine there's a problem you need to solve. \$\endgroup\$ – DMGregory Feb 29 at 0:37
  • \$\begingroup\$ :-) It might take 5 years until I release my first game. But then I will post the results here. :-) \$\endgroup\$ – tmighty Feb 29 at 0:41
  • \$\begingroup\$ You don't need to release your game to do a test. All you need is a friend with an Android. \$\endgroup\$ – DMGregory Feb 29 at 0:41
2
\$\begingroup\$

It will not work as unity uses Mono and a subset of the core libraries, Windows is not going to be one of them.

Unity Rect has Contains and Overlaps to do logic tests for rects and points.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Instead of using System.Windows.Rect I'd just write extension methods:

public static int left(this Rect rect) {
    return rect.x - rect.width * 0.5f;
}
public static int right(this Rect rect) {
    return rect.x + rect.width * 0.5f;
}
public static int top(this Rect rect) {
    return rect.y + rect.height * 0.5f;
}
public static int bottom(this Rect rect) {
    return rect.y - rect.height * 0.5f;
}

And write my own comparison extension method:

public static bool isEqualTo(this Rect rect, Rect otherRect) {
    return rect.x == otherRect.x && rect.y == otherRect.y &&
            rect.width == otherRect.width && rect.height == otherRect.height;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.