Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by DustyScreen · Oct 01, 2015 at 11:56 AM · spriteui imagepivot

UI Image: Using Sprite's pivot on the RectTransform

I'm creating a game, where I load sprites to UI Images on a canvas. When placing a UI Image, I need to set the pivot of it's RectTransform, so it matches the pivot of the sprite.

It seems like an obvious thing to do, but I can't get it to work.

Is that somehow possible?

I tried the following, with little luck:

     GetComponent<RectTransform> ().pivot = GetComponent<Image> ().sprite.pivot;

Any help is most welcome :-)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Chaky · Jan 18, 2016 at 12:51 PM

Sprite.pivot info is by pixel, no percentage, which UI RectTransform uses. This is the script I use to do this conversion.

Vector2 size = GetComponent<RectTransform>().sizeDelta;
Vector2 pixelPivot = GetComponent<Image>().sprite.pivot;
Vector2 percentPivot = new Vector2(pixelPivot.x / size.x, pixelPivot.y / size.y);
GetComponent<RectTransform>().pivot = percentPivot;

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
5

Answer by robal1991 · Mar 31, 2016 at 09:59 AM

Sometimes your sprite has also custom value of pixels per unit set. In that case you do:

 Vector2 size = imgObj.GetComponent<RectTransform>().sizeDelta;
 size *= imgObj.GetComponent<Image>().pixelsPerUnit;
 Vector2 pixelPivot = imgObj.GetComponent<Image>().sprite.pivot;
 Vector2 percentPivot = new Vector2(pixelPivot.x / size.x, pixelPivot.y / size.y);
 imgObj.GetComponent<RectTransform>().pivot = percentPivot;
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 mhmtbtn · Mar 16 at 12:05 PM 0
Share
 size *= imgObj.GetComponent<Image>().pixelsPerUnit;

avatar image
1

Answer by makspll · Apr 20, 2020 at 08:54 AM

I know it's an old answer but here is a one liner:

 yourRectTransform.pivot = yourSprite.pivot / yourSprite.rect.size;

@TobyKaos 's answer is close but the pivot returned by the sprite is in pixels, and rectTransform's is in "normalized" form i.e. expressed as a fraction of the total width/height of the

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 TobyKaos · Oct 01, 2015 at 12:05 PM

I think you should do:

 myObject.GetComponent<RectTransform>().pivot = new Vector2(myObject.GetComponent<Image>().sprite.pivot);

It is like when you want to set a new position you must create a new Vector3. Here a Vector2

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

38 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

Related Questions

UI Image not appear in mac / ios 1 Answer

Image UI color not changing when sprite is set via script 0 Answers

How to achieve this ? ( pivoting) ! (2D sprite) 0 Answers

Dynamically setting gameobject sprites in UI Image 0 Answers

How do I size a UI Image to fit the sprite exactly? 2 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