2014-03-12 81 views

回答

2

這是一個有點含糊你的描述,但我覺得你講的這一點:

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

camera.ScreenToWorldPoint(screenPos); 

作爲一個側面說明,對於2D團結的具體算法,搜索之謂也。

對於正投影檢查這種統一空間,這將幫助您:

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

+0

說實話,我不太瞭解你的代碼。假設我們有一個名爲Player的對象。無論縱橫比和分辨率如何,您如何將其位置改變到左下角? – BanzaiTokyo

+0

@BanzaiTokyo,首先可以肯定的是,您是否使用正交相機投影或透視? – tweellt

+0

我使用正字法 – BanzaiTokyo

1

我看沒有人會跟進這一點。讓我們先看一些術語: Camera.main =正在看你的遊戲世界的主相機 「遊戲世界」=你繪製的整個遊戲地圖 世界點=遊戲世界中絕對的,獨特的位置。可以是2D或3D(X,Y,Z) 屏幕分=二維x,y位置的像素在屏幕上

所以,當你想放置的物體(ietransform它的位置),你是什麼真正做的是把它放在遊戲世界的某個地方。如果相機碰巧看到世界上的那個位置,那麼它會出現在屏幕上。

要想知道世界上哪些部分當前正在屏幕上,您必須將屏幕點轉換爲世界點。所以......假設你的對象的大小是20×20,試試這個:

//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%肯定這是所有準確的信息,但如果有人看到一個錯誤,請指出來。

+0

這似乎是正確的。我對Camera.main.pixelRect的理解是它在整個屏幕上可見的矩形。這不一定是屏幕的全尺寸,也不一定是從屏幕的左下角開始。最好使用Screen.Width/Screen.Height,並使用百分比偏移量而不是絕對值來更好地處理多個分辨率。 –