[英]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.