繁体   English   中英

将精灵放置在相机的边缘(Unity)

[英]Position sprite at the edge of a camera (Unity)

我是Unity的新手,在过去的一天中,我无法将简单的Sprite游戏对象放置在相机的边缘,在iPhone和iPad上看起来不错(因此基本上是两个长宽比)。

例如,这就是在iPad上的外观(在iPhone上看起来应该相同)

在此处输入图片说明

这就是它在iPhone设备(或基本上任何其他电话)上的外观。

在此处输入图片说明

那么,我该怎么做才能使第二张图片上的球看起来像第一张图片上的球?

先感谢您!

基于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM