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 GarrickWinter · Mar 31, 2019 at 03:43 PM · uicanvasscreen size

Canvas Pivot in Screen Space Overlay

Hey all, I'm finally biting the bullet and moving my older projects to the new Unity UI (about time!), and I'm having a hard time with the pivot and anchor system. Here's what I want:
.

A UI element, generated at runtime through script, that is positioned such that its upper left corner is always in the upper left corner of the screen, even on screen resize.
.

Here's what I'm doing:

         RectTransform rectButton = objElement.GetComponent<RectTransform>();
         rectButton.sizeDelta = new Vector2(600, 200);
         rectButton.pivot = new Vector2(0f, 1f);
         rectButton.localPosition = GetRelativePos(main.canvasScreenUi.GetComponent<RectTransform>(), new Vector2(0f, 1f));

Where:

     protected static Vector2 GetRelativePos(RectTransform parent, Vector2 positionRatios)
     {
         //Clamp the ratios
         positionRatios.x = Mathf.Clamp(positionRatios.x, 0f, 1f);
         positionRatios.y = Mathf.Clamp(positionRatios.y, 0f, 1f);
         //Get the results
         return new Vector2(parent.rect.width * (positionRatios.x - parent.pivot.x),
                            parent.rect.height * (positionRatios.y - parent.pivot.y));
     }

Why am I doing this? Because the pivot point on the Canvas is set to (0.5,0.5) and that appears to be immutable so long as I'm in Screen Space - Overlay. Which is the mode I want, I think? I don't want my UI floating around in the 3D world or doing anything weird, I want it to be strictly confined to the 2D plane of the screen. If this isn't the mode I want for strictly screen-space UI, why not? If it is, why can't I adjust the pivot?
.

Because this is causing a second problem - while this puts the element where I want it initially, if I resize the screen, the element obviously remains at the exact same distance from the Canvas' pivot, which is the center of the screen. So if I widen the screen, for example, the button is now detached from the edge and closer to the center.
.

I'm sure there must be a way to deal with this with anchors or something, but I'm still completely unfamiliar with the system and I'm not seeing anything in the documentation that helps me understand what's going on.
.

TL;DR: How do I get script-generated UI elements to stick to the edge of the screen?

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

0 Replies

· Add your reply
  • Sort: 

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

175 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make game objects in the game scene proportionally adjust position for different screen sizes? 1 Answer

UNFIT SCREEN SIZE FOR MOBILE 0 Answers

Snap UI Text inside canvas 1 Answer

Render UI with RenderTexture to final render result 0 Answers

5.3.5 - Blurry UI text after switching canvasses - until text is refreshed by turning visibility off/on or changing resolution 4 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