Reputation: 796
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?
Upvotes: 1
Views: 12282
Reputation: 19429
they must have added this recently http://docs.unity3d.com/ScriptReference/Sprite-pixelsPerUnit.html
Example:
(myGameObject).GetComponent<SpriteRenderer>.sprite.pixelsPerUnit;
Upvotes: 2
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