统一校正精灵/纹理宽度高度



我发现了一些关于Unity Sprite和纹理的有趣东西(Texture2D(。我装箱了一个 50x50 的.png,并通过附加到GameObject并使用 SpriteRenderer 在 Unity 中渲染它。

意识到,每当我调用与 Unity 相关的方法(sprite.texture.widthsprite.rect.widthsprite.textureRect.width 等(时,它总是返回 50。但是,图像的实际大小变为 24x24 或 12x12,具体取决于我屏幕上的分辨率。

当然,这并不奇怪,因为在Unity渲染屏幕上的内容之前应用投影等;但是,有趣的部分我找不到任何方法或简单的方法来获取投影后Sprite的大小。

仍然可以制作自己的投影来得出相关的大小;但是,我想知道是否有更简单的方法来获取这些信息。

谢谢!

@Draco18s提到的方法似乎是解决此问题的唯一方法。

因此,我装了一个装有RectTransformSpriteRendererPrefab GameObject,并得到如下宽度和高度:

GameObject twoSide = Instantiate(Resources.Load(mFilePath + "Locater")) as GameObject;
twoSide.GetComponent<RectTransform>().position = Camera.main.ScreenToWorldPoint (new Vector3 (0, 0, 1));
twoSide.GetComponent<RectTransform> ().pivot = new Vector2 (0f, 0f);    // RectTransform should have the same pivot location with Sprite
float width = twoSide.GetComponent<RectTransform> ().offsetMax.x - twoSide.GetComponent<RectTransform> ().offsetMin.x;
float height = twoSide.GetComponent<RectTransform> ().offsetMax.y - twoSide.GetComponent<RectTransform> ().offsetMin.y;

最新更新