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
5
Question by Brocccoli · Mar 17, 2016 at 10:33 PM · c#spritedynamicpivot

Move Sprite pivot dynamically via C# script

I have a set of objects that grow/shrink in different directions.

The only way I've found how to do this is to set the pivot of the sprite and then scale x and y accordingly. As of right now I have a bunch of sprite prefabs saved with pivot positions set to multiple points on the sprite depending on how I want the object to grow.

I would like to instead be able to programmatically set the pivot of the sprite via C# script so that I may instantiate a sprite at run time (level load) and use it as needed. This seems to be a cleaner more elegant way of doing things.

However, I have no idea how I would go about doing such a thing. All questions that have been asked similar to this one have been ended with an answer to use the Sprite Editor.

Any ideas?

Thanks.

Comment
Add comment · Show 1
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 Ali-hatem · Mar 18, 2016 at 10:07 AM 1
Share

i have tried but i got this message

Property or indexer `UnityEngine.Sprite.pivot' cannot be assigned to (it is read only)

so unity says it's read only

 public Sprite sp;
 void Start(){
     sp = GetComponent<Sprite> ();
     sp.pivot = new Vector2 ();
 }

2 Replies

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

Answer by Zorkman · Mar 18, 2016 at 12:57 PM

There seems to be many different solutions for this.

Option 1:


You can make a prefab consisting of an empty root object and a child object with a sprite renderer:

-----> HandleObject (root, empty)
----------> SpriteObject (child, with sprite renderer)

Then by positioning the SpriteObject on different places you will get different behaviour when scaling the HandleObject. You can set the position of the SpriteObject manually in editor, or then you can do it by code, using your own created algorithm that positions it correctly for your intended behaviour. This certainly needs some trial and error before getting it correct.

Option 2:


You could instead use the Image component of Unity's UI system and having the Canvas set to World Space. With the UI system you then have access to the pivot property of the RectTransform component.

Option 3:


By looking at the docs you can see that the Sprite class has a static function called Sprite.Create http://docs.unity3d.com/ScriptReference/Sprite.Create.html With this you can make your own sprite at run-time by specifying its texture, pivot etc. I cannot say what this means in terms of garbage, but I assume you haven't addressed this as much since you are using Instantiate instead of an Object Pool.

Comment
Add comment · Show 3 · 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 Brocccoli · Mar 18, 2016 at 01:02 PM 0
Share

Thanks @Zorkman and @Ali hatem. You guys provided some good insight into the problem.

After reading both of your answers I think I am going to just stick to how I set the pivots currently. It seems like a lot of work for something that may not even be necessary.

Thanks again.

avatar image Zorkman Brocccoli · Mar 18, 2016 at 01:04 PM 0
Share

No problem, did you check the update I did on Option 3 btw? It seems like that one should suit you needs perfectly.

avatar image Brocccoli Zorkman · Mar 18, 2016 at 05:04 PM 0
Share

That actually looks like exactly what I asked for.

However, upon some further thought, I thought it might be prudent to keep the process the way it is, and here's why.

$$anonymous$$y game is currently in prototype, so while it WOULD be helpful to be able to programmatically assign a pivot at run-time, when I finally get real textures/sprites/animations, I think I'll want to be able to add the pivots exactly where I want them in the sprite editor for certain objects, then I can just create prefabs and reuse them as needed.

I do appreciate your help though. Thanks again.

avatar image
0

Answer by Lttldude · Dec 06, 2020 at 05:13 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Editor script - set sprite pivot for selected assets 0 Answers

What to use for dynamic loading of assets instead of Resources.Load()? 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