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 H3ll0VR · Jan 24, 2019 at 12:22 PM · uisizeui imageanchor

Help resizing UI at runtime

I have a frame, and then a child of that frame is a photo which is taken at runtime. I need to resize the frame to fit the photo. I've tried the content size fitter but it didn't work or perhaps I'm not using it correctly. Can someone please tell me what components/anchors I need to apply to achieve what I want? Here is an image of it now, and I want the white frame to be snug around the image. alt text

screen-shot-2019-01-24-at-121925.png (184.3 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

2 Replies

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

Answer by H3ll0VR · Jan 24, 2019 at 01:18 PM

The aspect ratio fitter component alongside the code below has solved all my problems.

  Vector2 imageSize = new Vector2(GetComponent<Image>().sprite.texture.width,
                                         GetComponent<Image>().sprite.texture.height);
         float imageRatio = imageSize.x / imageSize.y;
 
         GetComponentInParent<AspectRatioFitter>().aspectRatio = imageRatio;
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 xxmariofer · Jan 24, 2019 at 12:32 PM

Click in the anchor button where you can set different predefined anchors, then press alt key and shift key and select the down right corner one alt text


sin-tituloaa.png (9.9 kB)
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 H3ll0VR · Jan 24, 2019 at 12:39 PM 0
Share

Unfortunately this isn't what I need to do. The photo has a locked aspect. I should have made it clearer in my original post but I believe I need a script which resizes the frame using ratios and the size of the image. Here's what I have currently, with the debugs outputting (-46.6, -70), (-46.6, -70) and (642.0, 964.0) respectively but the image ends up negatively sized:

 private void OnEnable()
     {
         Vector2 frameDelta = GetComponentInParent<RectTransform>().sizeDelta;
         Vector2 imageDelta = GetComponent<RectTransform>().sizeDelta;
         Vector2 imageSize = new Vector2(GetComponent<Image>().sprite.texture.width,
                                         GetComponent<Image>().sprite.texture.height);
 
         Debug.Log("Frame Delta: " + frameDelta);
         Debug.Log("Image Delta: " + imageDelta);
         Debug.Log("Image Size: " + imageSize);
 
         float imageRatio = imageSize.x / imageSize.y;
 
         frameDelta.x = frameDelta.y * imageRatio;
 
         imageDelta.x = imageSize.x;
         GetComponent<RectTransform>().sizeDelta = imageDelta;
 
         GetComponentInParent<RectTransform>().sizeDelta = frameDelta;
 }

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

166 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 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

Unity UI constant physical size one image 0 Answers

How to use my own image for my UI Slider's Background 2 Answers

UI - Pixel perfect pattern overlay 0 Answers

Image custom fillOrigin 1 Answer

Instanciate prefab responsively 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