簡體   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