我发现了一些关于Unity Sprite
和纹理的有趣东西(Texture2D
(。我装箱了一个 50x50 的.png
,并通过附加到GameObject
并使用 SpriteRenderer
在 Unity 中渲染它。
意识到,每当我调用与 Unity 相关的方法(sprite.texture.width
、sprite.rect.width
、sprite.textureRect.width
等(时,它总是返回 50。但是,图像的实际大小变为 24x24 或 12x12,具体取决于我屏幕上的分辨率。
当然,这并不奇怪,因为在Unity渲染屏幕上的内容之前应用投影等;但是,有趣的部分我找不到任何方法或简单的方法来获取投影后Sprite
的大小。
仍然可以制作自己的投影来得出相关的大小;但是,我想知道是否有更简单的方法来获取这些信息。
谢谢!
@Draco18s提到的方法似乎是解决此问题的唯一方法。
因此,我装了一个装有RectTransform
和SpriteRenderer
的Prefab
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;