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
2
Question by Bloody-Swamp · Feb 10, 2014 at 01:55 AM · spriteruntimeboundspivot

Change Sprite Pivot/Bounds from Script

I searched all over the place (Google, Forums, here at Answers) with no luck, so excuse me if this is already answered (somewhere).

I would like to change the pivot of a Sprite through scripting.

So normally I would write:

 Sprite sprite; // some sprite reference
 sprite.bounds.center = Vector2.zero; // the preferable pivot

Or if there was a "pivot" set method like

 sprite.SetPivot(Vector2.zero); // or something similar

Since sprite.bounds is property AND readonly it cannot be assigned (Even if I create a new Bounds variable). There is not even a setter property in Sprite class to do the job, so my question is, is there a way to change sprite's pivot through scripting?

Thank you.

Comment
Add comment · Show 2
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 robertbu · Feb 10, 2014 at 02:27 AM 0
Share

From reading the script reference, the only way I could see to change the anchor is to use Sprite.Create() to create a new sprite from a texture.

avatar image Bloody-Swamp · Feb 10, 2014 at 04:07 AM 0
Share

I don't think that would solve the problem (sort of). It would lose batching and that would be ugly... After hours of searching, still nothing.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Feb 10, 2014 at 04:41 AM

I don't know how much of the Sprite technology you need. You could make the sprite a child of an empty game object and use localPosition to change the anchor. You could also roll your own sprite using a Quad, or there are third-party solutions. Sorry I cannot be more helpful.

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
1

Answer by Lttldude · Dec 06, 2020 at 05:22 AM

I made a tool for setting the Sprite pivot points in the editor while maintaining their world positions: https://github.com/thepowerprocess/UnitySpritePivotEditor


alt text


This is the part where the sprite texture is edited:

 SpriteRenderer sr = selectedGameObject.GetComponent<SpriteRenderer>();
 string path = AssetDatabase.GetAssetPath(sr.sprite.texture);
 TextureImporter ti = (TextureImporter)AssetImporter.GetAtPath(path);
 Vector2 newPivot = new Vector2(childMousePos.x / (sr.sprite.texture.width / sr.sprite.pixelsPerUnit), childMousePos.y / (sr.sprite.texture.height / sr.sprite.pixelsPerUnit)) + ti.spritePivot;
 ti.spritePivot = newPivot;
 TextureImporterSettings texSettings = new TextureImporterSettings();
 ti.ReadTextureSettings(texSettings);
 texSettings.spriteAlignment = (int)SpriteAlignment.Custom;
 ti.SetTextureSettings(texSettings);
 ti.SaveAndReimport();
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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Create colored rectangle at runtime 1 Answer

Using Texture2D created at runtime to create a Sprite decreases Sprite quality. (Android) 1 Answer

How do you get the world-space position of a sprite with a custom pivot? 1 Answer

How to get pivot of sprite in script 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