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
2
Question by esitoinatteso · May 28, 2015 at 11:30 PM · uipositionscreenruntimerecttransform

UI RectTransform Position && Screen Resolution

Hi there! I've noticed that the cloned UI images that I instantiate at runtime are misplaced.

Here's what I have

I've got Prefabs that are instantiated via script at runtime at a specific position relative to a parent ( which is a child of the main canvas). Each cloned Prefab is instantiated to the parent's position, plus the width of all previous clones. These prefabs are RectTransform with anchor set to center ( setting pivot and/or position too caused an error about threading and UI), the parent is an empty RectTransform. Canvas' Canvas Scaler is set to scale with screen size + match width or height. Here's a chunk of code:

             clone.transform.position = position; // a Vector defined outside the loop
             slicesRow.Add(clone); //just caching them inside a list
             position += new Vector3(clone.GetComponent<RectTransform>().sizeDelta.x,0,0);

Here's what I'd like to do

I'd like that my clones kept their right positions when playing with a Screen Resolution different than the one expected.

Here's what I get

Screen Size expected = ok:

Screen Size expected

Screen Size other = blargh:

alt text

Thanks for your time, I'm kinda clueless right now and I appreciate your attention

P.S.: about this line ( which is the cause of my problem I guess)

 position += new Vector3(clone.GetComponent<RectTransform>().sizeDelta.x,0,0);

I know it's better to cache that float value instead of calling it every time, I just don't see the point of optimising a chunk of code that doesn't get the job done ;)

screen-shot-2015-05-29-at-010659.png (35.9 kB)
screen-shot-2015-05-29-at-010732.png (9.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
8
Best Answer

Answer by jmorhart · May 29, 2015 at 12:51 AM

I came across this problem as well. If you are going to change a RectTransform's position or localPosition values you have to scale those values by the CanvasScaler's referenceResolution. Here's how I do it:

     private CanvasScaler canvasScaler;

     private Vector2 ScreenScale
     {
         get
         {
             if (canvasScaler == null)
             {
                 canvasScaler = GetComponentInParent<CanvasScaler>();
             }
 
             if (canvasScaler)
             {
                 return new Vector2(canvasScaler.referenceResolution.x / Screen.width, canvasScaler.referenceResolution.y / Screen.height);
             }
             else
             {
                 return Vector2.one;
             }
         }
     }

I'm not 100% sure what you're trying to do, but I think this would work:

 position += new Vector3(clone.GetComponent<RectTransform>().sizeDelta.x * ScreenScale.x,0,0);

Comment
Add comment · Show 2 · 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 esitoinatteso · May 30, 2015 at 02:26 PM 1
Share

Hi jmorhart and thanks a lot for your answer! It works like a charm! There's just one thing though, I think that this:

 position += new Vector3(clone.GetComponent<RectTransform>().sizeDelta.x * ScreenScale.x,0,0);

should be:

 position += new Vector3(clone.GetComponent<RectTransform>().sizeDelta.x / ScreenScale.x,0,0);

Everything fits when I divide ins$$anonymous$$d of multiply! Thanks a lot again!

avatar image Grhyll · Nov 01, 2015 at 10:37 AM 0
Share

Thanks you so much, I spent way too much time looking for this, trying to understand why it didn't behave the same on editor and device...

avatar image
0

Answer by wechat_os_Qy0_X1ITy6VrZHYPJFd58JGlA · Jan 08, 2019 at 05:53 AM

Thanks very much!! I have spent 4 hours to fix this problem. this answer is what I am looking for. I have done some further improvement about that problem. I found the Screen-Canvas Ratio depends on what we set in the inspector, Canvas > Canvas Scaler > Screen Match Mode. If the Match is Width, we just multiply the size.x and size.y value with Screen-Canvas Ratio. It is my code. Tips: it is for 2Dgame.

 private CanvasScaler canvasScaler;
 private float ScreenCanvasRatio 
 {
     get
     {
         if (canvasScaler == null)
         {
             canvasScaler = GameObject.FindObjectOfType<CanvasScaler>();
         }

         if (canvasScaler)
         {
             return Screen.width / canvasScaler.referenceResolution.x;
         }
         else
         {
             return 1;
         }
     }
 }

     public Vector3 GetRectTransformFitSize(RectTransform rt)
 {
     Vector3 newVector = new Vector3();

     newVector.x = rt.rect.size.x * this.ScreenCanvasRatio;
     newVector.y = rt.rect.size.y * this.ScreenCanvasRatio;
     return newVector;
 }
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

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

21 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

Related Questions

uGUI - resize RectTransform at runtime to fill entire screen 1 Answer

RectTransform.position of Panel always 0 in FullHD 0 Answers

Recttransform and Screen Resolution problem 0 Answers

Set Rect Transform Screen Position 0 Answers

this code doesnt work when the buttons anchor are not same(custom anchor) 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