繁体   English   中英

在屏幕上以不同的分辨率/纵横比(正交)放置对象

[英]Position object on screen in different resolutions / aspect ratios (orthographic)

老实说,我对Unity中的世界坐标,屏幕坐标和视口坐标感到迷茫。
我的问题很简单:在2D游戏中,无论分辨率和屏幕纵横比是多少,如何将对象放置在左下角?

您的描述有点含糊,但我认为您在谈论这一点:

Vector3 screenPos = new Vector3(x,y,z);

camera.ScreenToWorldPoint(screenPos);

附带说明一下,有一些用于2D Unity的特定算法,也可以进行搜索。

对于正交检查,请检查此统一空间,这可能对您有所帮助:

http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html

我看不到有人跟进此事。 首先让我们直接讲一些术语:Camera.main =看着游戏世界的主相机“ game world” =您绘制的整个游戏地图World Point =在游戏世界中的绝对唯一位置。 可以是2D或3D(x,y,z)屏幕点=屏幕上像素的2D x,y位置

因此,当您要放置一个对象(即转换其位置)时,您真正要做的就是将其放置在游戏世界中的某个位置。 如果相机恰好正看着世界中的那个位置,那么它将出现在屏幕上。

要弄清楚世界上哪些部分当前在屏幕上,您必须将屏幕点转换为世界点。 所以...假设对象的大小为20x20,请尝试以下操作:

//Attach this script to the item you want "pinned" to the bottom, left corner of the screen
void Update() {
  //fetch the rectangle for the whole screen
  Rect viewportRect = Camera.main.pixelRect; //again, this has nothing to do with the World, just the 2D screen "size", basically

  //now, let's pick out a point on the screen - bottom, left corner - but leave room for the size of our 20x20 object
  Vector3 newPos = new Vector3(viewportRect.xMin + 20, Camera.main.pixelHeight - 20, 0);

  //now calculate where we need to place this item in the World so that it appears in our Camera's view (and, thus, the screen)
  this.transform.position = Camera.main.ScreenToWorldPoint(newPos);
}

我98%确信这都是准确的信息,但是如果有人发现错误,请指出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM