Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Neolith998 · Sep 13, 2016 at 06:55 PM · uiguiimageui imagerecttransform

Check if UI element is fully inside another UI element

Hey everyone, I'm doing a gridless inventory system, where you can freely move around items inside a container. The problem I'm facing is checking if an item (image) is completely inside the container (another image). How would I do this? I've tried getting each corner of the item image and then using the Contains function to check if the corners are inside the container, but for some reason that didn't work. Here's a snippet of the code that I've used to attempt to achieve this:

 for (int c = 0; c < invContainers.Count; c++)
         {
             if (RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), Input.mousePosition))
             {
                 Vector3[] corners = new Vector3[4];
                 obj.transform.GetChild(1).gameObject.GetComponent<RectTransform().GetLocalCorners(corners);
 
 
                 if (invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>().rect.Contains(corners[0])
                 && invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>().rect.Contains(corners[1])
                 && invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>().rect.Contains(corners[2])
                 && invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>().rect.Contains(corners[3]))
                 {
                     Debug.Log("Can drop the item in ID " + c);
                     obj.transform.SetParent(invContainers[c].obj.transform.GetChild(2));
                     obj.GetComponent<RectTransform>().localScale = Vector2.one;
                     obj.GetComponent<RectTransform>().position = tempCoords;
                 }
                 else
                 {
                     Debug.Log("Can't drop here");
                 }
             }
         }


Using this code, I seem to pass through this check even if only 1 corner is inside the container (it's almost like it ignores the corner check). If anyone has any suggestions or a solution, please share them with me, I will greatly appreciate them. Thank you in advance.

EDIT: Fixed by using RectangleContainsScreenPoint instead of rect.Contains. Here's the working code below:

 for (int c = 0; c < invContainers.Count; c++)
         {
             if (RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), Input.mousePosition))
             {
                 Vector3[] corners = new Vector3[4];
                 obj.transform.GetChild(1).gameObject.GetComponent<RectTransform>().GetWorldCorners(corners);
 
                 if (RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), corners[0])
                     && RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), corners[1])
                     && RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), corners[2])
                     && RectTransformUtility.RectangleContainsScreenPoint(invContainers[c].obj.transform.GetChild(2).gameObject.GetComponent<RectTransform>(), corners[3]))
                 {
 
                         Debug.Log("Can drop the item in ID " + c);
                         obj.transform.SetParent(invContainers[c].obj.transform.GetChild(2));
                         invContainers[c].items.Add(obj.GetComponent<InventoryItem>().item);
                         obj.GetComponent<RectTransform>().localScale = Vector2.one;
                         obj.GetComponent<RectTransform>().position = tempCoords;
                         obj.GetComponent<InventoryItem>().containerID = c;
                         calculateWeight();
                         UpdateContainers();
 
                 }
                 else
                 {
                     Debug.Log("Can't drop here");
                     obj.GetComponent<InventoryItem>().ResetPos();
                 }
             }
         }
Comment
Add comment · Show 3
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 Drakonno · Sep 13, 2016 at 11:33 PM 2
Share

I'm not sure, but there can be a few things to check.

First, if You are checking against proper camera here: RectangleContainsScreenPoint Second, if Vector3[] corners has proper Z depth for each of them. Third, if proper objects are taken from GetChild Fourth, debug, Your corners of object and containrer. Display them using Debug.Log or in some other, nice way.

Personally I would use BoxColliders for walls of the container, and volume of objects, and check collisions of them. Also do something like: if object collides with container wall, move it to closest, empty space of container.

avatar image Drakonno · Sep 15, 2016 at 12:31 PM 0
Share

Please, provide information what have You done to resolve Your problem. Someone, will probably help with further problems about this topic.

If my previous comment helped enough, please inform, I'll turn it into an answer.

It's sad to see many questions left for death. :(

avatar image Neolith998 Drakonno · Sep 15, 2016 at 03:46 PM 0
Share

Thanks for your help, I already fixed it by using RectangleContainsScreenPoint ins$$anonymous$$d of rect.Contains

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dbdenny · Mar 24, 2020 at 02:19 AM

 public static bool RectContainsAnother (RectTransform rct, RectTransform another)
 {
     var r = rct.GetWorldRect();
     var a = another.GetWorldRect();
     return r.xMin <= a.xMin && r.yMin <= a.yMin && r.xMax >= a.xMax && r.yMax >= a.yMax;
 }
Comment
Add comment · Show 1 · 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 Davon92 · Jun 19, 2021 at 06:02 PM 0
Share

Is GetWorldRect a 3rd party extension or within unity docs?

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

103 People are following this question.

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

Related Questions

Matching the size of UI elements with that of world objects 0 Answers

Display loaded UI Image in Container with native size 0 Answers

How to move UI Image under the other UI elements? 3 Answers

Saving image in a variable purely through code? 1 Answer

How to copy one Rect Transform data to other Rect Transform 2 Answers


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