How to get pivot of sprite in script

I want to be able to get the Pivot Point of the Sprite attached to a GameObject. I found out there is a variable called pivot in Sprite in the ** ** but the variable does not have the value I’m looking for. I want the value of the Custom Pivot Point I set in the Sprite Editor.

Hi, you probably found the solution by now. For anyone else, try this

public Vector2 GetSpritePivot(Sprite sprite)
	{
		Bounds bounds = sprite.bounds;
		var pivotX = - bounds.center.x / bounds.extents.x / 2 + 0.5f;
		var pivotY = - bounds.center.y / bounds.extents.y / 2 + 0.5f;

		return new Vector2(pivotX, pivotY);
	}

If you are trying to get the pivot in pixels from the sprite, then I know what the problem is. Unity’s pivot calculations are off because it uses 0 to the length of a side of the sprite. This would mean is calculating the sprite’s length + 1. This probelm is furthur compounded when Unity divides this value by the original length, which creates some unwanted decimal numbers.

For example:

How It Should Work (By dividing 1 (inclusive) to 3 (inclusive) evenly and then selecting the number of pixels from the bottom left corner )
3 X X X
2 X X X
1 X X X
  1 2 3

How Unity Does it ((By dividing 0 (inclusive) to 3 (inclusive) evenly and then selecting the number of pixels from the bottom left corner.)
3   X  X   X
1.5 X  X   X
0   X  X   X
    0  1.5 3