- Home /
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.
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 ();
}
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.
No problem, did you check the update I did on Option 3 btw? It seems like that one should suit you needs perfectly.
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.
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
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();
Your answer
![](https://koobas.hobune.stream/wayback/20220612071953im_/https://answers.unity.com/themes/thub/images/avi.jpg)