user1423893
user1423893

Reputation: 796

How do I access the Pixels To Units property for a Unity sprite from a script

I would like to access the Pixels To Units property of a sprite from within a C# script in Unity.

If I get either the localScale or lossyScale properties they need to be divided by this number in order to scale in accordance with other objects that may have different values for their Pixels To Units property.

How can I get the value of this property or scale correctly in this situation?

PixelsToUnityUnits

Upvotes: 1

Views: 12282

Answers (2)

Rakka Rage
Rakka Rage

Reputation: 19429

they must have added this recently http://docs.unity3d.com/ScriptReference/Sprite-pixelsPerUnit.html

Example:

(myGameObject).GetComponent<SpriteRenderer>.sprite.pixelsPerUnit;

Upvotes: 2

Stefan Hoffmann
Stefan Hoffmann

Reputation: 3234

You can't access the property from a script, but you can calculate the factor yourself.

Sprite mySprite;
float pixel2units = mySprite.rect.width / mySprite.bounds.size.x;

Upvotes: 6

Related Questions