Search Unity

Recttransform.Overlaps

Discussion in 'UGUI & TextMesh Pro' started by elpuerco63, May 30, 2017.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I have a panel (background) that fills the entire canvas. Inside of it centred is another panel (container) which has space around it where the background is visible.

    I then add a button (myUIElement) as a child of container that can be moved around.

    I am trying to check if the button is moved outside of the container by using Recttransform.Overlaps() but from what I see as the button is in container which is itself in background it always reports that there is an overlap.

    Code (csharp):
    1.  
    2. RectTransform background;
    3. RectTransform myUIElement;
    4.  
    5.         if (myUIElement.rect.Overlaps(background.rect)) {
    6.      
    7.           // myUIElement has overlapped background
    8.        }
    9.        else {
    10.          
    11.            // myUIElement does not overlapped background
    12.        }
    13.  
    Is this the correct way to use this?

    I thought container would act as a block between itself and background?
     
  2. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I'm also trying to see if I can test has the button exited any part of the container but failing on that too :(
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    2 ideas that you may find suitable:
    1) if you're okay with semi precision of the IPointerEnter interface, you could use that to detect it entering the background area. By semi precise, I mean because if you drag from the bottom part of the button and just the top enters, that wouldn't register.
    2) If you know the dimensions of the rect for the inner panel (size + position), you could just check against that, to see if you're outside its limits.

    I'm sure there are probably more options.. like you could create 4 bounds (on the outter sides), just in code, and then check against those with your button.. This would be sort of similar to #2.
    You could use 'encapsulate' in some form... really reaching here. I mean, if you set the bounds to be the inner one, then use encapsulate and recheck the bounds to see if it grew..
    I may have gotten carried away ;)

    Anyways, hopefully one of those sounds helpful lol
     
    elpuerco63 likes this.
  4. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Thanks once again for offering advice and help :)

    Yeah, option 1 would not work as like you say if only the top enters then it would not register.

    I have been also trying the accepted answer here:http://answers.unity3d.com/question...nt-is.html?childToView=1359856#answer-1359856

    But I always get an overlap result, I think due to different coord systems?

    Now trying to see if it is better to check the child of the container has exited the containers rect, but all I get is overlap
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The accepted answer there sounds very similar to the second suggestion that I posted.

    I couldn't be sure I followed your train of thought with different coordinate systems, but if you mean like screen vs. world, then yep you'd want to convert.
     
  6. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Yeah, trying to figure that out. I see that the rect I am dragging is obtained by GetWorldCorners and then goes through a loop to test against the other rect which is in I local coordinates. But I dont see how I convert this rect to world and then use in the test loop.

    So lets say I have two images, square A in center of screen and a smaller square B inside square A. As I drag square B around I need to know has any part of it gone outside of square A's bounds

    The answer from that thread is close same as yours above but I see that I have to get square B's corners in world coords. But the test then uses them against square A that is in local (i presume)

    Been trying to figure out how to covert square A to world then use in the loop test...with little success
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I assume with the inner button/image that you'd take the position and adjust it based on its pivot.
    I can think of a simple way for you, if you don't mind adjusting the pivot (ie: you don't care about it for other calculations/adjustments).
    If you set the 3rd most inner object you have to upper left or bottom left (pivot).. you can then check very easily. I guess it's almost as easy to do with other pivot settings + math, sorry bear with me..
    Let's say we chose the bottom left. Now, let's say it's 20 in height. and the 2nd inner panel is 200 in height.
    At (0,0) we're at the bottom .. and anything negative (in local space) is outside.
    Anything higher than 180 means it's outside. Then similar comparisons for the right side, etc..
     
    elpuerco63 likes this.
  8. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Mm that sounds like a plan, but I have to keep the pivot of the container at centre for aspect ratio fitter and rotation purposes .. :(
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Right, well then you use 1/2 size (then scale if applicable) I think :) lol
     
  10. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    99.9% complete :)

    From http://orbcreation.com/orbcreation/docu.orb?1005 for which I had not realised had the method to handle exactly what I was after!!!

    Code (csharp):
    1.  
    2. Rect r1 = RectTransformExtensions.GetRect (panelOne.GetComponent<RectTransform> ());
    3.  
    4. Rect r2 = RectTransformExtensions.GetRect (childImageGetComponent<RectTransform> ());
    5.  
    6. if (!r1.Contains(r2)) {
    7.  
    8.       // do stuff when image not completely in panelOne          
    9. }
    10.  
    From http://www.voxelbusters.com/ extremely friendly and helpful crew which I have appended to the OrbCreationExtensions extension

    Code (csharp):
    1.  
    2. public static bool Contains (this Rect rect1, Rect rect2) {
    3.            
    4.             if ((rect1.position.x <= rect2.position.x) &&
    5.                 (rect1.position.x + rect1.size.x) >= (rect2.position.x + rect2.size.x) &&
    6.                 (rect1.position.y <= rect2.position.y) &&
    7.                 (rect1.position.y + rect1.size.y) >= (rect2.position.y + rect2.size.y)) {
    8.  
    9.                 return true;
    10.             }
    11.             else {
    12.  
    13.                 return false;
    14.             }
    15.         }
    16.  
    Trying to figure out how to deal with if image is rotated clockwise the test fails, but am so close

    Thanks
    methos5k
    VoxelBusters
    OrbCreationExtensions
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool -- you'll get there :)

    And you're welcome.. Enjoy your project.
     
    elpuerco63 likes this.