Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by jmorhart · May 29, 2015 at 12:30 AM · uipositionrecttransformpivot

Set a RectTranform's pivot without changing its position

Is there a way to change a RectTransform's pivot point without changing its position? If not, does someone know the math I would have to do to simulate this behavior? Thanks in advance!

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

3 Replies

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

Answer by jmorhart · Jul 30, 2015 at 05:35 PM

Well, I managed to figure this one out on my own. Implemented as an extension method of the RectTransform class. Currently only works properly for XY axis-aligned objects.

     public static void SetPivot(this RectTransform rectTransform, Vector2 pivot)
     {
         if (rectTransform == null) return;
 
         Vector2 size = rectTransform.rect.size;
         Vector2 deltaPivot = rectTransform.pivot - pivot;
         Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y);
         rectTransform.pivot = pivot;
         rectTransform.localPosition -= deltaPosition;
     }
Comment
Add comment · Show 7 · 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 KwahuNashoba · Dec 12, 2016 at 04:22 PM 0
Share

Thanks man! This does not make sense AT ALL! :D But it works. I have spent whole week trying to assign x,y position of RectTransform, also anchoredPosition and it never came to my $$anonymous$$d that it was this simple :)

avatar image Reyan62 · Feb 06, 2017 at 06:30 PM 0
Share

Awesome, I needed to do just this and was able to drop in your solution with no problems. Thanks!

avatar image dugsteen · Jul 12, 2017 at 04:42 PM 4
Share

Note: If you're changing scale at all, you'll need to incorporate that as well:

 var scale = rectTransform.localScale;

...

 var deltaPosition = new Vector3(
   deltaPivot.x * size.x * scale.x, 
   deltaPivot.y * size.y * scale.y);

avatar image Roywise · Sep 11, 2017 at 01:40 PM 0
Share

Do you know how to implement this method when the RectTransform has been rotated as well? It seems to be a bit more complex and I can't figure it out.

avatar image Issun · Sep 17, 2017 at 11:30 AM 0
Share

+Props to you dude, solid solution and awesome of you to put it in a method for people to copy paste.

Show more comments
avatar image
8

Answer by LiterallyJeff · Aug 24, 2018 at 03:20 PM

Here is a cleaned up version of previous commenters' extension function, which supports scaling & rotation.

 /// <summary>
 /// Set pivot without changing the position of the element
 /// </summary>
 public static void SetPivot(this RectTransform rectTransform, Vector2 pivot)
 {
     Vector3 deltaPosition = rectTransform.pivot - pivot;    // get change in pivot
     deltaPosition.Scale(rectTransform.rect.size);           // apply sizing
     deltaPosition.Scale(rectTransform.localScale);          // apply scaling
     deltaPosition = rectTransform.rotation * deltaPosition; // apply rotation
     
     rectTransform.pivot = pivot;                            // change the pivot
     rectTransform.localPosition -= deltaPosition;           // reverse the position change
 }
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 eses · Aug 24, 2018 at 03:47 PM 0
Share

@ jeffreyschoch - This is great! Thanks for sharing.

avatar image jomalomal · Feb 06, 2019 at 06:29 PM 0
Share

To get this to work I had to replace rectTransform.rotatation with rectTransform.transform.localRotation

avatar image
5

Answer by wck19870829 · Mar 14, 2019 at 08:52 AM

     public static void SetPivot(RectTransform target, Vector2 pivot)
     {
         if (!target) return;

         var offset=pivot - target.pivot;
         offset.Scale(target.rect.size);
         var wordlPos= target.position + target.TransformVector(offset);

         target.pivot = pivot;
         target.position = wordlPos;
     }
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 InteractiveScape · Jan 24, 2020 at 09:34 AM 0
Share

Thanks! That was the only solution that worked in my case. Not sure why the previous solutions caused problems in my setup...

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

29 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

Related Questions

How to force counter-adjusting of RectTransform Position when changing the Pivot at runtime? 1 Answer

Change RectTransform pivot without changing position 0 Answers

How to move gameobject (main camera) to another gameobject (UI camera) position ? 1 Answer

Camera rotation around player while following. 6 Answers

Make position of Buttons Elastic. 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