簡體   English   中英

Unity - 無法在圖像中心內將小地圖視口矩形居中

[英]Unity - Can't center mini-map viewport rect inside center of the image

如何將圖像位置轉換為相機視口視圖?

我有一個雷達,我使用相機在屏幕上顯示該視口,但它僅適用於一組矩形,該矩形僅適用於我設置的一種類型的屏幕。

如何將此矩形與圖像變換位置匹配。 所以如果屏幕發生變化並且隨着它移動,它周圍的 ui 也會隨之移動相機視圖。

這是 iPhone XS Max (2688x1242) 的屏幕

我想要一些像overlayCamera.rect = transform.position

這是我用來顯示相機矩形的代碼。

private void UpdateCameraViewportRect()
{
    float aspectRatio = (float)Screen.width / Screen.height;
    float desiredScreenWidth = 0.271f;
    float viewHeight = 0.831f;
    float viewWidth = 0.78f;

    float width = desiredScreenWidth;
    float height = width * aspectRatio;
    if (height > 1.0f)
    {
        height = 1.0f;
        width = height / aspectRatio;
    }
    
    //  Controls the position of the view
    overlayCamera.rect = new Rect(
        viewHeight - width, viewWidth - height,
        width, height
    );
    
    /*
    // Method 1 -> Does not work
    Vector3 p = overlayCamera.ViewportToWorldPoint(new Vector3(1, 1, overlayCamera.nearClipPlane));
    Debug.Log(p);

    // Method 2 -> Does not work
    Vector3 screenPos = overlayCamera.WorldToScreenPoint(overlayCamera.transform.position);
    float posx = screenPos.x - (width * 0.5f);
    float posy = screenPos.y - (height * 0.5f);
    Debug.Log(posx + " & " + posy);

    // Method 3 -> Does not work
    overlayCamera.rect = radarBorder.rectTransform.rect;
    */
}

如何將視口矩形居中放置在金色邊框圖像的中心?

解決方案:改為創建 RenderTexture 並將其顯示在 RawImage 中以在 UI 中移動。

好吧,我可能誤解了,但看起來這種方法是根據屏幕的縱橫比動態調整 UI。

最好在編輯器中正常布局 UI,並使用Camera.aspect 強制 overlayCamera的縱橫比為 1:1:

void Start(){
    var cam = GetComponent<Camera>();
    cam.aspect = 1;
    cam.ResetAspect();
}

編輯:

雖然可以調整相機在屏幕上渲染的位置,但您可以看到它有點凌亂。 通常的做法是渲染到 RenderTexture並將其放置在 UI 內的材質上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM