Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 almo · Nov 24, 2014 at 10:45 PM · spriterecttransformaspect-ratio

How do I anchor sprite positions to an Image with "Preserve Aspect" set?

You can see in the image I have a background image with some trees, and it is set to Preserve Aspect. So even though the screen has been set to a resolution with the wrong aspect ratio for the image, it's still displaying properly.

The panel it's on is larger than the image because of this. My problem is that I want the objects in the scene (the bugs) to maintain their location relative to the image, not the panel.

The anchors care about the panel's size, not the image's size. Is there a way for me to get this to work?

alt text

alt text

I've tried using an AspectRatioFitter as suggested by one of the answers, and I get this. The blue dots show the size of the fitter is correct, but its anchors are in the same place as the parent RectTransform, which means it doesn't change the location behavior of the objects.

http://i23.photobucket.com/albums/b362/almo2001/Work/Untitled9_zpsdf7f2c1c.jpg

untitled6.png (157.6 kB)
untitled5.png (460.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by troien · Nov 24, 2014 at 11:03 PM

Give the image a AspectRatioFitter and set it to the aspect ratio of the image (The mode should be FitInParent). This way the actual RectTransform it's height/width are set to this aspect ratio.

Comment
Add comment · Show 8 · 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 almo · Nov 25, 2014 at 03:27 PM 0
Share

Ok, I have a panel with an image in it, and a gameobject with the Fitter as child of that panel. I set the AspectRatioFitter to FitInParent, and it seems to be setting its anchors according to the size of the RectTransform, not the image itself. So the image maintains aspect, but the AspectRatioFitter is putting its anchors in exactly the same place as the parent, which make its RectTransform larger than the image. :(

avatar image almo · Nov 25, 2014 at 04:27 PM 0
Share

This appears to be a bug in the AspectRatioFitter. It is supposed to maintain aspect ratio for its anchors, but it does not. I have reported it to Unity.

avatar image troien · Nov 25, 2014 at 04:32 PM 0
Share

If you set it to fit in parent, you can also give it a float value. Try to give it a value which is your image's width / height (in pixels)

avatar image almo · Nov 25, 2014 at 06:12 PM 0
Share

Yup, did that. Set it to 1.3333, which makes the blue dots go to the right place, but not the white arrows.

avatar image troien · Nov 26, 2014 at 04:40 PM 0
Share

I believe that is expected behaviour, as the AspectRatioFitter sets the RectTransforms width/height, so that the rect maintains the aspect ratio.

In case of FitInParent or Envelope Parent, it sets it's anchors to $$anonymous$$ 0,0 and max 1,1. (Because that is the size of the parent), and then changes w-dalta/h-delta values to make sure the aspect ratio is maintained.

Child components also look at the parent's rect (the area between the 4 blue dots). Not the parent's anchors! The anchors are only used to calculate the rect.

Show more comments

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

27 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

Related Questions

Sprite in different aspect ratio 0 Answers

Unity 2D targeting multiple screen sizes 1 Answer

Changing sprite at runtime causes it to stop resizing 1 Answer

Inconsistent Sprite location on screen 1 Answer

Handling Multiple Resolutions in 2D? 0 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