Unity3d 统一正确的精灵/纹理宽度高度

Unity3d 统一正确的精灵/纹理宽度高度,unity3d,textures,sprite,projection,Unity3d,Textures,Sprite,Projection,我发现了有关UnitySprite和纹理(Texture2D)的一些有趣的东西。我用板条箱装了一个50x50.png,通过连接到一个游戏对象并使用SpriteRender将其渲染成一个整体 我意识到,每当我调用与Unity相关的方法(sprite.texture.width,sprite.rect.width,sprite.texture.width,等等),它总是返回50。但是,图像的实际大小会根据屏幕上的分辨率变成24x24或12x12 当然,这并不奇怪,因为投影等是在Unity渲染屏幕上的

我发现了有关Unity
Sprite
和纹理(
Texture2D
)的一些有趣的东西。我用板条箱装了一个50x50
.png
,通过连接到一个
游戏对象
并使用
SpriteRender
将其渲染成一个整体

我意识到,每当我调用与Unity相关的方法(
sprite.texture.width
sprite.rect.width
sprite.texture.width
,等等),它总是返回50。但是,图像的实际大小会根据屏幕上的分辨率变成24x24或12x12

当然,这并不奇怪,因为投影等是在Unity渲染屏幕上的东西之前应用的;然而,有趣的是,在应用投影后,我找不到任何方法或简单的方法来获得
精灵的大小

我仍然可以自己预测出相关的尺寸;然而,我想知道是否有更简单的方法来获取这些信息


谢谢大家!

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

因此,我用板条箱装了一个
预制件
游戏对象
,其中包含
RectTransform
SpriteRenderer
,得到的宽度和高度如下:

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;
gameobjecttwoside=实例化(Resources.Load(mFilePath+“Locater”))为GameObject;
twoSide.GetComponent().position=Camera.main.ScreenToWorldPoint(新矢量3(0,0,1));
twoSide.GetComponent().pivot=新向量2(0f,0f);//RectTransform应该与Sprite具有相同的轴位置
float width=twoSide.GetComponent().offsetMax.x-twoSide.GetComponent().offsetMin.x;
浮动高度=twoSide.GetComponent().offsetMax.y-twoSide.GetComponent().offsetMin.y;

这不是一个容易回答的问题。图像可以在应用了比例等的变换内,但可以从场景单位转换为屏幕坐标。因此,如果你知道角点在哪里,你可以做
Camera.main.WorldToScreenPoint(角点)
可以从中计算出来。我可能错了,但渲染图像的“真实”大小取决于你的屏幕分辨率、相机大小和精灵的PPU值。@Draco18s它工作不正常。我尝试使用sprite.rect.max返回世界空间,结果它返回(-7.3,-4.0)而不是(-4.166,-4.166)
sprite.rect.max
是sprite在其底层纹理中的UV坐标,在这里使用它们是完全错误的。您需要精灵的
RectTransform