【发布时间】:2018-08-25 12:17:03
【问题描述】:
我知道网上有很多这样的问题,但我还没有找到解决我的问题的方法......
我正在尝试制作一个填满屏幕的背景图像。在我的游戏中,有一个画布,在这个画布内部有一个附加了精灵组件的游戏对象。
许多解决方案都包含此代码:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
double variable = Camera.main.orthographicSize * 4.0;
float worldScreenHeight = (float)variable;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
transform.localScale = new Vector2(worldScreenWidth / width, worldScreenHeight / height);
}
但我试过了,它使游戏对象变得非常小。 有什么想法吗?
【问题讨论】:
-
为什么不在画布内使用 UI Image 而不是 sprite?
-
我试过这种方式,但我遇到了另一种问题:stackoverflow.com/questions/52016176/…
-
好的;我编辑了您的代码,并将向您发布答案:)
标签: unity3d sprite screen-size gameobject