【问题标题】:How do I access the Pixels To Units property for a Unity sprite from a script如何从脚本访问 Unity 精灵的像素到单位属性
【发布时间】:2014-06-05 19:19:53
【问题描述】:

我想在 Unity 的 C# 脚本中访问精灵的像素到单位属性。

如果我获得 localScale 或 lossyScale 属性,则需要将它们除以该数字,以便根据其他对象的 Pixels To Units 属性可能具有不同值进行缩放。

在这种情况下如何正确获取该属性的值或缩放?

【问题讨论】:

    标签: unity3d 2d sprite


    【解决方案1】:

    您无法通过脚本访问该属性,但您可以自己计算因子。

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

    【讨论】:

    • 太好了,谢谢。我今晚会试试看。
    【解决方案2】:

    他们一定是最近添加的 http://docs.unity3d.com/ScriptReference/Sprite-pixelsPerUnit.html

    例子:

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

    【讨论】:

    • 现在这是更优雅的解决方案,可以直接从精灵本身获取价值,如所要求的。我在答案中添加了一个示例,但如果它没有获得批准,这里有一个示例:(myGameObject).GetComponent<SpriteRenderer>.sprite.pixelsPerUnit; 不知道为什么这被否决了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2022-12-05
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多