[英]Position sprite at the edge of a camera (Unity)
基於Orthographic
攝影機, Size 5
屏幕尺寸為1536 x 2048(圖像中的Ipad Mini Retina)。
創建新腳本ResizeCamera
並將此腳本添加到MainCamera
:
using UnityEngine;
public class ResizeCamera : MonoBehaviour {
// Use this for initialization
void Start () {
float TARGET_WIDTH = 1536.0f;
float TARGET_HEIGHT = 2048.0f;
float PIXELS_TO_UNITS = 102.4f; // 1:1 ratio of pixels to units
float desiredRatio = TARGET_WIDTH / TARGET_HEIGHT;
float currentRatio = (float)Screen.width/(float)Screen.height;
if(currentRatio >= desiredRatio)
{
// Our resolution has plenty of width, so we just need to use the height to determine the camera size
Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS;
}
else
{
// Our camera needs to zoom out further than just fitting in the height of the image.
// Determine how much bigger it needs to be, then apply that to our original algorithm.
float differenceInSize = desiredRatio / currentRatio;
Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS * differenceInSize;
}
}
}
這就是HTC屏幕的結果(1080 x 1920作為圖像)
那就是Ipad Mini Retina屏幕的結果(15636 x 2048作為您的圖像)
...根據您的精靈PixelToUnits
值改變PIXEL_TO_UNITS
變量值以獲得所需的屏幕尺寸。 確保停止場景,然后更改代碼的分辨率以更新相機尺寸,因為我們的代碼位於Start方法中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.