将精灵定位在相机的边缘(Unity)

时间:2016-05-27 14:54:55

标签: c# unity3d

我是Unity的新手,过去的一天我很难将一个简单的精灵游戏对象放到相机的边缘,在iPhone和iPad上显得很好(基本上是两个宽高比)。

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

enter image description here

这就是iPhone设备(或基本上任何其他手机)的外观。

enter image description here

那么我需要做什么,第二张图像上的球看起来和第一张图像一样?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

基于相机OrthographicSize 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为您的图像)

enter image description here

这就是Ipad Mini Retina屏幕的结果(15636 x 2048作为您的图像)

enter image description here

...根据您的精灵PIXEL_TO_UNITS值改变PixelToUnits变量值以获得所需的屏幕尺寸。确保停止场景,然后更改代码的分辨率以更新相机大小,因为我们的代码在Start方法中...

Check more here!