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 /
  • Help Room /
avatar image
0
Question by hexagonius · Oct 26, 2015 at 10:25 PM · sizeui imagerecttransformaspect-ratio

Adjust RectTransform width to image that is scaled down

What I would like to know if the following is possible and how?

  • Create UI Canvas, with one GameObject and a Horizontal Layout Group on it

  • Add 2 GameObjects to it, one with any Text, one with an image

  • Enable keep aspect on that image

  • shrink the vertical size of the layout group down

The image will keep it's aspect, but it's containing RectTransform is as wide as the image would be if stretched. How do you get the RectTransform to uniformly adjust to the visual size of the image?

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
0

Answer by Addyarb · Oct 27, 2015 at 01:05 AM

It's sort of hard to understand what you're trying to do, but attach this script to your GameObject with the HZLayout on it.

Then:

  1. Enter Playmode and change regular to debug (dropdown/enum)

  2. Adjust parameters to get object where you want it

  3. Memorize/Copy parameters

  4. Exit Playmode

  5. Change/Paste parameters

  6. Make sure to turn debug mode back to regular

using UnityEngine; using System.Collections;

 public class SizeBasedOnScreenWidth : MonoBehaviour
 {
     public enum modes { regular, debug }
     public modes mode;
     public Vector2 screenPosition;
     Vector2 lastScreenSize;
     RectTransform rt;
     public float customScaleRatio = .001f;
     // Use this for initialization
     void Start()
     {
         if (customScaleRatio == 0) customScaleRatio = .001f;
         rt = GetComponent<RectTransform>();
         SetScreenSize();
     }
 
     void Update()
     {
         if (Screen.width != lastScreenSize.x || Screen.height != lastScreenSize.y || mode == modes.debug)
             SetScreenSize();
     }
     void SetScreenSize()
     {
         rt.anchoredPosition = new Vector2(Screen.width * screenPosition.x, Screen.height / screenPosition.y);
         rt.localScale = new Vector2(Screen.width * customScaleRatio, Screen.height * customScaleRatio);
         rt.localScale = new Vector2(rt.localScale.x, rt.localScale.x);
         lastScreenSize = new Vector2(Screen.width, Screen.height);
 
     }
 }


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 hexagonius · Oct 27, 2015 at 06:53 AM 0
Share

Thanks for that answer, but I'm looking for a solution that works with auto layout elements only. I don't want to adjust the content on a value basis.

Here's an image of what I meant in my question:

First image is more as expected, the image doesn't get any bigger as its actual size. But in the second image, the image is smaller but keeps its full width, the RectTransform does not adjust to the size, which means its not oriented on the right.

Full scale due to enough hight Lower scale but constant width due to lower hight

image-fullscale.png (87.6 kB)
image-halfscale.png (32.2 kB)

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

32 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

Related Questions

RectTransform Left Right Bottom Top 0 Answers

Drag and drop pivot point, Rect Transform 1 Answer

Layout Group failes to resize children automatically and can't force resize by script when adding child 1 Answer

Aspect ratio boxes floor 0 Answers

UI RectTransform.rect.width/heigth returns incorrect/negative values 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