Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ragnaros100 · Jul 22, 2012 at 11:25 AM · c#rtsheightwidth

rect.Contains cant accept rects with a negative width and height?

To make this short, I have a classic rectangular RTS selection system. And I use the rect.Contains() function. But for some reason the rect.Contains() function does'nt work when the width or height of the rect is negative.

EDIT: here's an example code:

 Rect rect3 = new Rect(Screen.width / 2, Screen.height / 2, -200, -200); // this rect doesnt work
  GUI.Box(rect3, "");
     
  if(rect3.Contains(-Input.mousePosition))//still doesnt work
  {
  Debug.Log("inside"); 
  }
     
  Rect rect4 = new Rect(Screen.width / 2, Screen.height / 2, 200, 200); // this rect works 
  GUI.Box(rect4, "");
     
  if(rect4.Contains(Input.mousePosition))
  {
  Debug.Log("inside"); 
  }

Howcome? Is there any way to fix this?

EDIT: my point of inverting the rect is because i have a RTS like selection system. And I use the function rect.Contains() to determine what units that are selected. And if you drag from the down right corner. The width and height of the drawn rectangle will get negative.

Link to my next question.

-Thanks :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alexfeature · Dec 31, 2012 at 07:20 AM

Hey,

You can't have a rectangle with negative dimensions.

What you need to do is create a rectangle that has positive dimensions starting from the point where your mouse is to the point where the drag operation started.

Basically, when your mouse X and / or Y are negative you will be redrawing the rectangle FROM CURRENT location of your mouse pointer to the START OF DRAG location.

In cases when they are positive then the logic stays as you have it since your are drawing the rectangle FROM START OF DRAG location TO CURRENT mouse position.

I hope this helps, Alex

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

Answer by Seth-Bergman · Jul 22, 2012 at 11:34 AM

if you are using mouse position, as I suspect, as in:

if (rect.Contains(Input.mousePosition))

the x and y of Input.mousePosition go from 0 to Screen width/height

Using GUITextures would be better, as they too use the same screen coordinates

EDIT : yup, as I figured..

just keep in mind Input.mousePosition goes from (0,0) in the lower left of screen to (Screen.width,Screen.height) in the upper right

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ragnaros100 · Jul 22, 2012 at 11:37 AM 0
Share

this is just an example. $$anonymous$$y question is: Is there any way to make a rect with a negative width and height. AND get the funciton rect.Contains() to work with it?

avatar image ragnaros100 · Jul 22, 2012 at 11:40 AM 0
Share

to the edit:

Yea. I already knew that. thanks. But this did'nt answer my question

avatar image Seth-Bergman · Jul 22, 2012 at 11:45 AM 0
Share

then you should realize that you need to convert the ARGU$$anonymous$$ENT you are passing to something that represents the actual position of your Rect.

for example: if (rect.Contains(-Input.mousePosition))

boom. negative.

but then your Rect has to have negative values not less than -Screen.width and -Screen.height,

or you can't reach it.. but say

if (rect.Contains(-Input.mousePosition * 2))

now it could be twice as negative, get it?

you don't need to use Input.mousePosition, it could be any point. (Vector2 or Vector3)

p.s. mousePosition is a Vector3, and rect.Contains takes a point so the above code is not literal.. just making the point..

in other words, just pass in a negative Vector3/Vector2

avatar image ragnaros100 · Jul 22, 2012 at 11:58 AM 0
Share

Nope. still doesnt work. (try and look at the example code.. the rect called rect3 doesnt work because the width and height of it are negative numbers)

avatar image Seth-Bergman · Jul 22, 2012 at 12:06 PM 0
Share

Ah ok my mistake after all.. Assumed you were using a negative position.. What possible reason could you have for declaring a rectangle with a negative width/height?

even if it's technically possible somehow, what's the point?

EDIT: ok , I see that making width and height negative invert the Rect. Still don't see much point though..

good luck anyway, sorry for the confusion

Show more comments
avatar image
0

Answer by Seth-Bergman · Jul 22, 2012 at 12:30 PM

Now that I understand the question, here's your answer:

http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html

A Rectangle whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle is treated as non-existant along those axes. Such a Rectangle is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false.

etc...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Ash-Blue · Jul 20, 2019 at 09:13 PM

You're completely correct. Unity's Rect class has no math to handle negative sizes. It jacks up any rectangle helper methods. It's a minor annoyance you can easily fix with this helper script I've written.

 using UnityEngine;
 
 namespace CleverCrow.Fluid.Dialogues.Editors {
     public static class RectCleaner {
         public static Rect FixNegativeSize (this Rect rectOld) {
             var rect = new Rect(rectOld);
 
             if (rect.width < 0) {
                 rect.x += rect.width;
                 rect.width = Mathf.Abs(rect.width);
             }
 
             if (rect.height < 0) {
                 rect.y += rect.height;
                 rect.height = Mathf.Abs(rect.height);
             }
 
             return rect;
         }
     }
 }
   

To use it you'll need to call it like this. It's non-destructive so it returns a value.

 var myRect = new Rect(0, 0, -20, -20);
 var cleanedRect = myRect.FixNegativeSize();
 // cleanedRect can now be used normally
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find width and height of game object, Unity2D 2 Answers

Having an issue with setting the minimum size for a field size 0 Answers

Unity not dividing correctly C# camera pixel width & height 1 Answer

How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges